{-# LANGUAGE OverloadedStrings #-}
module Yesod.Auth.OAuth2.Nylas
( oauth2Nylas
) where
import Yesod.Auth.OAuth2.Prelude
import Control.Monad (unless)
import qualified Data.ByteString.Lazy.Char8 as BL8
import Network.HTTP.Client
import qualified Network.HTTP.Types as HT
import qualified Yesod.Auth.OAuth2.Exception as YesodOAuth2Exception
newtype User = User Text
instance FromJSON User where
parseJSON :: Value -> Parser User
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"User" forall a b. (a -> b) -> a -> b
$ \Object
o -> Text -> User
User forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"id"
pluginName :: Text
pluginName :: Text
pluginName = Text
"nylas"
defaultScopes :: [Text]
defaultScopes :: [Text]
defaultScopes = [Text
"email"]
oauth2Nylas :: YesodAuth m => Text -> Text -> AuthPlugin m
oauth2Nylas :: forall m. YesodAuth m => Text -> Text -> AuthPlugin m
oauth2Nylas Text
clientId Text
clientSecret =
forall m.
YesodAuth m =>
Text -> OAuth2 -> FetchCreds m -> AuthPlugin m
authOAuth2 Text
pluginName OAuth2
oauth forall a b. (a -> b) -> a -> b
$ \Manager
manager OAuth2Token
token -> do
Request
req <-
ByteString -> ByteString -> Request -> Request
applyBasicAuth (Text -> ByteString
encodeUtf8 forall a b. (a -> b) -> a -> b
$ AccessToken -> Text
atoken forall a b. (a -> b) -> a -> b
$ OAuth2Token -> AccessToken
accessToken OAuth2Token
token) ByteString
""
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). MonadThrow m => String -> m Request
parseRequest String
"https://api.nylas.com/account"
Response ByteString
resp <- Request -> Manager -> IO (Response ByteString)
httpLbs Request
req Manager
manager
let userResponse :: ByteString
userResponse = forall body. Response body -> body
responseBody Response ByteString
resp
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Status -> Bool
HT.statusIsSuccessful forall a b. (a -> b) -> a -> b
$ forall body. Response body -> Status
responseStatus Response ByteString
resp) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwIO forall a b. (a -> b) -> a -> b
$
Text -> String -> YesodOAuth2Exception
YesodOAuth2Exception.GenericError Text
pluginName forall a b. (a -> b) -> a -> b
$
String
"Unsuccessful HTTP response: "
forall a. Semigroup a => a -> a -> a
<> ByteString -> String
BL8.unpack ByteString
userResponse
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
(forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String -> YesodOAuth2Exception
YesodOAuth2Exception.JSONDecodingError Text
pluginName)
( \(User Text
userId) ->
forall (f :: * -> *) a. Applicative f => a -> f a
pure
Creds
{ credsPlugin :: Text
credsPlugin = Text
pluginName
, credsIdent :: Text
credsIdent = Text
userId
, credsExtra :: [(Text, Text)]
credsExtra = OAuth2Token -> ByteString -> [(Text, Text)]
setExtra OAuth2Token
token ByteString
userResponse
}
)
forall a b. (a -> b) -> a -> b
$ forall a. FromJSON a => ByteString -> Either String a
eitherDecode ByteString
userResponse
where
oauth :: OAuth2
oauth =
OAuth2
{ oauth2ClientId :: Text
oauth2ClientId = Text
clientId
, oauth2ClientSecret :: Maybe Text
oauth2ClientSecret = forall a. a -> Maybe a
Just Text
clientSecret
, oauth2AuthorizeEndpoint :: URIRef Absolute
oauth2AuthorizeEndpoint =
URIRef Absolute
"https://api.nylas.com/oauth/authorize"
forall a. URIRef a -> [(ByteString, ByteString)] -> URIRef a
`withQuery` [ (ByteString
"response_type", ByteString
"code")
, (ByteString
"client_id", Text -> ByteString
encodeUtf8 Text
clientId)
,
Text -> [Text] -> (ByteString, ByteString)
scopeParam Text
"," [Text]
defaultScopes
]
, oauth2TokenEndpoint :: URIRef Absolute
oauth2TokenEndpoint = URIRef Absolute
"https://api.nylas.com/oauth/token"
, oauth2RedirectUri :: Maybe (URIRef Absolute)
oauth2RedirectUri = forall a. Maybe a
Nothing
}