hoauth2-2.8.0: Haskell OAuth2 authentication client

Safe Haskell | Safe-Inferred
Language | Haskell2010

Network.OAuth.OAuth2.TokenRequest

Contents

- Token Request Errors
- URL
- Token management
- Utilies

Description

Bindings Access Token and Refresh Token part of The OAuth 2.0 Authorization Framework RFC6749 https://www.rfc-editor.org/rfc/rfc6749

Synopsis

- data TokenRequestError = TokenRequestError {
  - error :: TokenRequestErrorCode
  - errorDescription :: Maybe Text
  - errorUri :: Maybe (URIRef Absolute)
  }
- data TokenRequestErrorCode
  - = InvalidRequest
  - | InvalidClient
  - | InvalidGrant
  - | UnauthorizedClient
  - | UnsupportedGrantType
  - | InvalidScope
  - | UnknownErrorCode Text
- parseTokeRequestError :: ByteString -> TokenRequestError
- accessTokenUrl :: OAuth2 -> ExchangeToken -> (URI, PostBody)
- refreshAccessTokenUrl :: OAuth2 -> RefreshToken -> (URI, PostBody)
- fetchAccessToken :: MonadIO m => Manager -> OAuth2 -> ExchangeToken -> ExceptT TokenRequestError m OAuth2Token
- fetchAccessToken2 :: MonadIO m => Manager -> OAuth2 -> ExchangeToken -> ExceptT TokenRequestError m OAuth2Token
- fetchAccessTokenInternal :: MonadIO m => ClientAuthenticationMethod -> Manager -> OAuth2 -> ExchangeToken -> ExceptT TokenRequestError m OAuth2Token
- fetchAccessTokenWithAuthMethod :: MonadIO m => ClientAuthenticationMethod -> Manager -> OAuth2 -> ExchangeToken -> ExceptT TokenRequestError m OAuth2Token
- refreshAccessToken :: MonadIO m => Manager -> OAuth2 -> RefreshToken -> ExceptT TokenRequestError m OAuth2Token
- refreshAccessToken2 :: MonadIO m => Manager -> OAuth2 -> RefreshToken -> ExceptT TokenRequestError m OAuth2Token
- refreshAccessTokenInternal :: MonadIO m => ClientAuthenticationMethod -> Manager -> OAuth2 -> RefreshToken -> ExceptT TokenRequestError m OAuth2Token
- refreshAccessTokenWithAuthMethod :: MonadIO m => ClientAuthenticationMethod -> Manager -> OAuth2 -> RefreshToken -> ExceptT TokenRequestError m OAuth2Token
- doJSONPostRequest :: (MonadIO m, FromJSON a) => Manager -> OAuth2 -> URI -> PostBody -> ExceptT TokenRequestError m a
- doSimplePostRequest :: MonadIO m => Manager -> OAuth2 -> URI -> PostBody -> ExceptT TokenRequestError m ByteString
- handleOAuth2TokenResponse :: Response ByteString -> Either TokenRequestError ByteString
- parseResponseFlexible :: FromJSON a => ByteString -> Either TokenRequestError a
- parseResponseString :: FromJSON a => ByteString -> Either TokenRequestError a
- addDefaultRequestHeaders :: Request -> Request
- clientSecretPost :: OAuth2 -> PostBody

# Token Request Errors

data TokenRequestError Source #

Constructors

TokenRequestError |

Fields

- error :: TokenRequestErrorCode
- errorDescription :: Maybe Text
- errorUri :: Maybe (URIRef Absolute)

#### Instances

Instances details

