mirror of
https://github.com/freckle/yesod-auth-oauth2.git
synced 2026-09-03 16:33:49 +02:00
Add Twitter
This commit is contained in:
parent
2d3d1d2a8e
commit
5c5f94dba4
53
Yesod/Auth/OAuth2/Twitter.hs
Normal file
53
Yesod/Auth/OAuth2/Twitter.hs
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
{-# LANGUAGE CPP #-}
|
||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
module Yesod.Auth.OAuth2.Twitter
|
||||||
|
( oauth2Twitter
|
||||||
|
, module Yesod.Auth.OAuth2
|
||||||
|
) where
|
||||||
|
|
||||||
|
#if __GLASGOW_HASKELL__ < 710
|
||||||
|
import Control.Applicative ((<$>), (<*>))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
import Control.Monad (mzero)
|
||||||
|
import Data.Aeson
|
||||||
|
import Data.Text (Text)
|
||||||
|
import Data.Text.Encoding (encodeUtf8)
|
||||||
|
import Yesod.Auth
|
||||||
|
import Yesod.Auth.OAuth2
|
||||||
|
|
||||||
|
data TwitterUser = TwitterUser
|
||||||
|
{ twitterUserId :: Text
|
||||||
|
, twitterUserName :: Text
|
||||||
|
, twitterScreenName :: Text
|
||||||
|
}
|
||||||
|
|
||||||
|
instance FromJSON TwitterUser where
|
||||||
|
parseJSON (Object o) = TwitterUser
|
||||||
|
<$> o .: "id_str"
|
||||||
|
<*> o .: "name"
|
||||||
|
<*> o .: "screen_name"
|
||||||
|
|
||||||
|
parseJSON _ = mzero
|
||||||
|
|
||||||
|
oauth2Twitter :: YesodAuth m
|
||||||
|
=> Text -- ^ Client ID
|
||||||
|
-> Text -- ^ Client Secret
|
||||||
|
-> AuthPlugin m
|
||||||
|
oauth2Twitter clientId clientSecret = authOAuth2 "twitter"
|
||||||
|
OAuth2
|
||||||
|
{ oauthClientId = encodeUtf8 clientId
|
||||||
|
, oauthClientSecret = encodeUtf8 clientSecret
|
||||||
|
, oauthOAuthorizeEndpoint = "https://api.twitter.com/oauth/authorize"
|
||||||
|
, oauthAccessTokenEndpoint = "https://api.twitter.com/oauth/access_token"
|
||||||
|
, oauthCallback = Nothing
|
||||||
|
}
|
||||||
|
$ fromProfileURL "twitter" "https://api.twitter.com/1.1/account/verify_credentials.json"
|
||||||
|
$ \user -> Creds
|
||||||
|
{ credsPlugin = "twitter"
|
||||||
|
, credsIdent = twitterUserId user
|
||||||
|
, credsExtra =
|
||||||
|
[ ("name", twitterUserName user)
|
||||||
|
, ("screen_name", twitterScreenName user)
|
||||||
|
]
|
||||||
|
}
|
||||||
@ -40,6 +40,7 @@ library
|
|||||||
exposed-modules: Yesod.Auth.OAuth2
|
exposed-modules: Yesod.Auth.OAuth2
|
||||||
Yesod.Auth.OAuth2.Github
|
Yesod.Auth.OAuth2.Github
|
||||||
Yesod.Auth.OAuth2.Spotify
|
Yesod.Auth.OAuth2.Spotify
|
||||||
|
Yesod.Auth.OAuth2.Twitter
|
||||||
Yesod.Auth.OAuth2.Upcase
|
Yesod.Auth.OAuth2.Upcase
|
||||||
|
|
||||||
ghc-options: -Wall
|
ghc-options: -Wall
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user