Skip to content

Commit

Permalink
shuffle the order of type parameter to IdpApplication
Browse files Browse the repository at this point in the history
  • Loading branch information
freizl committed Jun 27, 2023
1 parent 6ceb101 commit eda7898
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 45 deletions.
18 changes: 9 additions & 9 deletions hoauth2-demo/src/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ testPasswordGrantTypeH (auth0, okta) = do
_ -> raise $ "unable to find password grant type flow for idp " <> i
where
testPasswordGrantType ::
( HasDemoLoginUser b
, FromJSON (IdpUserInfo b)
( HasDemoLoginUser i
, FromJSON (IdpUserInfo i)
) =>
IdpApplication ResourceOwnerPassword.Application b ->
IdpApplication i ResourceOwnerPassword.Application ->
ActionM ()
testPasswordGrantType idpApp = do
exceptToActionM $ do
Expand All @@ -157,7 +157,7 @@ testClientCredentialGrantTypeH (auth0, okta) = do
_ -> raise $ "unable to find password grant type flow for idp " <> i

testClientCredentialsGrantType ::
IdpApplication ClientCredentials.Application b ->
IdpApplication i ClientCredentials.Application ->
ActionM ()
testClientCredentialsGrantType testApp = do
exceptToActionM $ do
Expand Down Expand Up @@ -214,7 +214,7 @@ fetchTokenAndUser c exchangeToken idpData@(DemoAppEnv (DemoAuthorizationApp idpA
updateIdp c idpData luser token
where
tryFetchAccessToken ::
IdpApplication AuthorizationCode.Application i ->
IdpApplication i AuthorizationCode.Application ->
Manager ->
Text ->
ExceptT Text IO OAuth2Token
Expand All @@ -239,15 +239,15 @@ oauth2ErrorToText :: TokenRequestError -> Text
oauth2ErrorToText e = TL.pack $ "conduitTokenRequest - cannot fetch access token. error detail: " ++ show e

tryFetchUser ::
forall a b.
(HasDemoLoginUser b, HasUserInfoRequest a, FromJSON (IdpUserInfo b)) =>
forall i a.
(HasDemoLoginUser i, HasUserInfoRequest a, FromJSON (IdpUserInfo i)) =>
Manager ->
OAuth2Token ->
IdpApplication a b ->
IdpApplication i a ->
ExceptT Text IO DemoLoginUser
tryFetchUser mgr at idpAppConfig = do
user <- withExceptT bslToText $ conduitUserInfoRequest idpAppConfig mgr (accessToken at)
pure $ toLoginUser @b user
pure $ toLoginUser @i user

doRefreshToken :: DemoAppEnv -> ExceptT Text IO OAuth2Token
doRefreshToken (DemoAppEnv (DemoAuthorizationApp idpAppConfig) (DemoAppPerAppSessionData {..})) = do
Expand Down
12 changes: 6 additions & 6 deletions hoauth2-demo/src/Idp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ defaultOAuth2RedirectUri = [uri|http://localhost:9988/oauth2/callback|]
createAuthorizationApps :: (MonadIO m) => (Idp IAuth0.Auth0, Idp IOkta.Okta) -> ExceptT Text m [DemoAuthorizationApp]
createAuthorizationApps (myAuth0Idp, myOktaIdp) = do
configParams <- readEnvFile
let initIdpAppConfig :: Idp i -> AuthorizationCode.Application -> IdpApplication AuthorizationCode.Application i
let initIdpAppConfig :: Idp i -> AuthorizationCode.Application -> IdpApplication i AuthorizationCode.Application
initIdpAppConfig i idpAppConfig =
case Aeson.lookup (Aeson.fromString $ TL.unpack $ TL.toLower $ getIdpAppName idpAppConfig) configParams of
Nothing -> IdpApplication {idp = i, application = idpAppConfig}
Expand Down Expand Up @@ -94,7 +94,7 @@ createAuthorizationApps (myAuth0Idp, myOktaIdp) = do
, DemoAuthorizationApp (initIdpAppConfig IStackExchange.defaultStackExchangeIdp IStackExchange.defaultStackExchangeApp)
]

googleServiceAccountApp :: ExceptT Text IO (IdpApplication JwtBearer.Application IGoogle.Google)
googleServiceAccountApp :: ExceptT Text IO (IdpApplication IGoogle.Google JwtBearer.Application)
googleServiceAccountApp = do
IGoogle.GoogleServiceAccountKey {..} <- withExceptT TL.pack (ExceptT $ Aeson.eitherDecodeFileStrict ".google-sa.json")
pkey <- withExceptT TL.pack (ExceptT $ IGoogle.readPemRsaKey privateKey)
Expand All @@ -115,7 +115,7 @@ googleServiceAccountApp = do
)
pure $ IdpApplication {idp = IGoogle.defaultGoogleIdp, application = IGoogle.defaultServiceAccountApp jwt}

oktaPasswordGrantApp :: Idp IOkta.Okta -> IdpApplication ResourceOwnerPassword.Application IOkta.Okta
oktaPasswordGrantApp :: Idp IOkta.Okta -> IdpApplication IOkta.Okta ResourceOwnerPassword.Application
oktaPasswordGrantApp i =
IdpApplication
{ idp = i
Expand All @@ -137,7 +137,7 @@ oktaPasswordGrantApp i =
-- With Org AS, got this error
-- Client Credentials requests to the Org Authorization Server must use the private_key_jwt token_endpoint_auth_method
--
oktaClientCredentialsGrantApp :: Idp IOkta.Okta -> IO (IdpApplication ClientCredentials.Application IOkta.Okta)
oktaClientCredentialsGrantApp :: Idp IOkta.Okta -> IO (IdpApplication IOkta.Okta ClientCredentials.Application)
oktaClientCredentialsGrantApp i = do
let clientId = "0oa9mbklxn2Ac0oJ24x7"
keyJsonStr <- BS.readFile ".okta-key.json"
Expand All @@ -164,7 +164,7 @@ oktaClientCredentialsGrantApp i = do
Left e -> Prelude.error e

-- | https://auth0.com/docs/api/authentication#resource-owner-password
auth0PasswordGrantApp :: Idp IAuth0.Auth0 -> IdpApplication ResourceOwnerPassword.Application IAuth0.Auth0
auth0PasswordGrantApp :: Idp IAuth0.Auth0 -> IdpApplication IAuth0.Auth0 ResourceOwnerPassword.Application
auth0PasswordGrantApp i =
IdpApplication
{ idp = i
Expand All @@ -181,7 +181,7 @@ auth0PasswordGrantApp i =
}

-- | https://auth0.com/docs/api/authentication#client-credentials-flow
auth0ClientCredentialsGrantApp :: Idp IAuth0.Auth0 -> IdpApplication ClientCredentials.Application IAuth0.Auth0
auth0ClientCredentialsGrantApp :: Idp IAuth0.Auth0 -> IdpApplication IAuth0.Auth0 ClientCredentials.Application
auth0ClientCredentialsGrantApp i =
IdpApplication
{ idp = i
Expand Down
8 changes: 4 additions & 4 deletions hoauth2-demo/src/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ instance HasDemoLoginUser IStackExchange.StackExchange where
-- Heterogenous collections
-- https://wiki.haskell.org/Heterogenous_collections
data DemoAuthorizationApp
= forall a.
( HasDemoLoginUser a
, FromJSON (IdpUserInfo a)
= forall i.
( HasDemoLoginUser i
, FromJSON (IdpUserInfo i)
) =>
DemoAuthorizationApp (IdpApplication AuthorizationCode.Application a)
DemoAuthorizationApp (IdpApplication i AuthorizationCode.Application)

-------------------------------------------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions hoauth2-providers-tutorial/src/HOAuth2ProvidersTutorial.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import Prelude hiding (id)

------------------------------

testAuth0App :: IdpApplication AuthorizationCode.Application Auth0
testAuth0App :: IdpApplication Auth0 AuthorizationCode.Application
testAuth0App =
let application =
Auth0.defaultAuth0App
Expand All @@ -67,7 +67,7 @@ testAuth0Idp =
, idpTokenEndpoint = [uri|https://freizl.auth0.com/oauth/token|]
}

testGoogleApp :: IdpApplication AuthorizationCode.Application Google
testGoogleApp :: IdpApplication Google AuthorizationCode.Application
testGoogleApp =
let application =
Google.defaultGoogleApp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ instance ToQueryParam AuthorizationRequestParam where
class HasAuthorizeRequest a where
mkAuthorizeRequestParam :: a -> AuthorizationRequestParam

mkAuthorizeRequest :: (HasAuthorizeRequest a) => IdpApplication a i -> Text
mkAuthorizeRequest :: (HasAuthorizeRequest a) => IdpApplication i a -> Text
mkAuthorizeRequest idpApp =
let req = mkAuthorizeRequestParam (application idpApp)
allParams =
Expand All @@ -63,7 +63,7 @@ class (HasAuthorizeRequest a) => HasPkceAuthorizeRequest a where

mkPkceAuthorizeRequest ::
(HasPkceAuthorizeRequest a, MonadIO m) =>
IdpApplication a i ->
IdpApplication i a ->
m (Text, CodeVerifier)
mkPkceAuthorizeRequest IdpApplication {..} = do
(req, codeVerifier) <- mkPkceAuthorizeRequestParam application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class (HasOAuth2Key a, HasTokenRequestClientAuthenticationMethod a) => HasRefres

conduitRefreshTokenRequest ::
(HasRefreshTokenRequest a, ToQueryParam (RefreshTokenRequest a), MonadIO m) =>
IdpApplication a i ->
IdpApplication i a ->
Manager ->
OAuth2.RefreshToken ->
ExceptT TokenRequestError m OAuth2Token
Expand Down
4 changes: 2 additions & 2 deletions hoauth2/src/Network/OAuth2/Experiment/Flows/TokenRequest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class (HasOAuth2Key a, HasTokenRequestClientAuthenticationMethod a) => HasTokenR

conduitTokenRequest ::
(HasTokenRequest a, ToQueryParam (TokenRequest a), MonadIO m) =>
IdpApplication a i ->
IdpApplication i a ->
Manager ->
ExchangeTokenInfo a ->
ExceptT TokenRequestError m OAuth2Token
Expand All @@ -58,7 +58,7 @@ conduitTokenRequest IdpApplication {..} mgr exchangeToken = do

conduitPkceTokenRequest ::
(HasTokenRequest a, ToQueryParam (TokenRequest a), MonadIO m) =>
IdpApplication a i ->
IdpApplication i a ->
Manager ->
(ExchangeTokenInfo a, CodeVerifier) ->
ExceptT TokenRequestError m OAuth2Token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class HasUserInfoRequest a

conduitUserInfoRequest ::
(HasUserInfoRequest a, FromJSON (IdpUserInfo i), MonadIO m) =>
IdpApplication a i ->
IdpApplication i a ->
Manager ->
AccessToken ->
ExceptT BSL.ByteString m (IdpUserInfo i)
Expand Down
29 changes: 11 additions & 18 deletions hoauth2/src/Network/OAuth2/Experiment/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,26 @@ import URI.ByteString hiding (UserInfo)

-------------------------------------------------------------------------------

-- | Shall IdpApplication has a field of 'Idp a'??
data Idp a = Idp
type family IdpUserInfo a

-- NOTE: maybe worth data type to distinguish authorize and token endpoint
-- as I made mistake at passing to Authorize and Token Request
data Idp i = Idp
{ idpUserInfoEndpoint :: URI
, -- NOTE: maybe worth data type to distinguish authorize and token endpoint
-- as I made mistake at passing to Authorize and Token Request
idpAuthorizeEndpoint :: URI
, idpAuthorizeEndpoint :: URI
, idpTokenEndpoint :: URI
, idpFetchUserInfo ::
forall m.
(FromJSON (IdpUserInfo a), MonadIO m) =>
(FromJSON (IdpUserInfo i), MonadIO m) =>
Manager ->
AccessToken ->
URI ->
ExceptT BSL.ByteString m (IdpUserInfo a)
ExceptT BSL.ByteString m (IdpUserInfo i)
}

data IdpApplication a i = IdpApplication
{ application :: a
, idp :: Idp i
data IdpApplication i a = IdpApplication
{ idp :: Idp i
, application :: a
}

-------------------------------------------------------------------------------
Expand Down Expand Up @@ -215,11 +216,3 @@ instance ToQueryParam OAuth2.RefreshToken where
instance ToQueryParam ResponseType where
toQueryParam :: ResponseType -> Map Text Text
toQueryParam Code = Map.singleton "response_type" "code"

-------------------------------------------------------------------------------

-- * User Info types

-------------------------------------------------------------------------------

type family IdpUserInfo a

0 comments on commit eda7898

Please sign in to comment.