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

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

type instance IdpUserInfo Dropbox = DropboxUser

defaultDropboxApp :: IdpApplication 'AuthorizationCode Dropbox
defaultDropboxApp :: IdpApplication 'AuthorizationCode Dropbox
defaultDropboxApp =
  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-dropbox-App",
      $sel:idpAppTokenRequestAuthenticationMethod:AuthorizationCodeIdpApplication :: ClientAuthenticationMethod
idpAppTokenRequestAuthenticationMethod = ClientAuthenticationMethod
ClientSecretBasic,
      $sel:idp:AuthorizationCodeIdpApplication :: Idp Dropbox
idp = Idp Dropbox
defaultDropboxIdp
    }

defaultDropboxIdp :: Idp Dropbox
defaultDropboxIdp :: Idp Dropbox
defaultDropboxIdp =
  Idp
    { $sel:idpFetchUserInfo:Idp :: forall (m :: * -> *).
(FromJSON (IdpUserInfo Dropbox), MonadIO m) =>
Manager
-> AccessToken -> URI -> ExceptT ByteString m (IdpUserInfo Dropbox)
idpFetchUserInfo = \Manager
mgr AccessToken
at URI
url -> forall a (m :: * -> *).
(FromJSON a, MonadIO m) =>
Manager
-> AccessToken
-> URI
-> [(ByteString, ByteString)]
-> ExceptT ByteString m a
authPostJSON @(IdpUserInfo Dropbox) Manager
mgr AccessToken
at URI
url [],
      $sel:idpAuthorizeEndpoint:Idp :: URI
idpAuthorizeEndpoint = [uri|https://www.dropbox.com/1/oauth2/authorize|],
      $sel:idpTokenEndpoint:Idp :: URI
idpTokenEndpoint = [uri|https://api.dropboxapi.com/oauth2/token|],
      $sel:idpUserInfoEndpoint:Idp :: URI
idpUserInfoEndpoint = [uri|https://api.dropboxapi.com/2/users/get_current_account|]
    }

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

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

instance FromJSON DropboxName where
  parseJSON :: Value -> Parser DropboxName
parseJSON = forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON Options
defaultOptions {fieldLabelModifier :: ShowS
fieldLabelModifier = Char -> ShowS
camelTo2 Char
'_'}

instance FromJSON DropboxUser