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

module Network.OAuth2.Provider.Slack 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 Slack = Slack deriving (Int -> Slack -> ShowS
[Slack] -> ShowS
Slack -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Slack] -> ShowS
$cshowList :: [Slack] -> ShowS
show :: Slack -> String
$cshow :: Slack -> String
showsPrec :: Int -> Slack -> ShowS
$cshowsPrec :: Int -> Slack -> ShowS
Show, Slack -> Slack -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Slack -> Slack -> Bool
$c/= :: Slack -> Slack -> Bool
== :: Slack -> Slack -> Bool
$c== :: Slack -> Slack -> Bool
Eq)

type instance IdpUserInfo Slack = SlackUser

defaultSlackApp :: IdpApplication 'AuthorizationCode Slack
defaultSlackApp :: IdpApplication 'AuthorizationCode Slack
defaultSlackApp =
  AuthorizationCodeIdpApplication
    { $sel:idpAppClientId:AuthorizationCodeIdpApplication :: ClientId
idpAppClientId = ClientId
"",
      $sel:idpAppClientSecret:AuthorizationCodeIdpApplication :: ClientSecret
idpAppClientSecret = ClientSecret
"",
      $sel:idpAppScope:AuthorizationCodeIdpApplication :: Set Scope
idpAppScope = forall a. Ord a => [a] -> Set a
Set.fromList [Scope
"openid", Scope
"profile"],
      $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:idpAppTokenRequestAuthenticationMethod:AuthorizationCodeIdpApplication :: ClientAuthenticationMethod
idpAppTokenRequestAuthenticationMethod = ClientAuthenticationMethod
ClientSecretBasic,
      $sel:idpAppName:AuthorizationCodeIdpApplication :: Text
idpAppName = Text
"default-slack-App",
      $sel:idp:AuthorizationCodeIdpApplication :: Idp Slack
idp = Idp Slack
defaultSlackIdp
    }

-- https://api.slack.com/authentication/sign-in-with-slack
-- https://slack.com/.well-known/openid-configuration
defaultSlackIdp :: Idp Slack
defaultSlackIdp :: Idp Slack
defaultSlackIdp =
  Idp
    { $sel:idpFetchUserInfo:Idp :: forall (m :: * -> *).
(FromJSON (IdpUserInfo Slack), MonadIO m) =>
Manager
-> AccessToken -> URI -> ExceptT ByteString m (IdpUserInfo Slack)
idpFetchUserInfo = forall a (m :: * -> *).
(FromJSON a, MonadIO m) =>
Manager -> AccessToken -> URI -> ExceptT ByteString m a
authGetJSON @(IdpUserInfo Slack),
      $sel:idpUserInfoEndpoint:Idp :: URI
idpUserInfoEndpoint = [uri|https://slack.com/api/openid.connect.userInfo|],
      $sel:idpAuthorizeEndpoint:Idp :: URI
idpAuthorizeEndpoint = [uri|https://slack.com/openid/connect/authorize|],
      $sel:idpTokenEndpoint:Idp :: URI
idpTokenEndpoint = [uri|https://slack.com/api/openid.connect.token|]
    }

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

instance FromJSON SlackUser