{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeFamilies #-}

-- | https://docs.github.com/en/developers/apps/building-oauth-apps/authorizing-oauth-apps
module Network.OAuth2.Provider.Github where

import Data.Aeson
import Data.Map.Strict qualified as Map
import Data.Set qualified as Set
import Data.Text.Lazy (Text)
import GHC.Generics
import Network.OAuth.OAuth2
import Network.OAuth2.Experiment
import URI.ByteString.QQ

data Github = Github deriving (Github -> Github -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Github -> Github -> Bool
$c/= :: Github -> Github -> Bool
== :: Github -> Github -> Bool
$c== :: Github -> Github -> Bool
Eq, Int -> Github -> ShowS
[Github] -> ShowS
Github -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Github] -> ShowS
$cshowList :: [Github] -> ShowS
show :: Github -> String
$cshow :: Github -> String
showsPrec :: Int -> Github -> ShowS
$cshowsPrec :: Int -> Github -> ShowS
Show)

type instance IdpUserInfo Github = GithubUser

defaultGithubApp :: IdpApplication 'AuthorizationCode Github
defaultGithubApp :: IdpApplication 'AuthorizationCode Github
defaultGithubApp =
  AuthorizationCodeIdpApplication
    { $sel:idpAppClientId:AuthorizationCodeIdpApplication :: ClientId
idpAppClientId = ClientId
""
    , $sel:idpAppClientSecret:AuthorizationCodeIdpApplication :: ClientSecret
idpAppClientSecret = ClientSecret
""
    , $sel:idpAppScope:AuthorizationCodeIdpApplication :: Set Scope
idpAppScope = forall a. Set a
Set.empty
    , $sel:idpAppAuthorizeState:AuthorizationCodeIdpApplication :: AuthorizeState
idpAppAuthorizeState = AuthorizeState
"CHANGE_ME"
    , $sel:idpAppAuthorizeExtraParams:AuthorizationCodeIdpApplication :: Map Text Text
idpAppAuthorizeExtraParams = forall k a. Map k a
Map.empty
    , $sel:idpAppRedirectUri:AuthorizationCodeIdpApplication :: URI
idpAppRedirectUri = [uri|http://localhost|]
    , $sel:idpAppName:AuthorizationCodeIdpApplication :: Text
idpAppName = Text
"default-github-App"
    , $sel:idpAppTokenRequestAuthenticationMethod:AuthorizationCodeIdpApplication :: ClientAuthenticationMethod
idpAppTokenRequestAuthenticationMethod = ClientAuthenticationMethod
ClientSecretBasic
    , $sel:idp:AuthorizationCodeIdpApplication :: Idp Github
idp = Idp Github
defaultGithubIdp
    }

defaultGithubIdp :: Idp Github
defaultGithubIdp :: Idp Github
defaultGithubIdp =
  Idp
    { $sel:idpFetchUserInfo:Idp :: forall (m :: * -> *).
(FromJSON (IdpUserInfo Github), MonadIO m) =>
Manager
-> AccessToken -> URI -> ExceptT ByteString m (IdpUserInfo Github)
idpFetchUserInfo = forall a (m :: * -> *).
(FromJSON a, MonadIO m) =>
Manager -> AccessToken -> URI -> ExceptT ByteString m a
authGetJSON @(IdpUserInfo Github)
    , $sel:idpUserInfoEndpoint:Idp :: URI
idpUserInfoEndpoint = [uri|https://api.github.com/user|]
    , $sel:idpAuthorizeEndpoint:Idp :: URI
idpAuthorizeEndpoint = [uri|https://github.com/login/oauth/authorize|]
    , $sel:idpTokenEndpoint:Idp :: URI
idpTokenEndpoint = [uri|https://github.com/login/oauth/access_token|]
    }

data GithubUser = GithubUser
  { GithubUser -> Text
name :: Text
  , GithubUser -> Integer
id :: Integer
  }
  deriving (Int -> GithubUser -> ShowS
[GithubUser] -> ShowS
GithubUser -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GithubUser] -> ShowS
$cshowList :: [GithubUser] -> ShowS
show :: GithubUser -> String
$cshow :: GithubUser -> String
showsPrec :: Int -> GithubUser -> ShowS
$cshowsPrec :: Int -> GithubUser -> ShowS
Show, forall x. Rep GithubUser x -> GithubUser
forall x. GithubUser -> Rep GithubUser x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep GithubUser x -> GithubUser
$cfrom :: forall x. GithubUser -> Rep GithubUser x
Generic)

instance FromJSON GithubUser