Skip to content

Commit

Permalink
Faster OAuthURL Generation
Browse files Browse the repository at this point in the history
  • Loading branch information
realTristan committed Sep 2, 2022
1 parent de18796 commit d1eb6f2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
var dc *discord.DiscordClient = discord.Init(&discord.DiscordClient{
ClientID: "CLIENT ID",
ClientSecret: "CLIENT SECRET",
RedirectURI: "localhost:8000/redirect",
RedirectURI: "http://localhost:8000/redirect",
Scopes: []string{discord.ScopeIdentify},
})

Expand Down
34 changes: 22 additions & 12 deletions access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,32 @@ func (dc *DiscordClient) refreshAccessTokenBody(refreshToken string) *bytes.Buff
// The credentialsAccessTokenBody() function is used to return
// the request body bytes being used in the
// GetCredentialsAccessToken() function
//
// Using append() and a byte slice is much faster than
// using += to a string!
func credentialsAccessTokenBody(scopes []string) *bytes.Buffer {
var _url string = "grant_type=client_credentials&scope="
// For each of the scopes
for i := 0; i < len(scopes); i++ {
// Append the scope to the url
_url += scopes[i]

// If there are multiple scopes and the
// current index isn't the last scope
if i != len(scopes) {
// Append %20 (space)
_url += "%20"
var body []byte = []byte("grant_type=client_credentials")

// Check to make sure the user provided a
// valid amount of scopes
if len(scopes) > 0 {
body = append(body, "&scope="...)

// For each of the scopes
for i := 0; i < len(scopes); i++ {
// Append the scope to the url
body = append(body, scopes[i]...)

// If there are multiple scopes and the
// current index isn't the last scope
if i != len(scopes)+1 {
// Append %20 (space)
body = append(body, "%20"...)
}
}
}
// Return the url bytes
return bytes.NewBuffer([]byte(_url))
return bytes.NewBuffer(body)
}

// The accessTokenRequestObject() function is used to establish
Expand Down
21 changes: 13 additions & 8 deletions discord_client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package disgoauth

// Import net/http
import "net/http"
import (
"net/http"
)

// Request Client for sending http requests
var RequestClient *http.Client = &http.Client{}
Expand Down Expand Up @@ -56,23 +58,26 @@ func (dc *DiscordClient) checkStructErrors() {
// the provided scopes to the OAuth URL. This function
// is called from the InitOAuthURL() function and is
// only ran if the number of provided scopes is valid.
func (dc *DiscordClient) appendScopes(url string) string {
//
// Using append() and a byte slice is much faster than
// using += to a string!
func (dc *DiscordClient) appendScopes(url []byte) string {
// Append the initial parameter name (scope)
url += "&scope="
url = append(url, "&scope="...)

// For each of the discord client's scopes
for i := 0; i < len(dc.Scopes); i++ {
// Append the scope to the OAuth URL
url += dc.Scopes[i]
url = append(url, dc.Scopes[i]...)

// If there are multiple scopes and the
// current index isn't the last scope
if i != len(dc.Scopes) {
if i != len(dc.Scopes)-1 {
// Append %20 (space)
url += "%20"
url = append(url, "%20"...)
}
}
return url
return string(url)
}

// The initOAuthURL() function is used to initialize
Expand All @@ -90,7 +95,7 @@ func (dc *DiscordClient) initOAuthURL() string {
// If user provided scopes
if len(dc.Scopes) > 0 {
// Append the scopes to the OAuth URL
tempUrl = dc.appendScopes(tempUrl) // discord_client.go (this file)
tempUrl = dc.appendScopes([]byte(tempUrl)) // discord_client.go (this file)
}
return tempUrl
}
Expand Down
8 changes: 4 additions & 4 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
func main() {
// Establish a new discord client
var dc *discord.DiscordClient = discord.Init(&discord.DiscordClient{
ClientID: "CLIENT ID",
ClientSecret: "CLIENT SECRET",
RedirectURI: "localhost:8000/redirect",
ClientID: "883006609280864257",
ClientSecret: "X-9n0rEBywVu1KKKOQSHskRQM7L8UlOV",
RedirectURI: "http://localhost:8000/redirect",
Scopes: []string{discord.ScopeIdentify},
})

Expand Down Expand Up @@ -58,7 +58,7 @@ func main() {
userData, _ = discord.GetUserData(accessToken)
)
// Print the user data map
fmt.Println(userData)
fmt.Fprint(w, userData)
})

// Listen and Serve to the incoming http requests
Expand Down
6 changes: 3 additions & 3 deletions implicit.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ func (dc *DiscordClient) implicitOAuth() string {
func (dc *DiscordClient) nonImplicitOAuth() string {
// Establish the prompt parameter
var prompt string = dc.Prompt
if len(dc.Prompt) < 1 {
prompt = "none"
if len(dc.Prompt) > 0 {
prompt = "&prompt=" + dc.Prompt
}

// Return the OAuth URL to a formatted string
// that contains the client id, redirect uri,
// and response type.
return fmt.Sprintf(
"https://discord.com/api/oauth2/authorize?client_id=%s&redirect_uri=%s&response_type=code&prompt=%s",
"https://discord.com/api/oauth2/authorize?client_id=%s&redirect_uri=%s&response_type=code%s",
dc.ClientID,
dc.RedirectURI,
prompt,
Expand Down

0 comments on commit d1eb6f2

Please sign in to comment.