Skip to content

Add oauth1 support #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 22, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Network/Wreq.hs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ module Network.Wreq
, AWSAuthVersion(..)
, Lens.auth
, basicAuth
, oauth1Auth
, oauth2Bearer
, oauth2Token
, awsAuth
Expand Down Expand Up @@ -460,6 +461,16 @@ basicAuth :: S.ByteString -- ^ Username.
-> Auth
basicAuth = BasicAuth

-- | OAuth1 authentication. This consists of a consumer token,
-- a consumer secret, a token and a token secret
oauth1Auth :: S.ByteString -- ^ Consumer token
-> S.ByteString -- ^ Consumer secret
-> S.ByteString -- ^ OAuth token
-> S.ByteString -- ^ OAuth token secret
-> Auth
oauth1Auth = OAuth1


-- | An OAuth2 bearer token. This is treated by many services as the
-- equivalent of a username and password.
--
Expand Down
4 changes: 4 additions & 0 deletions Network/Wreq/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import qualified Network.HTTP.Client as HTTP
import qualified Network.HTTP.Types as HTTP
import qualified Network.Wreq.Internal.Lens as Lens
import qualified Network.Wreq.Internal.AWS as AWS (signRequest)
import qualified Network.Wreq.Internal.OAuth1 as OAuth1 (signRequest)
import qualified Network.Wreq.Lens as Lens hiding (checkStatus)

-- This mess allows this module to continue to load during interactive
Expand Down Expand Up @@ -117,8 +118,10 @@ prepare modify opts url = do
signRequest = maybe return f $ auth opts
where
f (AWSAuth versn key secret) = AWS.signRequest versn key secret
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[bos wrote] While you're at it, please also fix that API mistake for the AWS code in a separate commit

We're not passing the auth record to AWS.signRequest, but only the constituent parts. What could I do better here?

f oauth1Credentials@(OAuth1 _ _ _ _) = OAuth1.signRequest oauth1Credentials
f _ = return


setQuery :: Options -> Request -> Request
setQuery opts =
case params opts of
Expand All @@ -136,6 +139,7 @@ setAuth = maybe id f . auth
f (OAuth2Token token) = setHeader "Authorization" ("token " <> token)
-- for AWS request signature, see Internal/AWS
f (AWSAuth _ _ _) = id
f (OAuth1 _ _ _ _) = id

setProxy :: Options -> Request -> Request
setProxy = maybe id f . proxy
Expand Down
17 changes: 17 additions & 0 deletions Network/Wreq/Internal/OAuth1.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Network.Wreq.Internal.OAuth1
(
signRequest
) where

import Network.HTTP.Client (Request(..))
import Network.Wreq.Internal.Types (Auth(..))
import Web.Authenticate.OAuth ( signOAuth, newOAuth, oauthConsumerKey
, oauthConsumerSecret, newCredential)

signRequest :: Auth -> Request -> IO Request
signRequest (OAuth1 consumerToken consumerSecret token tokenSecret) requestToSign = signOAuth app creds requestToSign
where
app = newOAuth { oauthConsumerKey = consumerToken, oauthConsumerSecret = consumerSecret }
creds = newCredential token tokenSecret

signRequest _ requestToSign = return requestToSign
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the use of a wildcard pattern here. This suggests it needs a comment.

Actually, wait, this is just ugly.. You're already pattern-matching on the OAuth1 constructor in Network.Wreq.Internal and then passing it through here. That's quite the wrong thing to do, as the API suggests that signRequest will do something meaningful with an Auth when in fact it will not.

Instead, you should unpack the constructor and pass its consumerToken consumerSecret token tokenSecret parameters through to here individually.

While you're at it, please also fix that API mistake for the AWS code in a separate commit :-)

1 change: 1 addition & 0 deletions Network/Wreq/Internal/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ data Auth = BasicAuth S.ByteString S.ByteString
| AWSAuth AWSAuthVersion S.ByteString S.ByteString
-- ^ Amazon Web Services request signing
-- AWSAuthVersion key secret
| OAuth1 S.ByteString S.ByteString S.ByteString S.ByteString
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document this field.

deriving (Eq, Show, Typeable)

data AWSAuthVersion = AWSv4
Expand Down
2 changes: 2 additions & 0 deletions wreq.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ library
other-modules:
Network.Wreq.Internal
Network.Wreq.Internal.AWS
Network.Wreq.Internal.OAuth1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The module names in this list are sorted. Please add this in the correct lexicographical position.

Network.Wreq.Internal.Lens
Network.Wreq.Internal.Link
Network.Wreq.Internal.Types
Expand All @@ -90,6 +91,7 @@ library
PSQueue >= 1.1,
aeson >= 0.7.0.3,
attoparsec >= 0.11.1.0,
authenticate-oauth == 1.5.*,
base >= 4.5 && < 5,
base16-bytestring,
byteable,
Expand Down