Skip to content

Commit

Permalink
Refactor oauth params into a struct (#345)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sebm253 committed Apr 7, 2024
1 parent 7039b07 commit d861eea
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 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, 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
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
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
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

0 comments on commit d861eea

Please sign in to comment.