mirror of
https://github.com/freckle/yesod-auth-oauth2.git
synced 2026-09-10 19:34:56 +02:00
defined utilities functions to build the queries, added a query to check github organization membership
This commit is contained in:
parent
306062eff3
commit
a63bdcf3b9
@ -10,15 +10,17 @@
|
|||||||
module Yesod.Auth.OAuth2.Github
|
module Yesod.Auth.OAuth2.Github
|
||||||
( oauth2Github
|
( oauth2Github
|
||||||
, oauth2GithubScoped
|
, oauth2GithubScoped
|
||||||
|
, oauth2GithubCheckOrg
|
||||||
, oauth2GithubQueried
|
, oauth2GithubQueried
|
||||||
, module Yesod.Auth.OAuth2
|
, module Yesod.Auth.OAuth2
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Applicative ((<$>), (<*>))
|
import Control.Applicative ((<$>), (<*>))
|
||||||
import Control.Exception.Lifted
|
import Control.Exception.Lifted
|
||||||
import Control.Monad (mzero)
|
import Control.Monad (mzero, void)
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
|
import Data.Map as M
|
||||||
import Data.Monoid (mappend)
|
import Data.Monoid (mappend)
|
||||||
import Data.Text.Encoding (encodeUtf8, decodeUtf8)
|
import Data.Text.Encoding (encodeUtf8, decodeUtf8)
|
||||||
import Yesod.Auth
|
import Yesod.Auth
|
||||||
@ -72,6 +74,14 @@ oauth2GithubScoped :: YesodAuth m
|
|||||||
oauth2GithubScoped clientId clientSecret scopes =
|
oauth2GithubScoped clientId clientSecret scopes =
|
||||||
oauth2GithubQueried clientId clientSecret scopes fetchGithubProfile
|
oauth2GithubQueried clientId clientSecret scopes fetchGithubProfile
|
||||||
|
|
||||||
|
oauth2GithubCheckOrg :: YesodAuth m
|
||||||
|
=> Text -- ^ Client ID
|
||||||
|
-> Text -- ^ Client Secret
|
||||||
|
-> Text -- ^ GitHub Org whose membership the user will be checked against
|
||||||
|
-> AuthPlugin m
|
||||||
|
oauth2GithubCheckOrg clientId clientSecret githubOrg =
|
||||||
|
oauth2GithubQueried clientId clientSecret ["user:email", "read:org"]
|
||||||
|
(fetchGithubProfileCheckOrg githubOrg)
|
||||||
oauth2GithubQueried :: YesodAuth m
|
oauth2GithubQueried :: YesodAuth m
|
||||||
=> Text -- ^ Client ID
|
=> Text -- ^ Client ID
|
||||||
-> Text -- ^ Client Secret
|
-> Text -- ^ Client Secret
|
||||||
@ -118,26 +128,77 @@ oauth2GithubQueried clientId clientSecret scopes credsQuery =
|
|||||||
|
|
||||||
dispatch method ps = apDispatch basicPlugin method ps
|
dispatch method ps = apDispatch basicPlugin method ps
|
||||||
|
|
||||||
|
-- the queries
|
||||||
|
|
||||||
fetchGithubProfile :: Manager -> AccessToken -> IO (Creds m)
|
fetchGithubProfile :: Manager -> AccessToken -> IO (Creds m)
|
||||||
fetchGithubProfile manager token = do
|
fetchGithubProfile manager token = do
|
||||||
userResult <- authGetJSON manager token "https://api.github.com/user"
|
(_, creds) <- queryJSONWithToken "user" (uncurry toGithubCreds) manager token
|
||||||
mailResult <- authGetJSON manager token "https://api.github.com/user/emails"
|
(_, sessionMap) <- queryJSON "user/emails" toGithubCredsExtraEmail manager token
|
||||||
|
return $ addSessionMap creds [sessionMap]
|
||||||
|
|
||||||
case (userResult, mailResult) of
|
fetchGithubProfileCheckOrg :: Text -> Manager -> AccessToken -> IO (Creds m)
|
||||||
(Right _, Right []) -> throwIO $ InvalidProfileResponse "github" "no mail address for user"
|
fetchGithubProfileCheckOrg githubOrg manager token = do
|
||||||
(Right user, Right mails) -> return $ toCreds user mails token
|
((_token, githubUser), creds) <- queryJSONWithToken "user" (uncurry toGithubCreds) manager token
|
||||||
(Left err, _) -> throwIO $ InvalidProfileResponse "github" err
|
(_githubUserEmail, sessionMap) <- queryJSON "user/emails" toGithubCredsExtraEmail manager token
|
||||||
(_, Left err) -> throwIO $ InvalidProfileResponse "github" err
|
queryWoResult ("orgs/" `mappend` githubOrg `mappend` "/members/" `mappend` githubUserLogin githubUser) manager token
|
||||||
|
return $ addSessionMap creds [sessionMap]
|
||||||
|
|
||||||
toCreds :: GithubUser -> [GithubUserEmail] -> AccessToken -> Creds m
|
-- Functions From results of query to Creds or Session map
|
||||||
toCreds user userMail token = Creds "github"
|
|
||||||
(T.pack $ show $ githubUserId user)
|
toGithubCreds :: AccessToken -> GithubUser -> Creds m
|
||||||
cExtra
|
toGithubCreds token user = Creds "github"
|
||||||
where
|
(T.pack $ show $ githubUserId user) $
|
||||||
cExtra = [ ("email", githubUserEmail $ head userMail)
|
[ ("login", githubUserLogin user)
|
||||||
, ("login", githubUserLogin user)
|
, ("avatar_url", githubUserAvatarUrl user)
|
||||||
, ("avatar_url", githubUserAvatarUrl user)
|
, ("access_token", decodeUtf8 $ accessToken token)
|
||||||
, ("access_token", decodeUtf8 $ accessToken token)
|
] ++ maybe [] (\name -> [("name", name)]) (githubUserName user)
|
||||||
] ++ (maybeName $ githubUserName user)
|
|
||||||
maybeName Nothing = []
|
toGithubCredsExtraEmail :: [GithubUserEmail] -> M.Map Text Text
|
||||||
maybeName (Just name) = [("name", name)]
|
toGithubCredsExtraEmail [] = throw $ InvalidProfileResponse "github" "no mail address for user"
|
||||||
|
toGithubCredsExtraEmail (mail:_) = M.singleton "email" $ githubUserEmail mail
|
||||||
|
|
||||||
|
-- utilities to create a query
|
||||||
|
|
||||||
|
githubApiEndPoint :: Text
|
||||||
|
githubApiEndPoint = "https://api.github.com"
|
||||||
|
|
||||||
|
queryWoResult :: Text -> Manager -> AccessToken -> IO()
|
||||||
|
queryWoResult url manager token = do
|
||||||
|
_ <- query url (const ()) githubAuthGetBS manager token
|
||||||
|
return ()
|
||||||
|
|
||||||
|
queryJSON :: FromJSON b => Text -> (b -> c) -> Manager -> AccessToken -> IO (b, c)
|
||||||
|
queryJSON url toResult = query url toResult githubAuthGetJson
|
||||||
|
|
||||||
|
queryJSONWithToken :: FromJSON b => Text -> ((AccessToken, b) -> c) -> Manager -> AccessToken -> IO ((AccessToken, b), c)
|
||||||
|
queryJSONWithToken url toResult manager token =
|
||||||
|
let githubAuthGetJsonWithToken argUrl argManager argToken = do
|
||||||
|
result <- githubAuthGetJson argUrl argManager argToken
|
||||||
|
return $ fmap (\res -> (argToken, res)) result
|
||||||
|
in
|
||||||
|
query url toResult githubAuthGetJsonWithToken manager token
|
||||||
|
|
||||||
|
githubAuthGetJson :: FromJSON b => Text -> Manager -> AccessToken -> IO (OAuth2Result b)
|
||||||
|
githubAuthGetJson url manager token =
|
||||||
|
authGetJSON manager token (encodeUtf8 url)
|
||||||
|
|
||||||
|
githubAuthGetBS :: Text -> Manager -> AccessToken -> IO (OAuth2Result ())
|
||||||
|
githubAuthGetBS url manager token = do
|
||||||
|
result <- authGetBS manager token (encodeUtf8 url)
|
||||||
|
return $ void result
|
||||||
|
|
||||||
|
query :: FromJSON b
|
||||||
|
=> Text
|
||||||
|
-> (b -> c)
|
||||||
|
-> (Text -> Manager -> AccessToken -> IO (OAuth2Result b))
|
||||||
|
-> Manager -> AccessToken -> IO (b, c)
|
||||||
|
query url toResult fQuery manager token = do
|
||||||
|
jsonResult <- fQuery (githubApiEndPoint `mappend` "/" `mappend` url) manager token
|
||||||
|
either
|
||||||
|
(throwIO . InvalidProfileResponse "github")
|
||||||
|
(\b -> return (b, toResult b))
|
||||||
|
jsonResult
|
||||||
|
|
||||||
|
addSessionMap :: Creds m -> [M.Map Text Text] -> Creds m
|
||||||
|
addSessionMap creds credsExtraMap =
|
||||||
|
creds { credsExtra = M.toList $ M.unions credsExtraMap }
|
||||||
|
|||||||
@ -43,6 +43,7 @@ library
|
|||||||
, hoauth2 >= 0.4.1 && < 0.5
|
, hoauth2 >= 0.4.1 && < 0.5
|
||||||
, lifted-base >= 0.2 && < 0.4
|
, lifted-base >= 0.2 && < 0.4
|
||||||
, uuid >= 1.3 && < 1.4
|
, uuid >= 1.3 && < 1.4
|
||||||
|
, containers >= 0.5.5 && < 0.6
|
||||||
|
|
||||||
exposed-modules: Yesod.Auth.OAuth2
|
exposed-modules: Yesod.Auth.OAuth2
|
||||||
Yesod.Auth.OAuth2.Google
|
Yesod.Auth.OAuth2.Google
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user