diff options
Diffstat (limited to 'Biz/Auth.hs')
-rw-r--r-- | Biz/Auth.hs | 45 |
1 files changed, 40 insertions, 5 deletions
diff --git a/Biz/Auth.hs b/Biz/Auth.hs index ed904b9..1c3e45c 100644 --- a/Biz/Auth.hs +++ b/Biz/Auth.hs @@ -13,6 +13,7 @@ module Biz.Auth OAuthArgs (..), -- * GitHub Authentication + GitHub (..), githubOauth, -- * Servant Helpers @@ -28,6 +29,10 @@ import Servant (Header, Headers) import qualified Servant.Auth.Server as Auth import qualified System.Envy as Envy +-- | Use this instead of 'mempty' for explicity. +notset :: Text +notset = "notset" + -- | Wrapper around 'Auth.SetCookie' that you can put in a servant path -- descriptor. type SetCookies ret = @@ -45,9 +50,9 @@ data OAuthArgs = OAuthArgs instance Envy.DefConfig OAuthArgs where defConfig = OAuthArgs - { clientSecret = mempty, - clientId = mempty, - clientState = mempty + { clientSecret = notset, + clientId = notset, + clientState = notset } instance Envy.FromEnv OAuthArgs @@ -61,13 +66,43 @@ data OAuthResponse = OAuthResponse } deriving (Generic, Aeson.FromJSON) +newtype GitHub = GitHub OAuthArgs + deriving (Show, Generic) + +instance Envy.DefConfig GitHub where + defConfig = + GitHub + <| OAuthArgs + { clientSecret = notset, + clientId = notset, + clientState = notset + } + +instance Envy.FromEnv GitHub where + fromEnv Nothing = + GitHub + </ Envy.gFromEnvCustom + Envy.Option + { Envy.dropPrefixCount = 0, + Envy.customPrefix = "GITHUB" + } + Nothing + fromEnv (Just (GitHub x)) = + GitHub + </ Envy.gFromEnvCustom + Envy.Option + { Envy.dropPrefixCount = 0, + Envy.customPrefix = "GITHUB" + } + (Just x) + -- | POST to GitHub's OAuth service and get the user's oAuth token. githubOauth :: - OAuthArgs -> + GitHub -> -- | This should be GitHub.Token but GitHub.Auth doesn't export Token. Text -> IO OAuthResponse -githubOauth OAuthArgs {..} code = +githubOauth (GitHub OAuthArgs {..}) code = accessTokenRequest /> Req.responseBody |> Req.runReq Req.defaultHttpConfig |