{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Tesla.Auth (AuthInfo(..),
clientID, clientSecret, email, password, bearerToken,
fromToken,
AuthResponse(..), access_token, expires_in, refresh_token,
HasTeslaAuth(..)
) where
import Control.Lens
import Data.Aeson (FromJSON (..), Options, defaultOptions, fieldLabelModifier, genericParseJSON)
import Generics.Deriving.Base (Generic)
data AuthInfo = AuthInfo {
_clientID :: String
, _clientSecret :: String
, _email :: String
, _password :: String
, _bearerToken :: String
} deriving(Show)
makeLenses ''AuthInfo
fromToken :: String -> AuthInfo
fromToken t = AuthInfo{_bearerToken=t, _clientID="", _clientSecret="", _email="", _password=""}
jsonOpts :: Data.Aeson.Options
jsonOpts = defaultOptions {
fieldLabelModifier = dropWhile (== '_')
}
data AuthResponse = AuthResponse {
_access_token :: String
, _expires_in :: Int
, _refresh_token :: String
} deriving(Generic, Show)
makeLenses ''AuthResponse
class Monad m => HasTeslaAuth m where
teslaAuth :: m AuthInfo
instance FromJSON AuthResponse where
parseJSON = genericParseJSON jsonOpts