|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | +{-# LANGUAGE RecordWildCards #-} |
| 3 | +module Kubernetes.Client.Auth.Basic where |
| 4 | + |
| 5 | +import Data.ByteString.Base64 ( encode ) |
| 6 | +import Data.Function ( (&) ) |
| 7 | +import Data.Monoid ( (<>) ) |
| 8 | +import Data.Text ( Text ) |
| 9 | +import Kubernetes.Client.Auth.Internal.Types |
| 10 | +import Kubernetes.OpenAPI.Core |
| 11 | +import Kubernetes.Client.KubeConfig |
| 12 | + |
| 13 | +import qualified Data.Text.Encoding as T |
| 14 | +import qualified Lens.Micro as L |
| 15 | + |
| 16 | + |
| 17 | +data BasicAuth = BasicAuth { basicAuthUsername :: Text |
| 18 | + , basicAuthPassword :: Text |
| 19 | + } |
| 20 | + |
| 21 | +instance AuthMethod BasicAuth where |
| 22 | + applyAuthMethod _ BasicAuth{..} req = |
| 23 | + pure |
| 24 | + $ req |
| 25 | + `setHeader` toHeader ("authorization", "Basic " <> encodeBasicAuth) |
| 26 | + & L.set rAuthTypesL [] |
| 27 | + where |
| 28 | + encodeBasicAuth = T.decodeUtf8 $ encode $ T.encodeUtf8 $ basicAuthUsername <> ":" <> basicAuthPassword |
| 29 | + |
| 30 | +-- |Detects if username and password is specified in AuthConfig, if it is configures 'KubernetesClientConfig' with 'BasicAuth' |
| 31 | +basicAuth :: DetectAuth |
| 32 | +basicAuth auth (tlsParams, cfg) = do |
| 33 | + u <- username auth |
| 34 | + p <- password auth |
| 35 | + return $ return (tlsParams, setBasicAuth u p cfg) |
| 36 | + |
| 37 | +-- |Configures the 'KubernetesClientConfig' to use basic authentication. |
| 38 | +setBasicAuth |
| 39 | + :: Text -- ^Username |
| 40 | + -> Text -- ^Password |
| 41 | + -> KubernetesClientConfig |
| 42 | + -> KubernetesClientConfig |
| 43 | +setBasicAuth u p kcfg = kcfg |
| 44 | + { configAuthMethods = [AnyAuthMethod (BasicAuth u p)] |
| 45 | + } |
0 commit comments