FromJSON TokenRequestError Source # |
Instance details Defined in Network.OAuth.OAuth2.TokenRequest Methods parseJSON :: Value -> Parser TokenRequestError Source # parseJSONList :: Value -> Parser [TokenRequestError] Source #
Generic TokenRequestError Source # |
Instance details Defined in Network.OAuth.OAuth2.TokenRequest Associated Types type Rep TokenRequestError :: Type -> Type Source # Methods from :: TokenRequestError -> Rep TokenRequestError x Source # to :: Rep TokenRequestError x -> TokenRequestError Source #
Show TokenRequestError Source # |
Instance details Defined in Network.OAuth.OAuth2.TokenRequest Methods showsPrec :: Int -> TokenRequestError -> ShowS Source # show :: TokenRequestError -> String Source # showList :: [TokenRequestError] -> ShowS Source #
Eq TokenRequestError Source # |
Instance details Defined in Network.OAuth.OAuth2.TokenRequest Methods (==) :: TokenRequestError -> TokenRequestError -> Bool Source # (/=) :: TokenRequestError -> TokenRequestError -> Bool Source #
type Rep TokenRequestError Source # |

Instance details

Defined in Network.OAuth.OAuth2.TokenRequest

type Rep TokenRequestError = D1 ('MetaData "TokenRequestError" "Network.OAuth.OAuth2.TokenRequest" "hoauth2-2.8.0-BZtVYBecI8q9t55UsNfiTk" 'False) (C1 ('MetaCons "TokenRequestError" 'PrefixI 'True) (S1 ('MetaSel ('Just "error") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 TokenRequestErrorCode) :*: (S1 ('MetaSel ('Just "errorDescription") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Maybe Text)) :*: S1 ('MetaSel ('Just "errorUri") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Maybe (URIRef Absolute))))))

data TokenRequestErrorCode Source #

Token Error Responses https://tools.ietf.org/html/rfc6749#section-5.2

Constructors

InvalidRequest |
InvalidClient |
InvalidGrant |
UnauthorizedClient |
UnsupportedGrantType |
InvalidScope |
UnknownErrorCode Text |

#### Instances

Instances details

FromJSON TokenRequestErrorCode Source # |
Instance details Defined in Network.OAuth.OAuth2.TokenRequest Methods parseJSON :: Value -> Parser TokenRequestErrorCode Source # parseJSONList :: Value -> Parser [TokenRequestErrorCode] Source #
Show TokenRequestErrorCode Source # |
Instance details Defined in Network.OAuth.OAuth2.TokenRequest Methods showsPrec :: Int -> TokenRequestErrorCode -> ShowS Source # show :: TokenRequestErrorCode -> String Source # showList :: [TokenRequestErrorCode] -> ShowS Source #
Eq TokenRequestErrorCode Source # |
Instance details Defined in Network.OAuth.OAuth2.TokenRequest Methods (==) :: TokenRequestErrorCode -> TokenRequestErrorCode -> Bool Source # (/=) :: TokenRequestErrorCode -> TokenRequestErrorCode -> Bool Source #

parseTokeRequestError :: ByteString -> TokenRequestError Source #

# URL

accessTokenUrl Source #

Arguments

:: OAuth2 |
-> ExchangeToken | access code gained via authorization URL
-> (URI, PostBody) | access token request URL plus the request body.

Prepare the URL and the request body query for fetching an access token.

refreshAccessTokenUrl Source #

Arguments

:: OAuth2 |
-> RefreshToken | Refresh Token gained via authorization URL
-> (URI, PostBody) | Refresh Token request URL plus the request body.

Obtain a new access token by sending a Refresh Token to the Authorization server.

# Token management

fetchAccessToken Source #

Arguments

:: MonadIO m |
=> Manager | HTTP connection manager
-> OAuth2 | OAuth Data
-> ExchangeToken | OAuth2 Code
-> ExceptT TokenRequestError m OAuth2Token | Access Token

Exchange `code` for an Access Token with authenticate in request header.

fetchAccessToken2 Source #

Arguments

:: MonadIO m |
=> Manager | HTTP connection manager
-> OAuth2 | OAuth Data
-> ExchangeToken | Authorization Code
-> ExceptT TokenRequestError m OAuth2Token | Access Token

Deprecated: use `fetchAccessTokenWithAuthMethod`

fetchAccessTokenInternal Source #

Arguments

:: MonadIO m |
=> ClientAuthenticationMethod |
-> Manager | HTTP connection manager
-> OAuth2 | OAuth Data
-> ExchangeToken | Authorization Code
-> ExceptT TokenRequestError m OAuth2Token | Access Token

Deprecated: use `fetchAccessTokenWithAuthMethod`

fetchAccessTokenWithAuthMethod Source #

Arguments

:: MonadIO m |
=> ClientAuthenticationMethod |
-> Manager | HTTP connection manager
-> OAuth2 | OAuth Data
-> ExchangeToken | Authorization Code
-> ExceptT TokenRequestError m OAuth2Token | Access Token

Exchange `code` for an Access Token

OAuth2 spec allows credential (`client_id`, `client_secret`) to be sent either in the header (a.k.a `ClientSecretBasic`). or as form/url params (a.k.a `ClientSecretPost`).

The OAuth provider can choose to implement only one, or both. Look for API document from the OAuth provider you're dealing with. If you're uncertain, try `fetchAccessToken` which sends credential in authorization http header, which is common case.

Since: 2.6.0

refreshAccessToken Source #

Arguments

:: MonadIO m |
=> Manager | HTTP connection manager.
-> OAuth2 | OAuth context
-> RefreshToken | Refresh Token gained after authorization
-> ExceptT TokenRequestError m OAuth2Token |

Fetch a new AccessToken using the Refresh Token with authentication in request header.

refreshAccessToken2 Source #

Arguments

:: MonadIO m |
=> Manager | HTTP connection manager.
-> OAuth2 | OAuth context
-> RefreshToken | Refresh Token gained after authorization
-> ExceptT TokenRequestError m OAuth2Token |

Deprecated: use `refreshAccessTokenWithAuthMethod`

refreshAccessTokenInternal Source #

Arguments

:: MonadIO m |
=> ClientAuthenticationMethod |
-> Manager | HTTP connection manager.
-> OAuth2 | OAuth context
-> RefreshToken | Refresh Token gained after authorization
-> ExceptT TokenRequestError m OAuth2Token |

Deprecated: use `refreshAccessTokenWithAuthMethod`

refreshAccessTokenWithAuthMethod Source #

Arguments

:: MonadIO m |
=> ClientAuthenticationMethod |
-> Manager | HTTP connection manager.
-> OAuth2 | OAuth context
-> RefreshToken | Refresh Token gained after authorization
-> ExceptT TokenRequestError m OAuth2Token |

Fetch a new AccessToken using the Refresh Token.

OAuth2 spec allows credential (`client_id`, `client_secret`) to be sent either in the header (a.k.a `ClientSecretBasic`). or as form/url params (a.k.a `ClientSecretPost`).

The OAuth provider can choose to implement only one, or both. Look for API document from the OAuth provider you're dealing with. If you're uncertain, try `refreshAccessToken` which sends credential in authorization http header, which is common case.

Since: 2.6.0

# Utilies

doJSONPostRequest Source #

Arguments

:: (MonadIO m, FromJSON a) |
=> Manager | HTTP connection manager.
-> OAuth2 | OAuth options
-> URI | The URL
-> PostBody | request body
-> ExceptT TokenRequestError m a | Response as JSON

Conduct post request and return response as JSON.

doSimplePostRequest Source #

Arguments

:: MonadIO m |
=> Manager | HTTP connection manager.
-> OAuth2 | OAuth options
-> URI | URL
-> PostBody | Request body.
-> ExceptT TokenRequestError m ByteString | Response as ByteString

Conduct post request.

handleOAuth2TokenResponse :: Response ByteString -> Either TokenRequestError ByteString Source #

Gets response body from a `Response` if 200 otherwise assume `OAuth2Error`

parseResponseFlexible :: FromJSON a => ByteString -> Either TokenRequestError a Source #

Try to parses response as JSON, if failed, try to parse as like query string.

parseResponseString :: FromJSON a => ByteString -> Either TokenRequestError a Source #

Parses the response that contains not JSON but a Query String

addDefaultRequestHeaders :: Request -> Request Source #

Set several header values: + userAgennt : `hoauth2` + accept : `application/json`

clientSecretPost :: OAuth2 -> PostBody Source #

Add Credential (client_id, client_secret) to the request post body.