Documentation
¶
Index ¶
- Variables
- type Client
- func (c *Client) GenerateAuthorizationURL(redirectURI string, permissions discord.Permissions, guildID discord.Snowflake, ...) string
- func (c *Client) GenerateAuthorizationURLState(redirectURI string, permissions discord.Permissions, guildID discord.Snowflake, ...) (string, string)
- func (c *Client) GetConnections(session Session, opts ...rest.RequestOpt) ([]discord.Connection, error)
- func (c *Client) GetGuilds(session Session, opts ...rest.RequestOpt) ([]discord.OAuth2Guild, error)
- func (c *Client) GetUser(session Session, opts ...rest.RequestOpt) (*discord.OAuth2User, error)
- func (c *Client) RefreshSession(identifier string, session Session, opts ...rest.RequestOpt) (Session, error)
- func (c *Client) StartSession(code string, state string, identifier string, opts ...rest.RequestOpt) (Session, error)
- type Config
- type ConfigOpt
- func WithLogger(logger log.Logger) ConfigOpt
- func WithOAuth2Service(oauth2service rest.OAuth2Service) ConfigOpt
- func WithRestClient(restClient rest.Client) ConfigOpt
- func WithRestClientConfig(restConfig rest.Config) ConfigOpt
- func WithRestClientConfigOpts(opts ...rest.ConfigOpt) ConfigOpt
- func WithSessionController(sessionController SessionController) ConfigOpt
- func WithStateController(stateController StateController) ConfigOpt
- func WithStateControllerConfig(stateControllerConfig StateControllerConfig) ConfigOpt
- func WithStateControllerOpts(opts ...StateControllerConfigOpt) ConfigOpt
- type Session
- type SessionController
- type StateController
- type StateControllerConfig
- type StateControllerConfigOpt
- type TTLMap
Constants ¶
This section is empty.
Variables ¶
var ( ErrStateNotFound = errors.New("state could not be found") ErrAccessTokenExpired = errors.New("access token expired. refresh the session") ErrMissingOAuth2Scope = func(scope discord.ApplicationScope) error { return fmt.Errorf("missing '%s' scope", scope) } )
errors returned by the OAuth2 client
var DefaultConfig = Config{ RestClientConfig: &rest.DefaultConfig, }
DefaultConfig is the configuration which is used by default
var DefaultStateControllerConfig = StateControllerConfig{ States: map[string]string{}, NewStateFunc: func() string { return insecurerandstr.RandStr(32) }, MaxTTL: time.Hour, }
DefaultStateControllerConfig is the default configuration for the StateController
Functions ¶
This section is empty.
Types ¶
type Client ¶
Client is an OAuth2 client
func (*Client) GenerateAuthorizationURL ¶
func (c *Client) GenerateAuthorizationURL(redirectURI string, permissions discord.Permissions, guildID discord.Snowflake, disableGuildSelect bool, scopes ...discord.ApplicationScope) string
GenerateAuthorizationURL generates an authorization URL with the given redirect URI, permissions, guildID, disableGuildSelect & scopes, state is automatically generated
func (*Client) GenerateAuthorizationURLState ¶ added in v0.6.3
func (c *Client) GenerateAuthorizationURLState(redirectURI string, permissions discord.Permissions, guildID discord.Snowflake, disableGuildSelect bool, scopes ...discord.ApplicationScope) (string, string)
GenerateAuthorizationURLState generates an authorization URL with the given redirect URI, permissions, guildID, disableGuildSelect & scopes, state is automatically generated & returned
func (*Client) GetConnections ¶
func (c *Client) GetConnections(session Session, opts ...rest.RequestOpt) ([]discord.Connection, error)
GetConnections returns the discord.Connection(s) the user has connected. This requires the discord.ApplicationScopeConnections scope in the session
func (*Client) GetGuilds ¶
func (c *Client) GetGuilds(session Session, opts ...rest.RequestOpt) ([]discord.OAuth2Guild, error)
GetGuilds returns the discord.OAuth2Guild(s) the user is a member of. This requires the discord.ApplicationScopeGuilds scope in the session
func (*Client) GetUser ¶
func (c *Client) GetUser(session Session, opts ...rest.RequestOpt) (*discord.OAuth2User, error)
GetUser returns the discord.OAuth2User associated with the given session. Fields filled in the struct depend on the Session.Scopes
func (*Client) RefreshSession ¶
func (c *Client) RefreshSession(identifier string, session Session, opts ...rest.RequestOpt) (Session, error)
RefreshSession refreshes the given session with the refresh token
func (*Client) StartSession ¶
func (c *Client) StartSession(code string, state string, identifier string, opts ...rest.RequestOpt) (Session, error)
StartSession starts a new session with the given authorization code & state
type Config ¶
type Config struct {
Logger log.Logger
RestClient rest.Client
RestClientConfig *rest.Config
OAuth2Service rest.OAuth2Service
SessionController SessionController
StateControllerConfig *StateControllerConfig
StateController StateController
}
Config is the configuration for the OAuth2 client
type ConfigOpt ¶
type ConfigOpt func(config *Config)
ConfigOpt can be used to supply optional parameters to New
func WithLogger ¶
WithLogger applies a custom logger to the OAuth2 client
func WithOAuth2Service ¶
func WithOAuth2Service(oauth2service rest.OAuth2Service) ConfigOpt
WithOAuth2Service applies a custom rest.OAuth2Service to the OAuth2 client
func WithRestClient ¶
WithRestClient applies a custom rest.Client to the OAuth2 client
func WithRestClientConfig ¶
WithRestClientConfig applies a custom rest.Config to the OAuth2 client
func WithRestClientConfigOpts ¶
WithRestClientConfigOpts applies rest.ConfigOpt for the rest.Client to the OAuth2 client
func WithSessionController ¶
func WithSessionController(sessionController SessionController) ConfigOpt
WithSessionController applies a custom SessionController to the OAuth2 client
func WithStateController ¶
func WithStateController(stateController StateController) ConfigOpt
WithStateController applies a custom StateController to the OAuth2 client
func WithStateControllerConfig ¶ added in v0.6.3
func WithStateControllerConfig(stateControllerConfig StateControllerConfig) ConfigOpt
WithStateControllerConfig applies a custom StateControllerConfig to the SessionController
func WithStateControllerOpts ¶ added in v0.6.3
func WithStateControllerOpts(opts ...StateControllerConfigOpt) ConfigOpt
WithStateControllerOpts applies all StateControllerConfigOpt(s) to the StateController
type Session ¶
type Session interface {
// AccessToken allows requesting user information
AccessToken() string
// RefreshToken allows refreshing the AccessToken
RefreshToken() string
// Scopes returns the discord.ApplicationScope(s) of the Session
Scopes() []discord.ApplicationScope
// TokenType returns the discord.TokenType of the AccessToken
TokenType() discord.TokenType
// Expiration returns the time.Time when the AccessToken expires and needs to be refreshed
Expiration() time.Time
// Webhook returns the discord.IncomingWebhook when the discord.ApplicationScopeWebhookIncoming is set
Webhook() *discord.IncomingWebhook
}
Session represents a discord access token response (https://discord.com/developers/docs/topics/oauth2#authorization-code-grant-access-token-response)
type SessionController ¶
type SessionController interface {
// GetSession returns the Session for the given identifier or nil if none was found
GetSession(identifier string) Session
// CreateSession creates a new Session from the given identifier, access token, refresh token, scope, token type, expiration and webhook
CreateSession(identifier string, accessToken string, refreshToken string, scopes []discord.ApplicationScope, tokenType discord.TokenType, expiration time.Time, webhook *discord.IncomingWebhook) Session
// CreateSessionFromExchange creates a new Session from the given identifier and discord.AccessTokenExchange payload
CreateSessionFromExchange(identifier string, exchange discord.AccessTokenExchange) Session
}
SessionController lets you manage your Session(s)
func NewSessionController ¶
func NewSessionController() SessionController
NewSessionController returns a new empty SessionController
func NewSessionControllerWithSessions ¶
func NewSessionControllerWithSessions(sessions map[string]Session) SessionController
NewSessionControllerWithSessions returns a new SessionController with the given Session(s)
type StateController ¶
type StateController interface {
// GenerateNewState generates a new random state to be used as a state
GenerateNewState(redirectURI string) string
// ConsumeState validates a state and returns the redirect url or nil if it is invalid
ConsumeState(state string) string
}
StateController is responsible for generating, storing and validating states
func NewStateController ¶
func NewStateController(config *StateControllerConfig) StateController
NewStateController returns a new empty StateController
type StateControllerConfig ¶ added in v0.6.3
type StateControllerConfig struct {
States map[string]string
NewStateFunc func() string
MaxTTL time.Duration
}
StateControllerConfig is the configuration for the StateController
func (*StateControllerConfig) Apply ¶ added in v0.6.3
func (c *StateControllerConfig) Apply(opts []StateControllerConfigOpt)
Apply applies the given StateControllerConfigOpt(s) to the StateControllerConfig
type StateControllerConfigOpt ¶ added in v0.6.3
type StateControllerConfigOpt func(config *StateControllerConfig)
StateControllerConfigOpt is used to pass optional parameters to NewStateController
func WithMaxTTL ¶ added in v0.6.3
func WithMaxTTL(maxTTL time.Duration) StateControllerConfigOpt
WithMaxTTL sets the maximum time to live for a state
func WithNewStateFunc ¶ added in v0.6.3
func WithNewStateFunc(newStateFunc func() string) StateControllerConfigOpt
WithNewStateFunc sets the function which is used to generate a new random state
func WithStates ¶ added in v0.6.3
func WithStates(states map[string]string) StateControllerConfigOpt
WithStates loads states from an existing map