Documentation
¶
Index ¶
- Variables
- type AuthorizationURLParams
- type Client
- func (c *Client) GenerateAuthorizationURL(params AuthorizationURLParams) string
- func (c *Client) GenerateAuthorizationURLState(params AuthorizationURLParams) (string, string)
- func (c *Client) GetApplicationRoleConnection(session Session, applicationID snowflake.ID, opts ...rest.RequestOpt) (*discord.ApplicationRoleConnection, error)
- 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) GetMember(session Session, guildID snowflake.ID, opts ...rest.RequestOpt) (*discord.Member, error)
- func (c *Client) GetUser(session Session, opts ...rest.RequestOpt) (*discord.OAuth2User, error)
- func (c *Client) RefreshSession(session Session, opts ...rest.RequestOpt) (Session, error)
- func (c *Client) StartSession(code string, state string, opts ...rest.RequestOpt) (Session, *discord.IncomingWebhook, error)
- func (c *Client) UpdateApplicationRoleConnection(session Session, applicationID snowflake.ID, ...) (*discord.ApplicationRoleConnection, error)
- func (c *Client) VerifySession(session Session, opts ...rest.RequestOpt) (Session, error)
- type ConfigOpt
- func WithLogger(logger *slog.Logger) ConfigOpt
- func WithOAuth2(oauth2 rest.OAuth2) ConfigOpt
- func WithRestClient(restClient rest.Client) ConfigOpt
- func WithRestClientConfigOpts(opts ...rest.ClientConfigOpt) ConfigOpt
- func WithStateController(stateController StateController) ConfigOpt
- func WithStateControllerOpts(opts ...StateControllerConfigOpt) ConfigOpt
- type Session
- type StateController
- type StateControllerConfigOpt
Constants ¶
This section is empty.
Variables ¶
var ( // ErrStateNotFound is returned when the state is not found in the SessionController. ErrStateNotFound = errors.New("state could not be found") // ErrSessionExpired is returned when the Session has expired. ErrSessionExpired = errors.New("access token expired. refresh the session") // ErrMissingOAuth2Scope is returned when a specific OAuth2 scope is missing. ErrMissingOAuth2Scope = func(scope discord.OAuth2Scope) error { return fmt.Errorf("missing '%s' scope", scope) } )
Functions ¶
This section is empty.
Types ¶
type AuthorizationURLParams ¶ added in v0.18.0
type AuthorizationURLParams struct {
RedirectURI string
Permissions discord.Permissions
GuildID snowflake.ID
DisableGuildSelect bool
IntegrationType discord.ApplicationIntegrationType
Scopes []discord.OAuth2Scope
}
type Client ¶
type Client struct {
ID snowflake.ID
Secret string
Rest rest.OAuth2
StateController StateController
}
func (*Client) GenerateAuthorizationURL ¶
func (c *Client) GenerateAuthorizationURL(params AuthorizationURLParams) string
func (*Client) GenerateAuthorizationURLState ¶
func (c *Client) GenerateAuthorizationURLState(params AuthorizationURLParams) (string, string)
func (*Client) GetApplicationRoleConnection ¶ added in v0.14.1
func (c *Client) GetApplicationRoleConnection(session Session, applicationID snowflake.ID, opts ...rest.RequestOpt) (*discord.ApplicationRoleConnection, error)
func (*Client) GetConnections ¶
func (c *Client) GetConnections(session Session, opts ...rest.RequestOpt) ([]discord.Connection, error)
func (*Client) GetGuilds ¶
func (c *Client) GetGuilds(session Session, opts ...rest.RequestOpt) ([]discord.OAuth2Guild, error)
func (*Client) GetUser ¶
func (c *Client) GetUser(session Session, opts ...rest.RequestOpt) (*discord.OAuth2User, error)
func (*Client) RefreshSession ¶
func (*Client) StartSession ¶
func (c *Client) StartSession(code string, state string, opts ...rest.RequestOpt) (Session, *discord.IncomingWebhook, error)
func (*Client) UpdateApplicationRoleConnection ¶ added in v0.14.1
func (c *Client) UpdateApplicationRoleConnection(session Session, applicationID snowflake.ID, update discord.ApplicationRoleConnectionUpdate, opts ...rest.RequestOpt) (*discord.ApplicationRoleConnection, error)
func (*Client) VerifySession ¶ added in v0.15.2
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 WithOAuth2 ¶
WithOAuth2 applies a custom rest.OAuth2 to the OAuth2 client
func WithRestClient ¶
WithRestClient applies a custom rest.Client to the OAuth2 client
func WithRestClientConfigOpts ¶
func WithRestClientConfigOpts(opts ...rest.ClientConfigOpt) ConfigOpt
WithRestClientConfigOpts applies rest.ConfigOpt for the rest.Client to the OAuth2 client
func WithStateController ¶
func WithStateController(stateController StateController) ConfigOpt
WithStateController applies a custom StateController to the OAuth2 client
func WithStateControllerOpts ¶
func WithStateControllerOpts(opts ...StateControllerConfigOpt) ConfigOpt
WithStateControllerOpts applies all StateControllerConfigOpt(s) to the StateController
type Session ¶
type Session struct {
// AccessToken allows requesting user information
AccessToken string `json:"access_token"`
// RefreshToken allows refreshing the AccessToken
RefreshToken string `json:"refresh_token"`
// Scopes returns the discord.OAuth2Scope(s) of the Session
Scopes []discord.OAuth2Scope `json:"scopes"`
// TokenType returns the discord.TokenType of the AccessToken
TokenType discord.TokenType `json:"token_type"`
// Expiration returns the time.Time when the AccessToken expires and needs to be refreshed
Expiration time.Time `json:"expiration"`
}
Session represents a discord access token response (https://discord.com/developers/docs/topics/oauth2#authorization-code-grant-access-token-response)
type StateController ¶
type StateController interface {
// NewState generates a new random state to be used as a state.
NewState(redirectURI string) string
// UseState validates a state and returns the redirect url or nil if it is invalid.
UseState(state string) string
}
StateController is responsible for generating, storing and validating states.
func NewStateController ¶
func NewStateController(opts ...StateControllerConfigOpt) StateController
NewStateController returns a new empty StateController.
type StateControllerConfigOpt ¶
type StateControllerConfigOpt func(config *stateControllerConfig)
StateControllerConfigOpt is used to pass optional parameters to NewStateController
func WithMaxTTL ¶
func WithMaxTTL(maxTTL time.Duration) StateControllerConfigOpt
WithMaxTTL sets the maximum time to live for a state
func WithNewStateFunc ¶
func WithNewStateFunc(newStateFunc func() string) StateControllerConfigOpt
WithNewStateFunc sets the function which is used to generate a new random state
func WithStateControllerLogger ¶ added in v0.15.2
func WithStateControllerLogger(logger *slog.Logger) StateControllerConfigOpt
WithStateControllerLogger sets the logger for the StateController
func WithStates ¶
func WithStates(states map[string]string) StateControllerConfigOpt
WithStates loads states from an existing map