{-# LANGUAGE QuasiQuotes #-}

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

type instance IdpUserInfo Weibo = WeiboUID

defaultWeiboApp :: IdpApplication 'AuthorizationCode Weibo
defaultWeiboApp :: IdpApplication 'AuthorizationCode Weibo
defaultWeiboApp =
  AuthorizationCodeIdpApplication
    { $sel:idpAppName:AuthorizationCodeIdpApplication :: Text
idpAppName = Text
"default-weibo-App"
    , $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:idpAppTokenRequestAuthenticationMethod:AuthorizationCodeIdpApplication :: ClientAuthenticationMethod
idpAppTokenRequestAuthenticationMethod = ClientAuthenticationMethod
ClientSecretBasic
    , $sel:idp:AuthorizationCodeIdpApplication :: Idp Weibo
idp = Idp Weibo
defaultWeiboIdp
    }

defaultWeiboIdp :: Idp Weibo
defaultWeiboIdp :: Idp Weibo
defaultWeiboIdp =
  Idp
    { $sel:idpFetchUserInfo:Idp :: forall (m :: * -> *).
(FromJSON (IdpUserInfo Weibo), MonadIO m) =>
Manager
-> AccessToken -> URI -> ExceptT ByteString m (IdpUserInfo Weibo)
idpFetchUserInfo = forall (m :: * -> *) a.
(MonadIO m, FromJSON a) =>
APIAuthenticationMethod
-> Manager -> AccessToken -> URI -> ExceptT ByteString m a
authGetJSONWithAuthMethod @_ @(IdpUserInfo Weibo) APIAuthenticationMethod
AuthInRequestQuery
    , $sel:idpUserInfoEndpoint:Idp :: URI
idpUserInfoEndpoint = [uri|https://api.weibo.com/2/account/get_uid.json|]
    , $sel:idpAuthorizeEndpoint:Idp :: URI
idpAuthorizeEndpoint = [uri|https://api.weibo.com/oauth2/authorize|]
    , $sel:idpTokenEndpoint:Idp :: URI
idpTokenEndpoint = [uri|https://api.weibo.com/oauth2/access_token|]
    }

-- | http://open.weibo.com/wiki/2/users/show
data WeiboUser = WeiboUser
  { WeiboUser -> Integer
id :: Integer
  , WeiboUser -> Text
name :: Text
  , WeiboUser -> Text
screenName :: Text
  }
  deriving (Int -> WeiboUser -> ShowS
[WeiboUser] -> ShowS
WeiboUser -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [WeiboUser] -> ShowS
$cshowList :: [WeiboUser] -> ShowS
show :: WeiboUser -> String
$cshow :: WeiboUser -> String
showsPrec :: Int -> WeiboUser -> ShowS
$cshowsPrec :: Int -> WeiboUser -> ShowS
Show, forall x. Rep WeiboUser x -> WeiboUser
forall x. WeiboUser -> Rep WeiboUser x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep WeiboUser x -> WeiboUser
$cfrom :: forall x. WeiboUser -> Rep WeiboUser x
Generic)

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

instance FromJSON WeiboUID

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