Skip to content

Commit

Permalink
Dedeprate all *Internal method with *WithAuthMethod alternative
Browse files Browse the repository at this point in the history
  • Loading branch information
freizl committed Sep 14, 2022
1 parent b1bbe61 commit 07b31bb
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 42 deletions.
1 change: 1 addition & 0 deletions hoauth2-example/hoauth2-example.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,4 @@ executable demo-server

ghc-options:
-Wall -Wtabs -Wno-unused-do-bind -Wunused-packages -Wpartial-fields
-Wwarnings-deprecations
4 changes: 2 additions & 2 deletions hoauth2-example/src/IDP/Facebook.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ facebookIdp =
def
{ idpName = Facebook,
oauth2Config = facebookKey,
oauth2FetchAccessToken = fetchAccessTokenInternal ClientSecretPost,
oauth2RefreshAccessToken = refreshAccessTokenInternal ClientSecretPost,
oauth2FetchAccessToken = fetchAccessTokenWithAuthMethod ClientSecretPost,
oauth2RefreshAccessToken = refreshAccessTokenWithAuthMethod ClientSecretPost,
convertUserInfoToLoginUser = toLoginUser,
oauth2UserInfoUri = [uri|https://graph.facebook.com/me?fields=id,name,email|]
}
Expand Down
6 changes: 3 additions & 3 deletions hoauth2-example/src/IDP/StackExchange.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ stackexchangeIdp =
def
{ idpName = StackExchange,
oauth2Config = stackexchangeKey,
oauth2FetchAccessToken = fetchAccessTokenInternal ClientSecretPost,
oauth2RefreshAccessToken = refreshAccessTokenInternal ClientSecretPost,
oauth2FetchAccessToken = fetchAccessTokenWithAuthMethod ClientSecretPost,
oauth2RefreshAccessToken = refreshAccessTokenWithAuthMethod ClientSecretPost,
convertUserInfoToLoginUser = toLoginUser,
oauth2FetchUserInfo = fetchUserInfo,
--
Expand All @@ -67,7 +67,7 @@ fetchUserInfo ::
IO
b
fetchUserInfo IDP {..} mgr accessToken =
authGetJSONInternal
authGetJSONWithAuthMethod
(Set.fromList [AuthInRequestQuery])
mgr
accessToken
Expand Down
2 changes: 1 addition & 1 deletion hoauth2-example/src/IDP/Weibo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fetchUserInfo ::
IO
b
fetchUserInfo IDP {..} mgr accessToken =
authGetJSONInternal
authGetJSONWithAuthMethod
(Set.fromList [AuthInRequestQuery])
mgr
accessToken
Expand Down
4 changes: 2 additions & 2 deletions hoauth2-example/src/IDP/ZOHO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ zohoIdp =
oauth2Config = zohoKey,
oauth2AuthorizeParams = [("access_type", "offline"), ("prompt", "consent")],
convertUserInfoToLoginUser = toLoginUser,
oauth2FetchAccessToken = fetchAccessTokenInternal ClientSecretPost,
oauth2RefreshAccessToken = refreshAccessTokenInternal ClientSecretPost,
oauth2FetchAccessToken = fetchAccessTokenWithAuthMethod ClientSecretPost,
oauth2RefreshAccessToken = refreshAccessTokenWithAuthMethod ClientSecretPost,
oauth2UserInfoUri = [uri|https://www.zohoapis.com/crm/v2/users|]
}

Expand Down
4 changes: 2 additions & 2 deletions hoauth2-example/src/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ getIdpName :: Show (IDPName a) => IDP a -> TL.Text
getIdpName = TL.pack . show . idpName

fetchUserInfoViaGet :: FromJSON (IDPUserInfo a) => IDP a -> Manager -> AccessToken -> ExceptT BSL.ByteString IO (IDPUserInfo a)
fetchUserInfoViaGet i2 mgr at = authGetJSONInternal (Set.fromList [AuthInRequestHeader] ) mgr at (oauth2UserInfoUri i2)
fetchUserInfoViaGet i2 mgr at = authGetJSONWithAuthMethod (Set.fromList [AuthInRequestHeader] ) mgr at (oauth2UserInfoUri i2)

fetchUserInfoViaPost :: FromJSON (IDPUserInfo a) => IDP a -> Manager -> AccessToken -> ExceptT BSL.ByteString IO (IDPUserInfo a)
fetchUserInfoViaPost i2 mgr at = authPostJSONInternal (Set.fromList [AuthInRequestHeader] ) mgr at (oauth2UserInfoUri i2) []
fetchUserInfoViaPost i2 mgr at = authPostJSONWithAuthMethod (Set.fromList [AuthInRequestHeader] ) mgr at (oauth2UserInfoUri i2) []

createAuthorizeUri :: (Show (IDPName a)) => IDP a -> TL.Text
createAuthorizeUri idp@IDP {..} = createCodeUri oauth2Config $ defaultAuthorizeParam idp ++ oauth2AuthorizeParams
Expand Down
1 change: 1 addition & 0 deletions hoauth2/hoauth2.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ library

ghc-options:
-Wall -Wtabs -Wno-unused-do-bind -Wunused-packages -Wpartial-fields
-Wwarnings-deprecations
99 changes: 75 additions & 24 deletions hoauth2/src/Network/OAuth/OAuth2/HttpClient.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ module Network.OAuth.OAuth2.HttpClient
authGetJSON,
authGetBS,
authGetBS2,
authGetJSONWithAuthMethod,
authGetJSONInternal,
authGetBSWithAuthMethod,
authGetBSInternal,
authPostJSON,
authPostBS,
authPostBS1,
authPostBS2,
authPostBS3,
authPostJSONWithAuthMethod,
authPostJSONInternal,
authPostBSWithAuthMethod,
authPostBSInternal,
)
where
Expand Down Expand Up @@ -52,12 +56,23 @@ authGetJSON ::
URI ->
-- | Response as JSON
ExceptT BSL.ByteString IO b
authGetJSON = authGetJSONInternal (Set.fromList [AuthInRequestHeader])
{-# DEPRECATED authGetJSON "use authGetJSONInternal" #-}
authGetJSON = authGetJSONWithAuthMethod (Set.fromList [AuthInRequestHeader])

authGetJSONInternal ::
(FromJSON b) =>
Set.Set APIAuthenticationMethod ->
-- | HTTP connection manager.
Manager ->
AccessToken ->
URI ->
-- | Response as JSON
ExceptT BSL.ByteString IO b
authGetJSONInternal = authGetJSONWithAuthMethod
{-# DEPRECATED authGetJSONInternal "use authGetJSONWithAuthMethod" #-}

-- | Conduct an authorized GET request and return response as JSON.
-- Allow to specify how to append AccessToken.
authGetJSONInternal ::
authGetJSONWithAuthMethod ::
(FromJSON b) =>
Set.Set APIAuthenticationMethod ->
-- | HTTP connection manager.
Expand All @@ -66,8 +81,8 @@ authGetJSONInternal ::
URI ->
-- | Response as JSON
ExceptT BSL.ByteString IO b
authGetJSONInternal authTypes manager t uri = do
resp <- authGetBSInternal authTypes manager t uri
authGetJSONWithAuthMethod authTypes manager t uri = do
resp <- authGetBSWithAuthMethod authTypes manager t uri
either (throwE . BSL.pack) return (eitherDecode resp)

-- | Conduct an authorized GET request.
Expand All @@ -79,7 +94,7 @@ authGetBS ::
URI ->
-- | Response as ByteString
ExceptT BSL.ByteString IO BSL.ByteString
authGetBS = authGetBSInternal $ Set.fromList [AuthInRequestHeader]
authGetBS = authGetBSWithAuthMethod $ Set.fromList [AuthInRequestHeader]

-- | Same to 'authGetBS' but set access token to query parameter rather than header
authGetBS2 ::
Expand All @@ -89,12 +104,24 @@ authGetBS2 ::
URI ->
-- | Response as ByteString
ExceptT BSL.ByteString IO BSL.ByteString
authGetBS2 = authGetBSInternal $ Set.fromList [AuthInRequestQuery]
{-# DEPRECATED authGetBS2 "use authGetBSInternal" #-}
authGetBS2 = authGetBSWithAuthMethod $ Set.fromList [AuthInRequestQuery]
{-# DEPRECATED authGetBS2 "use authGetBSWithAuthMethod" #-}

authGetBSInternal ::
-- |
Set.Set APIAuthenticationMethod ->
-- | HTTP connection manager.
Manager ->
AccessToken ->
URI ->
-- | Response as ByteString
ExceptT BSL.ByteString IO BSL.ByteString
authGetBSInternal = authGetBSWithAuthMethod
{-# DEPRECATED authGetBSInternal "use authGetBSWithAuthMethod" #-}

-- | Conduct an authorized GET request and return response as ByteString.
-- Allow to specify how to append AccessToken.
authGetBSInternal ::
authGetBSWithAuthMethod ::
-- |
Set.Set APIAuthenticationMethod ->
-- | HTTP connection manager.
Expand All @@ -103,7 +130,7 @@ authGetBSInternal ::
URI ->
-- | Response as ByteString
ExceptT BSL.ByteString IO BSL.ByteString
authGetBSInternal authTypes manager token url = do
authGetBSWithAuthMethod authTypes manager token url = do
let appendToUrl = AuthInRequestQuery `Set.member` authTypes
let appendToHeader = AuthInRequestHeader `Set.member` authTypes
let uri = if appendToUrl then url `appendAccessToken` token else url
Expand All @@ -122,12 +149,25 @@ authPostJSON ::
PostBody ->
-- | Response as JSON
ExceptT BSL.ByteString IO b
authPostJSON = authPostJSONInternal $ Set.fromList [AuthInRequestHeader]
{-# DEPRECATED authPostJSON "use authPostJSONInternal" #-}
authPostJSON = authPostJSONWithAuthMethod $ Set.fromList [AuthInRequestHeader]
{-# DEPRECATED authPostJSON "use authPostJSONWithAuthMethod" #-}

authPostJSONInternal ::
FromJSON a =>
Set.Set APIAuthenticationMethod ->
-- | HTTP connection manager.
Manager ->
AccessToken ->
URI ->
PostBody ->
-- | Response as ByteString
ExceptT BSL.ByteString IO a
authPostJSONInternal = authPostJSONWithAuthMethod
{-# DEPRECATED authPostJSONInternal "use authPostJSONWithAuthMethod" #-}

-- | Conduct POST request and return response as JSON.
-- Allow to specify how to append AccessToken.
authPostJSONInternal ::
authPostJSONWithAuthMethod ::
FromJSON a =>
Set.Set APIAuthenticationMethod ->
-- | HTTP connection manager.
Expand All @@ -137,8 +177,8 @@ authPostJSONInternal ::
PostBody ->
-- | Response as ByteString
ExceptT BSL.ByteString IO a
authPostJSONInternal authTypes manager token url body = do
resp <- authPostBSInternal authTypes manager token url body
authPostJSONWithAuthMethod authTypes manager token url body = do
resp <- authPostBSWithAuthMethod authTypes manager token url body
either (throwE . BSL.pack) return (eitherDecode resp)

-- | Conduct POST request.
Expand All @@ -151,7 +191,7 @@ authPostBS ::
PostBody ->
-- | Response as ByteString
ExceptT BSL.ByteString IO BSL.ByteString
authPostBS = authPostBSInternal $ Set.fromList [AuthInRequestHeader]
authPostBS = authPostBSWithAuthMethod $ Set.fromList [AuthInRequestHeader]

-- | Conduct POST request.
-- Inject Access Token to both http header (Authorization) and request body.
Expand All @@ -163,8 +203,8 @@ authPostBS1 ::
PostBody ->
-- | Response as ByteString
ExceptT BSL.ByteString IO BSL.ByteString
authPostBS1 = authPostBSInternal $ Set.fromList [AuthInRequestBody, AuthInRequestHeader]
{-# DEPRECATED authPostBS1 "use authPostBSInternal" #-}
authPostBS1 = authPostBSWithAuthMethod $ Set.fromList [AuthInRequestBody, AuthInRequestHeader]
{-# DEPRECATED authPostBS1 "use authPostBSWithAuthMethod" #-}

-- | Conduct POST request with access token only in the request body but header.
authPostBS2 ::
Expand All @@ -175,8 +215,8 @@ authPostBS2 ::
PostBody ->
-- | Response as ByteString
ExceptT BSL.ByteString IO BSL.ByteString
authPostBS2 = authPostBSInternal $ Set.fromList [AuthInRequestBody]
{-# DEPRECATED authPostBS2 "use authPostBSInternal" #-}
authPostBS2 = authPostBSWithAuthMethod $ Set.fromList [AuthInRequestBody]
{-# DEPRECATED authPostBS2 "use authPostBSWithAuthMethod" #-}

-- | Conduct POST request with access token only in the header and not in body
authPostBS3 ::
Expand All @@ -187,12 +227,23 @@ authPostBS3 ::
PostBody ->
-- | Response as ByteString
ExceptT BSL.ByteString IO BSL.ByteString
authPostBS3 = authPostBSInternal $ Set.fromList [AuthInRequestHeader]
{-# DEPRECATED authPostBS3 "use authPostBSInternal" #-}
authPostBS3 = authPostBSWithAuthMethod $ Set.fromList [AuthInRequestHeader]
{-# DEPRECATED authPostBS3 "use authPostBSWithAuthMethod" #-}

authPostBSInternal ::
Set.Set APIAuthenticationMethod ->
-- | HTTP connection manager.
Manager ->
AccessToken ->
URI ->
PostBody ->
-- | Response as ByteString
ExceptT BSL.ByteString IO BSL.ByteString
authPostBSInternal = authPostBSWithAuthMethod

-- | Conduct POST request and return response as ByteString.
-- Allow to specify how to append AccessToken.
authPostBSInternal ::
authPostBSWithAuthMethod ::
Set.Set APIAuthenticationMethod ->
-- | HTTP connection manager.
Manager ->
Expand All @@ -201,7 +252,7 @@ authPostBSInternal ::
PostBody ->
-- | Response as ByteString
ExceptT BSL.ByteString IO BSL.ByteString
authPostBSInternal authTypes manager token url body = do
authPostBSWithAuthMethod authTypes manager token url body = do
let appendToBody = AuthInRequestBody `Set.member` authTypes
let appendToHeader = AuthInRequestHeader `Set.member` authTypes
let reqBody = if appendToBody then body ++ accessTokenToParam token else body
Expand Down
41 changes: 33 additions & 8 deletions hoauth2/src/Network/OAuth/OAuth2/TokenRequest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fetchAccessToken ::
ExchangeToken ->
-- | Access Token
ExceptT (OAuth2Error Errors) IO OAuth2Token
fetchAccessToken = fetchAccessTokenInternal ClientSecretBasic
fetchAccessToken = fetchAccessTokenWithAuthMethod ClientSecretBasic

fetchAccessToken2 ::
-- | HTTP connection manager
Expand All @@ -118,8 +118,8 @@ fetchAccessToken2 ::
ExchangeToken ->
-- | Access Token
ExceptT (OAuth2Error Errors) IO OAuth2Token
fetchAccessToken2 = fetchAccessTokenInternal ClientSecretPost
{-# DEPRECATED fetchAccessToken2 "renamed to fetchAccessTokenInternal" #-}
fetchAccessToken2 = fetchAccessTokenWithAuthMethod ClientSecretPost
{-# DEPRECATED fetchAccessToken2 "use fetchAccessTokenWithAuthMethod" #-}

fetchAccessTokenInternal ::
ClientAuthenticationMethod ->
Expand All @@ -131,7 +131,20 @@ fetchAccessTokenInternal ::
ExchangeToken ->
-- | Access Token
ExceptT (OAuth2Error Errors) IO OAuth2Token
fetchAccessTokenInternal authMethod manager oa code = do
fetchAccessTokenInternal = fetchAccessTokenWithAuthMethod
{-# DEPRECATED fetchAccessTokenInternal "use fetchAccessTokenWithAuthMethod" #-}

fetchAccessTokenWithAuthMethod ::
ClientAuthenticationMethod ->
-- | HTTP connection manager
Manager ->
-- | OAuth Data
OAuth2 ->
-- | OAuth 2 Tokens
ExchangeToken ->
-- | Access Token
ExceptT (OAuth2Error Errors) IO OAuth2Token
fetchAccessTokenWithAuthMethod authMethod manager oa code = do
let (uri, body) = accessTokenUrl oa code
let extraBody = if authMethod == ClientSecretPost then clientSecretPost oa else []
doJSONPostRequest manager oa uri (body ++ extraBody)
Expand All @@ -156,7 +169,7 @@ refreshAccessToken ::
-- | refresh token gained after authorization
RefreshToken ->
ExceptT (OAuth2Error Errors) IO OAuth2Token
refreshAccessToken = refreshAccessTokenInternal ClientSecretBasic
refreshAccessToken = refreshAccessTokenWithAuthMethod ClientSecretBasic

refreshAccessToken2 ::
-- | HTTP connection manager.
Expand All @@ -166,8 +179,8 @@ refreshAccessToken2 ::
-- | refresh token gained after authorization
RefreshToken ->
ExceptT (OAuth2Error Errors) IO OAuth2Token
refreshAccessToken2 = refreshAccessTokenInternal ClientSecretPost
{-# DEPRECATED refreshAccessToken2 "renamed to fetchAccessTokenInternal" #-}
refreshAccessToken2 = refreshAccessTokenWithAuthMethod ClientSecretPost
{-# DEPRECATED refreshAccessToken2 "use fetchAccessTokenWithAuthMethod" #-}

refreshAccessTokenInternal ::
ClientAuthenticationMethod ->
Expand All @@ -178,7 +191,19 @@ refreshAccessTokenInternal ::
-- | refresh token gained after authorization
RefreshToken ->
ExceptT (OAuth2Error Errors) IO OAuth2Token
refreshAccessTokenInternal authMethod manager oa token = do
refreshAccessTokenInternal = refreshAccessTokenWithAuthMethod
{-# DEPRECATED refreshAccessTokenInternal "use refreshAccessTokenWithAuthMethod" #-}

refreshAccessTokenWithAuthMethod ::
ClientAuthenticationMethod ->
-- | HTTP connection manager.
Manager ->
-- | OAuth context
OAuth2 ->
-- | refresh token gained after authorization
RefreshToken ->
ExceptT (OAuth2Error Errors) IO OAuth2Token
refreshAccessTokenWithAuthMethod authMethod manager oa token = do
let (uri, body) = refreshAccessTokenUrl oa token
let extraBody = if authMethod == ClientSecretPost then clientSecretPost oa else []
doJSONPostRequest manager oa uri (body ++ extraBody)
Expand Down

0 comments on commit 07b31bb

Please sign in to comment.