From 074b0c68e73a8441e60053b1c726590d6ac5f2a9 Mon Sep 17 00:00:00 2001 From: Kazuki Okamoto Date: Mon, 29 Feb 2016 04:07:42 +0900 Subject: [PATCH] add twitterId function --- yesod-auth-oauth/Yesod/Auth/OAuth.hs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/yesod-auth-oauth/Yesod/Auth/OAuth.hs b/yesod-auth-oauth/Yesod/Auth/OAuth.hs index 79ab12ac..7a6c2268 100644 --- a/yesod-auth-oauth/Yesod/Auth/OAuth.hs +++ b/yesod-auth-oauth/Yesod/Auth/OAuth.hs @@ -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