diff --git a/Yesod/Auth/OAuth2.hs b/Yesod/Auth/OAuth2.hs index 1da5515..9ff4677 100644 --- a/Yesod/Auth/OAuth2.hs +++ b/Yesod/Auth/OAuth2.hs @@ -6,6 +6,8 @@ import Data.ByteString (ByteString) import Data.Text (Text) import Data.Text.Encoding (decodeUtf8With, encodeUtf8) import Data.Text.Encoding.Error (lenientDecode) +import Data.Maybe +import Network.HTTP.Conduit as C import Yesod.Auth import Yesod.Form import Yesod.Core @@ -35,15 +37,19 @@ authOAuth2 name oauth getCreds = AuthPlugin name dispatch login let oaUrl = render $ tm $ oauth2Url name [whamlet| Login via #{name} |] -oauth2Goodle clientId clientSecret = newOAuth2 { oauthClientId = encodeUtf8 clientId +oauth2Google clientId clientSecret = newOAuth2 { oauthClientId = encodeUtf8 clientId , oauthClientSecret = encodeUtf8 clientSecret , oauthOAuthorizeEndpoint = "https://accounts.google.com/o/oauth2/auth" , oauthAccessTokenEndpoint = "https://accounts.google.com/o/oauth2/token" } -oauth2Cloudsdale clientId clientSecret = newOAuth2 { oauthClientId = encodeUtf8 clientId - , oauthClientSecret = encodeUtf8 clientSecret - , oauthOAuthorizeEndpoint = "http://www.cloudsdale.org/oauth/authorize" - , oauthAccessTokenEndpoint = "http://www.cloudsdale.org/oauth/token" } +cloudsdaleAuth clientId clientSecret = authOAuth2 "cloudsdale" oauth2 $ \token -> do + rsp <- request $ authorizeRequest token $ fromJust $ parseUrl "http://api.cloudsdale.org/v2/me.json" + undefined + where + oauth2 = newOAuth2 { oauthClientId = encodeUtf8 clientId + , oauthClientSecret = encodeUtf8 clientSecret + , oauthOAuthorizeEndpoint = "http://www.cloudsdale.org/oauth/authorize" + , oauthAccessTokenEndpoint = "http://www.cloudsdale.org/oauth/token" } bsToText :: ByteString -> Text bsToText = decodeUtf8With lenientDecode diff --git a/Yesod/Auth/OAuth2/Internal.hs b/Yesod/Auth/OAuth2/Internal.hs index f86f4fe..d38cd36 100644 --- a/Yesod/Auth/OAuth2/Internal.hs +++ b/Yesod/Auth/OAuth2/Internal.hs @@ -103,3 +103,8 @@ signRequest oa req = req { queryString = (renderSimpleQuery False newQuery) } Just at -> insert ("oauth_token", at) oldQuery _ -> insert ("client_id", oauthClientId oa) . insert ("client_secret", oauthClientSecret oa) $ oldQuery oldQuery = parseSimpleQuery (queryString req) + +authorizeRequest :: AccessToken -> Request m -> Request m +authorizeRequest (AccessToken token) req = req { requestHeaders = auth : requestHeaders req } + where + auth = ("Authorization", BS.concat ["Bearer ", token])