From e33b3a8efbfc844d48997c6dbe171262b24dc4ee Mon Sep 17 00:00:00 2001 From: Hiromi Ishii Date: Sun, 1 Jan 2012 15:04:19 +0900 Subject: [PATCH] added newOAuth and newCredential --- Web/Authenticate/OAuth.hs | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/Web/Authenticate/OAuth.hs b/Web/Authenticate/OAuth.hs index e844d5f5..a43c01d9 100644 --- a/Web/Authenticate/OAuth.hs +++ b/Web/Authenticate/OAuth.hs @@ -2,9 +2,9 @@ {-# OPTIONS_GHC -Wall -fno-warn-orphans #-} module Web.Authenticate.OAuth ( -- * Data types - OAuth(..), SignMethod(..), Credential(..), OAuthException(..), + OAuth(..), newOAuth, SignMethod(..), Credential(..), OAuthException(..), -- * Operations for credentials - emptyCredential, insert, delete, inserts, + newCredential, emptyCredential, insert, delete, inserts, -- * Signature signOAuth, genSign, -- * Url & operation for authentication @@ -43,17 +43,25 @@ import Data.Conduit.Blaze (builderToByteString) import Blaze.ByteString.Builder (Builder) -- | Data type for OAuth client (consumer). -data OAuth = OAuth { oauthServerName :: String -- ^ Service name - , oauthRequestUri :: String -- ^ URI to request temporary credential - , oauthAccessTokenUri :: String -- ^ Uri to obtain access token - , oauthAuthorizeUri :: String -- ^ Uri to authorize - , oauthSignatureMethod :: SignMethod -- ^ Signature Method - , oauthConsumerKey :: BS.ByteString -- ^ Consumer key - , oauthConsumerSecret :: BS.ByteString -- ^ Consumer Secret - , oauthCallback :: Maybe BS.ByteString -- ^ Callback uri to redirect after authentication - , oauthRealm :: Maybe BS.ByteString -- ^ Optional authorization realm +-- The default values apply when you use 'newOAuth' +data OAuth = OAuth { oauthServerName :: String -- ^ Service name (You MUST specify) + , oauthRequestUri :: String -- ^ URI to request temporary credential (You MUST specify) + , oauthAccessTokenUri :: String -- ^ Uri to obtain access token (You MUST specify) + , oauthAuthorizeUri :: String -- ^ Uri to authorize (You MUST specify) + , oauthSignatureMethod :: SignMethod -- ^ Signature Method (default: 'HMACSHA1') + , oauthConsumerKey :: BS.ByteString -- ^ Consumer key (You MUST specify) + , oauthConsumerSecret :: BS.ByteString -- ^ Consumer Secret (You MUST specify) + , oauthCallback :: Maybe BS.ByteString -- ^ Callback uri to redirect after authentication (default: 'Nothing') + , oauthRealm :: Maybe BS.ByteString -- ^ Optional authorization realm (default: 'Nothing') } deriving (Show, Eq, Ord, Read, Data, Typeable) +-- | Default value for OAuth datatype. +-- You must specify at least oauthServerName, URIs and Tokens. +newOAuth :: OAuth +newOAuth = OAuth { oauthSignatureMethod = HMACSHA1 + , oauthCallback = Nothing + , oauthRealm = Nothing + } -- | Data type for signature method. data SignMethod = PLAINTEXT @@ -75,6 +83,12 @@ data Credential = Credential { unCredential :: [(BS.ByteString, BS.ByteString)] emptyCredential :: Credential emptyCredential = Credential [] +-- | Convenient function to create 'Credential' with OAuth Token and Token Secret. +newCredential :: BS.ByteString -- ^ value for oauth_token + -> BS.ByteString -- ^ value for oauth_token_secret + -> Credential +newCredential tok sec = Credential [("oauth_token", tok), ("oauth_token_secret", sec)] + token, tokenSecret :: Credential -> BS.ByteString token = fromMaybe "" . lookup "oauth_token" . unCredential tokenSecret = fromMaybe "" . lookup "oauth_token_secret" . unCredential