{-# LANGUAGE QuasiQuotes #-}

-- | [Fitbit Authorization developer guide](https://dev.fitbit.com/build/reference/web-api/developer-guide/authorization/)
module Network.OAuth2.Provider.Fitbit where

import Control.Monad (mzero)
import Control.Monad.IO.Class (MonadIO (..))
import Control.Monad.Trans.Except (ExceptT (..))
import Data.Aeson
import Data.ByteString.Lazy.Char8 qualified as BSL
import Data.Map.Strict qualified as Map
import Data.Set qualified as Set
import Data.Text.Lazy (Text)
import Network.HTTP.Conduit (Manager)
import Network.OAuth.OAuth2
import Network.OAuth2.Experiment
import Network.OAuth2.Provider
import URI.ByteString.QQ

sampleFitbitAuthorizationCodeApp :: AuthorizationCodeApplication
sampleFitbitAuthorizationCodeApp :: AuthorizationCodeApplication
sampleFitbitAuthorizationCodeApp =
  AuthorizationCodeApplication
    { acClientId :: ClientId
acClientId = ClientId
""
    , acClientSecret :: ClientSecret
acClientSecret = ClientSecret
""
    , acScope :: Set Scope
acScope = forall a. Set a
Set.empty
    , acAuthorizeRequestExtraParams :: Map Text Text
acAuthorizeRequestExtraParams = forall k a. Map k a
Map.empty
    , acAuthorizeState :: AuthorizeState
acAuthorizeState = AuthorizeState
"CHANGE_ME"
    , acRedirectUri :: URI
acRedirectUri = [uri|http://localhost|]
    , acName :: Text
acName = Text
"sample-fitbit-authorization-code-app"
    , acTokenRequestAuthenticationMethod :: ClientAuthenticationMethod
acTokenRequestAuthenticationMethod = ClientAuthenticationMethod
ClientSecretBasic
    }

fetchUserInfo ::
  (MonadIO m, HasUserInfoRequest a, FromJSON b) =>
  IdpApplication i a ->
  Manager ->
  AccessToken ->
  ExceptT BSL.ByteString m b
fetchUserInfo :: forall {k} (m :: * -> *) a b (i :: k).
(MonadIO m, HasUserInfoRequest a, FromJSON b) =>
IdpApplication i a
-> Manager -> AccessToken -> ExceptT ByteString m b
fetchUserInfo = forall {k} (m :: * -> *) a b (i :: k).
(MonadIO m, HasUserInfoRequest a, FromJSON b) =>
IdpApplication i a
-> Manager -> AccessToken -> ExceptT ByteString m b
conduitUserInfoRequest

defaultFitbitIdp :: Idp Fitbit
defaultFitbitIdp :: Idp 'Fitbit
defaultFitbitIdp =
  Idp
    { idpUserInfoEndpoint :: URI
idpUserInfoEndpoint = [uri|https://api.fitbit.com/1/user/-/profile.json|]
    , idpAuthorizeEndpoint :: URI
idpAuthorizeEndpoint = [uri|https://www.fitbit.com/oauth2/authorize|]
    , idpTokenEndpoint :: URI
idpTokenEndpoint = [uri|https://api.fitbit.com/oauth2/token|]
    , idpDeviceAuthorizationEndpoint :: Maybe URI
idpDeviceAuthorizationEndpoint = forall a. Maybe a
Nothing
    }

data FitbitUser = FitbitUser
  { FitbitUser -> Text
userId :: Text
  , FitbitUser -> Text
userName :: Text
  , FitbitUser -> Int
userAge :: Int
  }
  deriving (Int -> FitbitUser -> ShowS
[FitbitUser] -> ShowS
FitbitUser -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FitbitUser] -> ShowS
$cshowList :: [FitbitUser] -> ShowS
show :: FitbitUser -> String
$cshow :: FitbitUser -> String
showsPrec :: Int -> FitbitUser -> ShowS
$cshowsPrec :: Int -> FitbitUser -> ShowS
Show, FitbitUser -> FitbitUser -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FitbitUser -> FitbitUser -> Bool
$c/= :: FitbitUser -> FitbitUser -> Bool
== :: FitbitUser -> FitbitUser -> Bool
$c== :: FitbitUser -> FitbitUser -> Bool
Eq)

instance FromJSON FitbitUser where
  parseJSON :: Value -> Parser FitbitUser
parseJSON (Object Object
o) =
    Text -> Text -> Int -> FitbitUser
FitbitUser
      forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"user") forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"encodedId"))
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ((Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"user") forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"fullName"))
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ((Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"user") forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"age"))
  parseJSON Value
_ = forall (m :: * -> *) a. MonadPlus m => m a
mzero