mirror of
https://github.com/freckle/yesod-auth-oauth2.git
synced 2026-04-04 19:19:46 +02:00
Switch to newer Nylas API endpoint
This commit is contained in:
parent
43d15d8478
commit
d3c60bf47c
@ -16,7 +16,6 @@ import Data.Aeson (FromJSON, Value(..), parseJSON, decode, (.:))
|
|||||||
import Data.Monoid ((<>))
|
import Data.Monoid ((<>))
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import Data.Text.Encoding (encodeUtf8, decodeUtf8)
|
import Data.Text.Encoding (encodeUtf8, decodeUtf8)
|
||||||
import Data.Vector ((!?))
|
|
||||||
import Network.HTTP.Client (applyBasicAuth, parseUrl, httpLbs, responseStatus
|
import Network.HTTP.Client (applyBasicAuth, parseUrl, httpLbs, responseStatus
|
||||||
, responseBody)
|
, responseBody)
|
||||||
import Network.HTTP.Conduit (Manager)
|
import Network.HTTP.Conduit (Manager)
|
||||||
@ -28,25 +27,21 @@ import Yesod.Auth.OAuth2 (OAuth2(..), AccessToken(..)
|
|||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import qualified Network.HTTP.Types as HT
|
import qualified Network.HTTP.Types as HT
|
||||||
|
|
||||||
data NylasNamespace = NylasNamespace
|
data NylasAccount = NylasAccount
|
||||||
{ nylasNamespaceId :: Text
|
{ nylasAccountId :: Text
|
||||||
, nylasNamespaceAccountId :: Text
|
, nylasAccountEmailAddress :: Text
|
||||||
, nylasNamespaceEmailAddress :: Text
|
, nylasAccountName :: Text
|
||||||
, nylasNamespaceName :: Text
|
, nylasAccountProvider :: Text
|
||||||
, nylasNamespaceProvider :: Text
|
, nylasAccountOrganizationUnit :: Text
|
||||||
, nylasNamespaceOrganizationUnit :: Text
|
|
||||||
}
|
}
|
||||||
|
|
||||||
instance FromJSON NylasNamespace where
|
instance FromJSON NylasAccount where
|
||||||
parseJSON (Array singleton) = case singleton !? 0 of
|
parseJSON (Object o) = NylasAccount
|
||||||
Just (Object o) -> NylasNamespace
|
<$> o .: "id"
|
||||||
<$> o .: "id"
|
<*> o .: "email_address"
|
||||||
<*> o .: "account_id"
|
<*> o .: "name"
|
||||||
<*> o .: "email_address"
|
<*> o .: "provider"
|
||||||
<*> o .: "name"
|
<*> o .: "organization_unit"
|
||||||
<*> o .: "provider"
|
|
||||||
<*> o .: "organization_unit"
|
|
||||||
_ -> mzero
|
|
||||||
parseJSON _ = mzero
|
parseJSON _ = mzero
|
||||||
|
|
||||||
oauth2Nylas :: YesodAuth m
|
oauth2Nylas :: YesodAuth m
|
||||||
@ -77,7 +72,7 @@ oauth2NylasScoped scopes clientId clientSecret =
|
|||||||
|
|
||||||
fetchCreds :: Manager -> AccessToken -> IO (Creds a)
|
fetchCreds :: Manager -> AccessToken -> IO (Creds a)
|
||||||
fetchCreds manager token = do
|
fetchCreds manager token = do
|
||||||
req <- authorize <$> parseUrl "https://api.nylas.com/n"
|
req <- authorize <$> parseUrl "https://api.nylas.com/account"
|
||||||
resp <- httpLbs req manager
|
resp <- httpLbs req manager
|
||||||
if HT.statusIsSuccessful (responseStatus resp)
|
if HT.statusIsSuccessful (responseStatus resp)
|
||||||
then case decode (responseBody resp) of
|
then case decode (responseBody resp) of
|
||||||
@ -86,19 +81,18 @@ fetchCreds manager token = do
|
|||||||
else throwIO requestFailure
|
else throwIO requestFailure
|
||||||
where
|
where
|
||||||
authorize = applyBasicAuth (accessToken token) ""
|
authorize = applyBasicAuth (accessToken token) ""
|
||||||
parseFailure = InvalidProfileResponse "nylas" "failed to parse namespace"
|
parseFailure = InvalidProfileResponse "nylas" "failed to parse account"
|
||||||
requestFailure = InvalidProfileResponse "nylas" "failed to get namespace"
|
requestFailure = InvalidProfileResponse "nylas" "failed to get account"
|
||||||
|
|
||||||
toCreds :: NylasNamespace -> AccessToken -> Creds a
|
toCreds :: NylasAccount -> AccessToken -> Creds a
|
||||||
toCreds ns token = Creds
|
toCreds ns token = Creds
|
||||||
{ credsPlugin = "nylas"
|
{ credsPlugin = "nylas"
|
||||||
, credsIdent = nylasNamespaceId ns
|
, credsIdent = nylasAccountId ns
|
||||||
, credsExtra =
|
, credsExtra =
|
||||||
[ ("account_id", nylasNamespaceAccountId ns)
|
[ ("email_address", nylasAccountEmailAddress ns)
|
||||||
, ("email_address", nylasNamespaceEmailAddress ns)
|
, ("name", nylasAccountName ns)
|
||||||
, ("name", nylasNamespaceName ns)
|
, ("provider", nylasAccountProvider ns)
|
||||||
, ("provider", nylasNamespaceProvider ns)
|
, ("organization_unit", nylasAccountOrganizationUnit ns)
|
||||||
, ("organization_unit", nylasNamespaceOrganizationUnit ns)
|
|
||||||
, ("access_token", decodeUtf8 $ accessToken token)
|
, ("access_token", decodeUtf8 $ accessToken token)
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user