go-twitch/auth/client.go

56 lines
1.4 KiB
Go
Raw Permalink Normal View History

2024-03-04 18:14:38 -05:00
package auth
type Client struct {
clientId string
clientSecret string
redirectUri string
stateStorage StateStorage
}
func NewClient(clientId string, clientSecret string, redirectUri string) *Client {
return &Client{
clientId: clientId,
clientSecret: clientSecret,
redirectUri: redirectUri,
stateStorage: NewHttpCookieStateStorage(StateStorageCookie),
}
}
// GetToken exchanges an authorization code for an access token.
//
// https://dev.twitch.tv/docs/authentication/getting-tokens-oidc/#oidc-authorization-code-grant-flow
func (c *Client) GetToken(code string) (*Token, error) {
return GetToken(&GetTokenParams{
ClientId: c.clientId,
ClientSecret: c.clientSecret,
Code: code,
GrantType: "authorization_code",
RedirectUri: c.redirectUri,
})
}
// RefreshToken exchanges a refresh token for an access token.
//
// https://dev.twitch.tv/docs/authentication/refresh-tokens/
func (c *Client) RefreshToken(token *Token) (*Token, error) {
return GetToken(&GetTokenParams{
ClientId: c.clientId,
ClientSecret: c.clientSecret,
Code: token.RefreshToken,
GrantType: "refresh_token",
RedirectUri: c.redirectUri,
})
}
// WithStateStorage sets the instance's state storage,
// which is used to store the state parameter between requests.
//
// By default, the http cookie state storage is used.
func (c *Client) WithStateStorage(storage StateStorage) *Client {
c.stateStorage = storage
return c
}