diff --git a/_examples/oauth2/example.go b/_examples/oauth2/example.go index 6e1c323f..18438287 100644 --- a/_examples/oauth2/example.go +++ b/_examples/oauth2/example.go @@ -87,7 +87,11 @@ func handleRoot(w http.ResponseWriter, r *http.Request) { } func handleLogin(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, client.GenerateAuthorizationURL(baseURL+"/trylogin", discord.PermissionsNone, 0, false, discord.OAuth2ScopeIdentify, discord.OAuth2ScopeGuilds, discord.OAuth2ScopeEmail, discord.OAuth2ScopeConnections, discord.OAuth2ScopeWebhookIncoming), http.StatusSeeOther) + params := oauth2.AuthorizationURLParams{ + RedirectURI: baseURL + "/trylogin", + Scopes: []discord.OAuth2Scope{discord.OAuth2ScopeIdentify, discord.OAuth2ScopeGuilds, discord.OAuth2ScopeEmail, discord.OAuth2ScopeConnections, discord.OAuth2ScopeWebhookIncoming}, + } + http.Redirect(w, r, client.GenerateAuthorizationURL(params), http.StatusSeeOther) } func handleTryLogin(w http.ResponseWriter, r *http.Request) { diff --git a/_examples/verified_roles/main.go b/_examples/verified_roles/main.go index 2fadbaa5..a053bd83 100644 --- a/_examples/verified_roles/main.go +++ b/_examples/verified_roles/main.go @@ -52,7 +52,11 @@ func main() { } func handleVerify(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, oAuth2Client.GenerateAuthorizationURL(baseURL+"/callback", discord.PermissionsNone, 0, false, discord.OAuth2ScopeIdentify, discord.OAuth2ScopeRoleConnectionsWrite), http.StatusTemporaryRedirect) + params := oauth2.AuthorizationURLParams{ + RedirectURI: baseURL + "/callback", + Scopes: []discord.OAuth2Scope{discord.OAuth2ScopeIdentify, discord.OAuth2ScopeRoleConnectionsWrite}, + } + http.Redirect(w, r, oAuth2Client.GenerateAuthorizationURL(params), http.StatusTemporaryRedirect) } func handleCallback(w http.ResponseWriter, r *http.Request) { diff --git a/oauth2/client.go b/oauth2/client.go index c7a83ee5..60a1ff66 100644 --- a/oauth2/client.go +++ b/oauth2/client.go @@ -46,6 +46,15 @@ func (s Session) Expired() bool { return s.Expiration.Before(time.Now()) } +type AuthorizationURLParams struct { + RedirectURI string + Permissions discord.Permissions + GuildID snowflake.ID + DisableGuildSelect bool + IntegrationType discord.ApplicationIntegrationType + Scopes []discord.OAuth2Scope +} + // Client is a high level wrapper around Discord's OAuth2 API. type Client interface { // ID returns the configured client ID. @@ -58,10 +67,10 @@ type Client interface { // StateController returns the configured StateController. StateController() StateController - // GenerateAuthorizationURL generates an authorization URL with the given redirect URI, permissions, guildID, disableGuildSelect & scopes. State is automatically generated. - GenerateAuthorizationURL(redirectURI string, permissions discord.Permissions, guildID snowflake.ID, disableGuildSelect bool, integrationType discord.ApplicationIntegrationType, scopes ...discord.OAuth2Scope) string - // GenerateAuthorizationURLState generates an authorization URL with the given redirect URI, permissions, guildID, disableGuildSelect & scopes. State is automatically generated & returned. - GenerateAuthorizationURLState(redirectURI string, permissions discord.Permissions, guildID snowflake.ID, disableGuildSelect bool, integrationType discord.ApplicationIntegrationType, scopes ...discord.OAuth2Scope) (string, string) + // GenerateAuthorizationURL generates an authorization URL with the given authorization params. State is automatically generated. + GenerateAuthorizationURL(params AuthorizationURLParams) string + // GenerateAuthorizationURLState generates an authorization URL with the given authorization params. State is automatically generated & returned. + GenerateAuthorizationURLState(params AuthorizationURLParams) (string, string) // StartSession starts a new Session with the given authorization code & state. StartSession(code string, state string, opts ...rest.RequestOpt) (Session, *discord.IncomingWebhook, error) diff --git a/oauth2/client_impl.go b/oauth2/client_impl.go index 13c15771..961d2f44 100644 --- a/oauth2/client_impl.go +++ b/oauth2/client_impl.go @@ -47,30 +47,26 @@ func (c *clientImpl) StateController() StateController { return c.stateController } -func (c *clientImpl) GenerateAuthorizationURL(redirectURI string, permissions discord.Permissions, guildID snowflake.ID, disableGuildSelect bool, integrationType discord.ApplicationIntegrationType, scopes ...discord.OAuth2Scope) string { - authURL, _ := c.GenerateAuthorizationURLState(redirectURI, permissions, guildID, disableGuildSelect, integrationType, scopes...) +func (c *clientImpl) GenerateAuthorizationURL(params AuthorizationURLParams) string { + authURL, _ := c.GenerateAuthorizationURLState(params) return authURL } -func (c *clientImpl) GenerateAuthorizationURLState(redirectURI string, permissions discord.Permissions, guildID snowflake.ID, disableGuildSelect bool, integrationType discord.ApplicationIntegrationType, scopes ...discord.OAuth2Scope) (string, string) { - state := c.StateController().NewState(redirectURI) +func (c *clientImpl) GenerateAuthorizationURLState(params AuthorizationURLParams) (string, string) { + state := c.StateController().NewState(params.RedirectURI) values := discord.QueryValues{ - "client_id": c.id, - "redirect_uri": redirectURI, - "response_type": "code", - "scope": discord.JoinScopes(scopes), - "state": state, - "integration_type": integrationType, - } - - if permissions != discord.PermissionsNone { - values["permissions"] = permissions - } - if guildID != 0 { - values["guild_id"] = guildID - } - if disableGuildSelect { - values["disable_guild_select"] = true + "client_id": c.id, + "redirect_uri": params.RedirectURI, + "response_type": "code", + "scope": discord.JoinScopes(params.Scopes), + "state": state, + "permissions": params.Permissions, + "disable_guild_select": params.DisableGuildSelect, + "integration_type": params.IntegrationType, + } + + if params.GuildID != 0 { + values["guild_id"] = params.GuildID } return discord.AuthorizeURL(values), state }