Skip to content

Commit 440aa72

Browse files
committed
Support new hoauth2, due to newer oidc-client
1 parent 89dec17 commit 440aa72

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

kubernetes-client/kubernetes-client.cabal

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ library
5555
, data-default-class >=0.1
5656
, either >=5.0
5757
, filepath >=1.4
58-
, hoauth2 >=1.11
58+
, hoauth2 >=1.11 && <=2.3.0
5959
, http-client >=0.5 && <0.8
6060
, http-client-tls >=0.3
6161
, jose-jwt >=0.8
@@ -99,7 +99,7 @@ test-suite example
9999
, data-default-class >=0.1
100100
, either >=5.0
101101
, filepath >=1.4
102-
, hoauth2 >=1.11
102+
, hoauth2 >=1.11 && <=2.3.0
103103
, http-client >=0.5 && <0.8
104104
, http-client-tls >=0.3
105105
, jose-jwt >=0.8
@@ -150,7 +150,7 @@ test-suite spec
150150
, either >=5.0
151151
, file-embed
152152
, filepath >=1.4
153-
, hoauth2 >=1.11
153+
, hoauth2 >=1.11 && <=2.3.0
154154
, hspec
155155
, hspec-attoparsec
156156
, http-client >=0.5 && <0.8

kubernetes-client/package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ dependencies:
4545
- data-default-class >=0.1
4646
- either >=5.0
4747
- filepath >=1.4
48-
- hoauth2 >=1.11
48+
- hoauth2 >=1.11 && <=2.3.0
4949
- http-client >=0.5 && <0.8
5050
- http-client-tls >=0.3
5151
- jose-jwt >=0.8

kubernetes-client/src/Kubernetes/Client/Auth/OIDC.hs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
{-# LANGUAGE FlexibleContexts #-}
22
{-# LANGUAGE OverloadedStrings #-}
33
{-# LANGUAGE RecordWildCards #-}
4+
{-# LANGUAGE LambdaCase #-}
5+
{-# LANGUAGE CPP #-}
46
module Kubernetes.Client.Auth.OIDC
57
(oidcAuth, OIDCCache, cachedOIDCAuth)
68
where
79

810
import Control.Applicative
911
import Control.Concurrent.STM
1012
import Control.Exception.Safe (Exception, throwM)
13+
import Control.Monad.Except (runExceptT)
1114
import Data.Either.Combinators
1215
import Data.Function ((&))
1316
import Data.Map (Map)
1417
import Data.Maybe
1518
import Data.Monoid ((<>))
1619
import Data.Text
20+
import Data.Text.Encoding (encodeUtf8)
1721
import Data.Time.Clock.POSIX (getPOSIXTime)
1822
import Jose.Jwt
1923
import Kubernetes.Client.Auth.Internal.Types
@@ -41,6 +45,9 @@ data OIDCAuth = OIDCAuth { issuerURL :: Text
4145
, tlsParams :: TLS.ClientParams
4246
, idTokenTVar :: TVar(Maybe Text)
4347
, refreshTokenTVar :: TVar(Maybe Text)
48+
#if MIN_VERSION_hoauth2(2,3,0)
49+
, redirectUri :: URI
50+
#endif
4451
}
4552

4653
-- | Cache OIDCAuth based on issuerURL and clientID.
@@ -93,14 +100,43 @@ fetchToken auth@(OIDCAuth{..}) = do
93100
tokenEndpoint <- fetchTokenEndpoint mgr auth
94101
tokenURI <- parseURI strictURIParserOptions (Text.encodeUtf8 tokenEndpoint)
95102
& either (throwM . OIDCURIException) pure
103+
104+
#if MIN_VERSION_hoauth2(2,3,0)
105+
let oauth = OAuth2{ oauth2ClientId = clientID
106+
, oauth2ClientSecret = clientSecret
107+
, oauth2AuthorizeEndpoint = tokenURI
108+
, oauth2TokenEndpoint = tokenURI
109+
, oauth2RedirectUri = redirectUri
110+
}
111+
#elif MIN_VERSION_hoauth2(2,2,0)
112+
let oauth = OAuth2{ oauth2ClientId = clientID
113+
, oauth2ClientSecret = clientSecret
114+
, oauth2AuthorizeEndpoint = tokenURI
115+
, oauth2TokenEndpoint = tokenURI
116+
, oauth2RedirectUri = Nothing
117+
}
118+
#elif MIN_VERSION_hoauth2(2,0,0)
119+
let oauth = OAuth2{ oauth2ClientId = clientID
120+
, oauth2ClientSecret = Just clientSecret
121+
, oauth2AuthorizeEndpoint = tokenURI
122+
, oauth2TokenEndpoint = tokenURI
123+
, oauth2RedirectUri = Nothing
124+
}
125+
#else
96126
let oauth = OAuth2{ oauthClientId = clientID
97127
, oauthClientSecret = Just clientSecret
98128
, oauthAccessTokenEndpoint = tokenURI
99129
, oauthOAuthorizeEndpoint = tokenURI
100130
, oauthCallback = Nothing
101131
}
102-
oauthToken <- refreshAccessToken mgr oauth (RefreshToken token)
103-
>>= either (throwM . OIDCOAuthException) pure
132+
#endif
133+
134+
#if MIN_VERSION_hoauth2(2,2,0)
135+
oauthToken <- runExceptT (refreshAccessToken mgr oauth (RefreshToken token)) >>= either (throwM . OIDCOAuthException) pure
136+
#else
137+
oauthToken <- (refreshAccessToken mgr oauth (RefreshToken token)) >>= either (throwM . OIDCOAuthException) pure
138+
#endif
139+
104140
case OAuth.idToken oauthToken of
105141
Nothing -> throwM $ OIDCGetTokenException "token response did not contain an id_token, either the scope \"openid\" wasn't requested upon login, or the provider doesn't support id_tokens as part of the refresh response."
106142
Just (IdToken t) -> do
@@ -152,6 +188,15 @@ parseOIDCAuthInfo authInfo = do
152188
eitherTLSParams <- parseCA authInfo
153189
idTokenTVar <- atomically $ newTVar $ Map.lookup "id-token" authInfo
154190
refreshTokenTVar <- atomically $ newTVar $ Map.lookup "refresh-token" authInfo
191+
192+
#if MIN_VERSION_hoauth2(2,3,0)
193+
redirectUri <- case Map.lookup "redirect-uri" authInfo of
194+
Nothing -> throwM $ OIDCAuthMissingInformation "redirect-uri"
195+
Just raw -> case parseURI laxURIParserOptions $ encodeUtf8 raw of
196+
Left err -> throwM $ OIDCAuthMissingInformation ("Couldn't parse redirect URI: " <> show err)
197+
Right x -> return x
198+
#endif
199+
155200
return $ do
156201
tlsParams <- mapLeft OIDCAuthCAParsingFailed eitherTLSParams
157202
issuerURL <- lookupEither "idp-issuer-url"

0 commit comments

Comments
 (0)