{-# LANGUAGE OverloadedStrings #-}

-- |
-- OAuth2 plugin for https://slack.com/
--
-- * Authenticates against slack
-- * Uses slack user id as credentials identifier
module Yesod.Auth.OAuth2.Slack
  ( SlackScope (..)
  , oauth2Slack
  , oauth2SlackScoped
  ) where

import Yesod.Auth.OAuth2.Prelude

import Network.HTTP.Client
  ( httpLbs
  , parseUrlThrow
  , responseBody
  , setQueryString
  )
import Yesod.Auth.OAuth2.Exception as YesodOAuth2Exception

data SlackScope
  = SlackBasicScope
  | SlackEmailScope
  | SlackTeamScope
  | SlackAvatarScope

scopeText :: SlackScope -> Text
scopeText :: SlackScope -> Text
scopeText SlackScope
SlackBasicScope = Text
"identity.basic"
scopeText SlackScope
SlackEmailScope = Text
"identity.email"
scopeText SlackScope
SlackTeamScope = Text
"identity.team"
scopeText SlackScope
SlackAvatarScope = Text
"identity.avatar"

newtype User = User Text

instance FromJSON User where
  parseJSON :: Value -> Parser User
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"User" forall a b. (a -> b) -> a -> b
$ \Object
root -> do
    Object
o <- Object
root forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"user"
    Text -> User
User forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"id"

pluginName :: Text
pluginName :: Text
pluginName = Text
"slack"

defaultScopes :: [SlackScope]
defaultScopes :: [SlackScope]
defaultScopes = [SlackScope
SlackBasicScope]

oauth2Slack :: YesodAuth m => Text -> Text -> AuthPlugin m
oauth2Slack :: forall m. YesodAuth m => Text -> Text -> AuthPlugin m
oauth2Slack = forall m.
YesodAuth m =>
[SlackScope] -> Text -> Text -> AuthPlugin m
oauth2SlackScoped [SlackScope]
defaultScopes

oauth2SlackScoped
  :: YesodAuth m => [SlackScope] -> Text -> Text -> AuthPlugin m
oauth2SlackScoped :: forall m.
YesodAuth m =>
[SlackScope] -> Text -> Text -> AuthPlugin m
oauth2SlackScoped [SlackScope]
scopes Text
clientId Text
clientSecret =
  forall m.
YesodAuth m =>
Text -> OAuth2 -> FetchCreds m -> AuthPlugin m
authOAuth2 Text
pluginName OAuth2
oauth2 forall a b. (a -> b) -> a -> b
$ \Manager
manager OAuth2Token
token -> do
    let param :: ByteString
param = Text -> ByteString
encodeUtf8 forall a b. (a -> b) -> a -> b
$ AccessToken -> Text
atoken forall a b. (a -> b) -> a -> b
$ OAuth2Token -> AccessToken
accessToken OAuth2Token
token
    Request
req <-
      [(ByteString, Maybe ByteString)] -> Request -> Request
setQueryString [(ByteString
"token", forall a. a -> Maybe a
Just ByteString
param)]
        forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). MonadThrow m => String -> m Request
parseUrlThrow String
"https://slack.com/api/users.identity"
    ByteString
userResponse <- forall body. Response body -> body
responseBody forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Request -> Manager -> IO (Response ByteString)
httpLbs Request
req Manager
manager

    forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
      (forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String -> YesodOAuth2Exception
YesodOAuth2Exception.JSONDecodingError Text
pluginName)
      ( \(User Text
userId) ->
          forall (f :: * -> *) a. Applicative f => a -> f a
pure
            Creds
              { credsPlugin :: Text
credsPlugin = Text
pluginName
              , credsIdent :: Text
credsIdent = Text
userId
              , credsExtra :: [(Text, Text)]
credsExtra = OAuth2Token -> ByteString -> [(Text, Text)]
setExtra OAuth2Token
token ByteString
userResponse
              }
      )
      forall a b. (a -> b) -> a -> b
$ forall a. FromJSON a => ByteString -> Either String a
eitherDecode ByteString
userResponse
 where
  oauth2 :: OAuth2
oauth2 =
    OAuth2
      { oauth2ClientId :: Text
oauth2ClientId = Text
clientId
      , oauth2ClientSecret :: Maybe Text
oauth2ClientSecret = forall a. a -> Maybe a
Just Text
clientSecret
      , oauth2AuthorizeEndpoint :: URIRef Absolute
oauth2AuthorizeEndpoint =
          URIRef Absolute
"https://slack.com/oauth/authorize"
            forall a. URIRef a -> [(ByteString, ByteString)] -> URIRef a
`withQuery` [Text -> [Text] -> (ByteString, ByteString)
scopeParam Text
"," forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map SlackScope -> Text
scopeText [SlackScope]
scopes]
      , oauth2TokenEndpoint :: URIRef Absolute
oauth2TokenEndpoint = URIRef Absolute
"https://slack.com/api/oauth.access"
      , oauth2RedirectUri :: Maybe (URIRef Absolute)
oauth2RedirectUri = forall a. Maybe a
Nothing
      }