Skip to content

Commit

Permalink
Refactor oauth params into a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
sebm253 committed Mar 20, 2024
1 parent 4aabae4 commit 0339d2d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
6 changes: 5 additions & 1 deletion _examples/oauth2/example.go
Expand Up @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion _examples/verified_roles/main.go
Expand Up @@ -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) {
Expand Down
17 changes: 13 additions & 4 deletions oauth2/client.go
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
36 changes: 16 additions & 20 deletions oauth2/client_impl.go
Expand Up @@ -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
}
Expand Down

0 comments on commit 0339d2d

Please sign in to comment.