add twitterId function

This commit is contained in:
Kazuki Okamoto 2016-02-29 04:07:42 +09:00
parent d46d754555
commit 074b0c68e7

View File

@ -5,6 +5,7 @@ module Yesod.Auth.OAuth
, oauthUrl
, authTwitter
, twitterUrl
, twitterId
, authTumblr
, tumblrUrl
, module Web.Authenticate.OAuth
@ -108,6 +109,25 @@ authTwitter key secret = authOAuth
twitterUrl :: AuthRoute
twitterUrl = oauthUrl "twitter"
-- | Gets Twitter ID (/user_id/) from @Creds@.
--
-- Never use @credsIdent@ for Twitter OAuth. @credsIdent@ returns /screen_name/, which shouldn't be used for authentication.
-- /screen_name/ is text like /foo/ of /\@foo/ which is unique but __mutable__. So /screen_name/ cannot authenticate users.
-- /user_id/ is integer which is unique and __immutable__. So you should use this for authentication.
--
-- Because of compatibility, @credsIdent@ returns /screen_name/ yet.
--
-- Since 1.4.x.x
twitterId :: Yesod m => Creds m -> Text
twitterId creds =
let
key = "user_id"
extra = credsExtra creds
in
case lookup key extra of
Just uId -> uId
Nothing -> throw $ CredentialError ("key not found: " ++ (T.unpack key)) (Credential $ map (encodeUtf8 *** encodeUtf8) extra)
authTumblr :: YesodAuth m
=> ByteString -- ^ Consumer Key
-> ByteString -- ^ Consumer Secret