Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor oauth params into a struct #345

Merged
merged 16 commits into from
Apr 7, 2024
6 changes: 5 additions & 1 deletion _examples/oauth2/example.go
Original file line number Diff line number Diff line change
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, 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) {
Expand Down
6 changes: 5 additions & 1 deletion _examples/verified_roles/main.go
Original file line number Diff line number Diff line change
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, 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) {
Expand Down
17 changes: 13 additions & 4 deletions oauth2/client.go
Original file line number Diff line number Diff line change
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, 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)
Expand Down
26 changes: 13 additions & 13 deletions oauth2/client_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading