DisGOAuth

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2022 License: MIT Imports: 4 Imported by: 0

README

DisGOAuth Stars Watchers

banner

About

  • DisGOAuth is a light-weight, fast and easy-to-use module that makes using Discord's OAuth2.0 much easier. DisGOAuth uses solely native golang packages which makes it fast and secure.

Installation

go get -u github.com/realTristan/DisGOAuth

Quick Usage


package main

// Import Packages
import (
	"fmt"
	"net/http"

	// Import DisGOAuth
	discord "github.com/realTristan/DisGOAuth"
)

// Main function
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",
		Scopes:       []string{discord.ScopeIdentify},
	})

	// Home Page Handler
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// Send the user to the discord authentication
		// website. This is where they authorize access.
		dc.RedirectHandler(w, r, "")
	})

	// The OAuth URL Redirect Uri
	http.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) {
		// Put this in the handler of the dc.RedirectURI
		// Define Variables
		var (
			// Get the code from the redirect parameters (&code=...)
			codeFromURLParamaters = r.URL.Query()["code"][0]

			// Get the access token using the above codeFromURLParamaters
			accessToken string = dc.GetAccessToken(codeFromURLParamaters)

			// Get the authorized user's data using the above accessToken
			userData map[string]interface{} = discord.GetUserData(accessToken)
		)
		// Print the user data map
		fmt.Println(userData)
	})

	// Listen and Serve to the incoming http requests
	http.ListenAndServe(":8000", nil)
}

License

MIT License

Copyright (c) 2022 Tristan Simpson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

View Source
const (
	// Non-Whitelist
	ScopeIdentify                   = "identify"
	ScopeBot                        = "bot"
	ScopeEmail                      = "email"
	ScopeGuilds                     = "guilds"
	ScopeGuildsJoin                 = "guilds.join"
	ScopeConnections                = "connections"
	ScopeGroupDMJoin                = "gdm.join"
	ScopeMessagesRead               = "messages.read"
	ScopeWebhookIncoming            = "webhook.Incoming"
	ScopeApplicationsBuildsRead     = "applications.builds.read"
	ScopeApplicationsStoreUpdate    = "applications.store.update"
	ScopeApplicationsEntitlements   = "applications.entitlements"
	ScopeApplicationsCommands       = "applications.commands"
	ScopeApplicationsCommandsUpdate = "applications.commands.update"

	// Whitelist Only
	ScopeRPC                      = "rpc"
	ScopeRPCAPI                   = "rpc.api"
	ScopeRPCNotificationsRead     = "rpc.notifications.read"
	ScopeApplicationsBuildsUpload = "applications.builds.upload"
	ScopeRelationshipsRead        = "relationships.read"
	ScopeActivitiesRead           = "activities.read"
	ScopeActivitiesWrite          = "activities.write"
)

Constant OAuth URL Scopes

Variables

View Source
var RequestClient *http.Client = &http.Client{}

Request Client for sending http requests

Functions

func GetUserData

func GetUserData(token string) map[string]interface{}

The GetUserData() function is used to send an api request to the discord/users/@me endpoint with the provided accessToken.

Types

type DiscordClient

type DiscordClient struct {
	ClientID     string
	ClientSecret string
	RedirectURI  string
	Scopes       []string
	OAuthURL     string
}

The DiscordClient struct contains five primary keys

ClientID: 	string 	 { "Your Application's Client ID" }
ClientSecret: 	string 	 { "Your Application's Client Secret" }
Scopes: 	[]string { "Your Application's Permission Scopes (REQUIRED)" }
RedirectURI: 	string 	 { "The Redirect URI (This is where you use the GetUserData functions)" }
OAuthURL: 	string 	 { "Your Application's OAuth URL (If none is provided, one will be generated for you)" }

func Init

func Init(dc *DiscordClient) *DiscordClient

The Init() function is used to initalize the required data for the discord oauth to work It panics if required parameters are missing from the provided DiscordClient struct

func (*DiscordClient) AppendScopes

func (dc *DiscordClient) AppendScopes()

The AppendScopes() function is used to append 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 (*DiscordClient) CheckStructErrors

func (dc *DiscordClient) CheckStructErrors()

The CheckStructErrors() function is used to check for any invalid / empty struct values that are required for the discord oauth to work.

func (*DiscordClient) GetAccessToken

func (dc *DiscordClient) GetAccessToken(code string) string

The GetAccessToken() function is used to send an api request to discord's oauth2/token endpoint. The function returns the token required for accessing the authorized users data

func (*DiscordClient) GetAccessTokenBody

func (dc *DiscordClient) GetAccessTokenBody(code string) *bytes.Buffer

The GetAccessTokenBody() function is used to return the request body bytes being used in the GetAccessToken() http request

func (*DiscordClient) InitOAuthURL

func (dc *DiscordClient) InitOAuthURL()

The InitOAuthURL() function is used to initialize a discord OAuth URL. This function is called from the Init() function and is only ran if there is no previously provided OAuth URL.

func (*DiscordClient) RedirectHandler

func (dc *DiscordClient) RedirectHandler(w http.ResponseWriter, r *http.Request, state string)

The RedirectHandler() function is used to redirect the user to the provided DiscordClient OAuth URL. If there is a provided state, it will add it to said OAuth URL

If using a state, base64encode the data beforehand, else, set the state to "" (length: 0)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL