From d861eea2be15fddeede96e945ffce6a1a3adc84b Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 8 Apr 2024 00:23:09 +0200 Subject: [PATCH] Refactor oauth params into a struct (#345) * Add support for user apps * add Name to InteractionMetadata * add integrationType to auth * Refactor oauth params into a struct * fix examples * check optional values like before * omit default integration_type * omit default integration_type v2 * fuck * don't explicitly use integration const * mention integrationType in docs --- _examples/oauth2/example.go | 6 +++++- _examples/verified_roles/main.go | 6 +++++- oauth2/client.go | 17 +++++++++++++---- oauth2/client_impl.go | 26 +++++++++++++------------- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/_examples/oauth2/example.go b/_examples/oauth2/example.go index 29687fa3..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, 0, 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 d3ef5e95..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, 0, 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 4c826baa..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, integrationType & 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, integrationType & 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 232e3070..9e050607 100644 --- a/oauth2/client_impl.go +++ b/oauth2/client_impl.go @@ -47,32 +47,32 @@ 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, + "redirect_uri": params.RedirectURI, "response_type": "code", - "scope": discord.JoinScopes(scopes), + "scope": discord.JoinScopes(params.Scopes), "state": state, } - if permissions != discord.PermissionsNone { - values["permissions"] = permissions + if params.Permissions != discord.PermissionsNone { + values["permissions"] = params.Permissions } - if guildID != 0 { - values["guild_id"] = guildID + if params.GuildID != 0 { + values["guild_id"] = params.GuildID } - if disableGuildSelect { + if params.DisableGuildSelect { values["disable_guild_select"] = true } - if integrationType != 0 { - values["integration_type"] = integrationType + if params.IntegrationType != 0 { + values["integration_type"] = params.IntegrationType } return discord.AuthorizeURL(values), state }