Add json request

This commit is contained in:
Tom Streller 2013-07-15 11:43:28 +02:00
parent d966f17704
commit 6d3bd0f281
2 changed files with 16 additions and 5 deletions

View File

@ -6,6 +6,8 @@ import Data.ByteString (ByteString)
import Data.Text (Text) import Data.Text (Text)
import Data.Text.Encoding (decodeUtf8With, encodeUtf8) import Data.Text.Encoding (decodeUtf8With, encodeUtf8)
import Data.Text.Encoding.Error (lenientDecode) import Data.Text.Encoding.Error (lenientDecode)
import Data.Maybe
import Network.HTTP.Conduit as C
import Yesod.Auth import Yesod.Auth
import Yesod.Form import Yesod.Form
import Yesod.Core import Yesod.Core
@ -35,15 +37,19 @@ authOAuth2 name oauth getCreds = AuthPlugin name dispatch login
let oaUrl = render $ tm $ oauth2Url name let oaUrl = render $ tm $ oauth2Url name
[whamlet| <a href=#{oaUrl}>Login via #{name} |] [whamlet| <a href=#{oaUrl}>Login via #{name} |]
oauth2Goodle clientId clientSecret = newOAuth2 { oauthClientId = encodeUtf8 clientId oauth2Google clientId clientSecret = newOAuth2 { oauthClientId = encodeUtf8 clientId
, oauthClientSecret = encodeUtf8 clientSecret , oauthClientSecret = encodeUtf8 clientSecret
, oauthOAuthorizeEndpoint = "https://accounts.google.com/o/oauth2/auth" , oauthOAuthorizeEndpoint = "https://accounts.google.com/o/oauth2/auth"
, oauthAccessTokenEndpoint = "https://accounts.google.com/o/oauth2/token" } , oauthAccessTokenEndpoint = "https://accounts.google.com/o/oauth2/token" }
oauth2Cloudsdale clientId clientSecret = newOAuth2 { oauthClientId = encodeUtf8 clientId cloudsdaleAuth clientId clientSecret = authOAuth2 "cloudsdale" oauth2 $ \token -> do
, oauthClientSecret = encodeUtf8 clientSecret rsp <- request $ authorizeRequest token $ fromJust $ parseUrl "http://api.cloudsdale.org/v2/me.json"
, oauthOAuthorizeEndpoint = "http://www.cloudsdale.org/oauth/authorize" undefined
, oauthAccessTokenEndpoint = "http://www.cloudsdale.org/oauth/token" } 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 :: ByteString -> Text
bsToText = decodeUtf8With lenientDecode bsToText = decodeUtf8With lenientDecode

View File

@ -103,3 +103,8 @@ signRequest oa req = req { queryString = (renderSimpleQuery False newQuery) }
Just at -> insert ("oauth_token", at) oldQuery Just at -> insert ("oauth_token", at) oldQuery
_ -> insert ("client_id", oauthClientId oa) . insert ("client_secret", oauthClientSecret oa) $ oldQuery _ -> insert ("client_id", oauthClientId oa) . insert ("client_secret", oauthClientSecret oa) $ oldQuery
oldQuery = parseSimpleQuery (queryString req) 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])