oauth2

package module
v0.0.0-...-3d8b443 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2021 License: MIT Imports: 14 Imported by: 10

README

go.oauth2

loc license discord goreportcard codefactor

HTTP function handlers to easily add OAuth2 client support to your Go application

AppConf Schema

type AppConf struct {
	For    string `json:"for"`
	ID     string `json:"id"`
	Secret string `json:"secret"`
	Extra1 string `json:"extra_1"`
	Extra2 string `json:"extra_2"`
	Extra3 string `json:"extra_3"`
}
  • "for" is the short-code this config refers to.
  • "id" is your Client ID.
  • "secret" is for your Client Secret.
  • Extra 1, 2, and 3 are filler spots for misc. info your app may need, such as Discord's Bot Token for example.

Creating Credentials

In order to use an app that uses this library, you will need to create an app on your Identity Provider of choice. Below, you will see a table of the supported Identity Providers and a link to the respective dashboards where you can go to create your app and obtain your App ID and App Secret.

Identity Provider Short Code Developer Dashboard
Amazon amazon https://developer.amazon.com/settings/console/securityprofile/overview.html
Battle.net battle.net https://develop.battle.net/access/clients
Discord discord https://discordapp.com/developers/applications/
Facebook facebook https://developers.facebook.com/apps/
GitHub github https://github.com/settings/developers
GitLab gitlab.com https://gitlab.com/profile/applications
Google google https://console.developers.google.com
Microsoft microsoft https://apps.dev.microsoft.com/
Reddit reddit https://www.reddit.com/prefs/apps

Installing

$ go get -v github.com/nektro/go.oauth2

Provider Schema

type Provider struct {
	ID           string `json:"id"`
	AuthorizeURL string `json:"authorize_url"`
	TokenURL     string `json:"token_url"`
	MeURL        string `json:"me_url"`
	Scope        string `json:"scope"`
	NameProp     string `json:"name_prop"`
	NamePrefix   string `json:"name_prefix"`
	Logo         string `json:"logo"`
	Color        string `json:"color"`
	Customable   string `json:"customable"`
}
  • "id" is the short-code this is creating.
  • "authorize_url" is the OAuth2 authorization URL.
  • "token_url" is the OAuth2 token URL.
  • "me_url" is the service's URL to get the currently logged in user.
  • "scope" is the OAuth2 scope required to be able to get the currently logged in user.
  • "name_prop" is the JSON key of current user's real name in the response of fetching "me_url".
  • "name_prefix" is any prefix to put in front of all names, this is typically @, u/, blank, etc.

AppConf Details for Self-Hosted Services

There are also a number of providers that allow you to specify a custom domain for that provider. They are accessed as such:

...
"clients": [
	{
		"for": "{provider_id},{domain}",
		"id": "",
		"secret": ""
	}
],
...

So for example, if adding a login config for https://mastodon.social/, your "for" key would be "mastodon,mastodon.social"

The full list of customizable provider are as follows:

Identity Provider Short Code Home Site
Gitea gitea https://gitea.io/en-us/
Gitlab gitlab https://about.gitlab.com/
mastodon mastodon https://joinmastodon.org/
pleroma pleroma https://pleroma.social/

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ProviderIDMap = map[string]Provider{
		"amazon": Provider{
			ID:           "amazon",
			AuthorizeURL: "https://www.amazon.com/ap/oa",
			TokenURL:     "https://api.amazon.com/auth/o2/token",
			MeURL:        "https://api.amazon.com/user/profile",
			Scope:        "profile",
			NameProp:     "name",
			IDProp:       "user_id",
			Logo:         "https://unpkg.com/simple-icons@latest/icons/amazon.svg",
			Color:        "#FF9900",
		},
		"battle.net": Provider{
			ID:           "battle.net",
			AuthorizeURL: "https://us.battle.net/oauth/authorize",
			TokenURL:     "https://us.battle.net/oauth/token",
			MeURL:        "https://us.battle.net/oauth/userinfo",
			Scope:        "openid",
			NameProp:     "battletag",
			Logo:         "https://unpkg.com/simple-icons@latest/icons/battle-dot-net.svg",
			Color:        "#00AEFF",
		},
		"discord": Provider{
			ID:           "discord",
			AuthorizeURL: "https://discordapp.com/api/oauth2/authorize",
			TokenURL:     "https://discordapp.com/api/oauth2/token",
			MeURL:        "https://discordapp.com/api/users/@me",
			Scope:        "identify",
			NameProp:     "username",
			NamePrefix:   "@",
			Logo:         "https://unpkg.com/simple-icons@latest/icons/discord.svg",
			Color:        "#7289DA",
		},
		"facebook": Provider{
			ID:           "facebook",
			AuthorizeURL: "https://graph.facebook.com/oauth/authorize",
			TokenURL:     "https://graph.facebook.com/oauth/access_token",
			MeURL:        "https://graph.facebook.com/me",
			NameProp:     "name",
			Logo:         "https://unpkg.com/simple-icons@latest/icons/facebook.svg",
			Color:        "#1877F2",
		},
		"github": Provider{
			ID:           "github",
			AuthorizeURL: "https://github.com/login/oauth/authorize",
			TokenURL:     "https://github.com/login/oauth/access_token",
			MeURL:        "https://api.github.com/user",
			Scope:        "read:user",
			NameProp:     "login",
			NamePrefix:   "@",
			Logo:         "https://unpkg.com/simple-icons@latest/icons/github.svg",
			Color:        "#181717",
		},
		"gitlab.com": Provider{
			ID:           "gitlab.com",
			AuthorizeURL: "https://gitlab.com/oauth/authorize",
			TokenURL:     "https://gitlab.com/oauth/token",
			MeURL:        "https://gitlab.com/api/v4/user",
			Scope:        "read_user",
			NameProp:     "username",
			NamePrefix:   "@",
			Logo:         "https://unpkg.com/simple-icons@latest/icons/gitlab.svg",
			Color:        "#FCA121",
		},
		"google": Provider{
			ID:           "google",
			AuthorizeURL: "https://accounts.google.com/o/oauth2/v2/auth",
			TokenURL:     "https://www.googleapis.com/oauth2/v4/token",
			MeURL:        "https://www.googleapis.com/oauth2/v1/userinfo?alt=json",
			Scope:        "profile",
			NameProp:     "name",
			Logo:         "https://unpkg.com/simple-icons@latest/icons/google.svg",
			Color:        "#4285F4",
		},
		"microsoft": Provider{
			ID:           "microsoft",
			AuthorizeURL: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
			TokenURL:     "https://login.microsoftonline.com/common/oauth2/v2.0/token",
			MeURL:        "https://graph.microsoft.com/v1.0/me/",
			Scope:        "https://graph.microsoft.com/user.read",
			NameProp:     "displayName",
			Logo:         "https://unpkg.com/simple-icons@latest/icons/microsoft.svg",
			Color:        "#666666",
		},
		"reddit": Provider{
			ID:           "reddit",
			AuthorizeURL: "https://old.reddit.com/api/v1/authorize",
			TokenURL:     "https://old.reddit.com/api/v1/access_token",
			MeURL:        "https://oauth.reddit.com/api/v1/me",
			Scope:        "identity",
			NameProp:     "name",
			NamePrefix:   "u/",
			Logo:         "https://unpkg.com/simple-icons@latest/icons/reddit.svg",
			Color:        "#FF4500",
		},

		"_gitea": Provider{
			ID:           "gitea",
			AuthorizeURL: "https://{domain}/login/oauth/authorize",
			TokenURL:     "https://{domain}/login/oauth/access_token",
			MeURL:        "https://{domain}/api/v1/user",
			NameProp:     "username",
			NamePrefix:   "@",
			Logo:         "https://unpkg.com/simple-icons@latest/icons/gitea.svg",
			Color:        "#609926",
		},
		"_gitlab": Provider{
			ID:           "gitlab",
			AuthorizeURL: "https://{domain}/oauth/authorize",
			TokenURL:     "https://{domain}/oauth/token",
			MeURL:        "https://{domain}/api/v4/user",
			Scope:        "read_user",
			NameProp:     "username",
			NamePrefix:   "@",
			Logo:         "https://unpkg.com/simple-icons@latest/icons/gitlab.svg",
			Color:        "#FCA121",
		},
		"_mastodon": Provider{
			ID:           "mastodon",
			AuthorizeURL: "https://{domain}/oauth/authorize",
			TokenURL:     "https://{domain}/oauth/token",
			MeURL:        "https://{domain}/api/v1/accounts/verify_credentials",
			Scope:        "read:accounts",
			NameProp:     "username",
			NamePrefix:   "@",
			Logo:         "https://unpkg.com/simple-icons@latest/icons/mastodon.svg",
			Color:        "#3088D4",
		},
		"_pleroma": Provider{
			ID:           "pleroma",
			AuthorizeURL: "https://{domain}/oauth/authorize",
			TokenURL:     "https://{domain}/oauth/token",
			MeURL:        "https://{domain}/api/v1/accounts/verify_credentials",
			Scope:        "read:accounts",
			NameProp:     "username",
			NamePrefix:   "@",
			Logo:         "https://unpkg.com/simple-icons@latest/icons/pleroma.svg",
			Color:        "#FBA457",
		},
	}
)

Functions

func GetHandlers

func GetHandlers(isLoggedIn func(*http.Request) bool, doneURL, callbackPath string, clients *[]AppConf, saveInfo SaveInfoFunc) (http.HandlerFunc, http.HandlerFunc)

func HandleMultiOAuthCallback

func HandleMultiOAuthCallback(doneURL string, clients []AppConf, saveInfo SaveInfoFunc, callbackPath string) http.HandlerFunc

func HandleMultiOAuthLogin

func HandleMultiOAuthLogin(isLoggedIn func(*http.Request) bool, doneURL string, clients []AppConf, callbackPath string) http.HandlerFunc

func HandleOAuthCallback

func HandleOAuthCallback(idp Provider, appID, appSecret string, saveInfo SaveInfoFunc, doneURL, callbackPath string) http.HandlerFunc

func HandleOAuthLogin

func HandleOAuthLogin(isLoggedIn func(*http.Request) bool, doneURL string, idp Provider, appID, callbackPath string) http.HandlerFunc

Types

type AppConf

type AppConf struct {
	For    string `json:"for"`
	ID     string `json:"id"`
	Secret string `json:"secret"`
	Extra1 string `json:"extra_1"`
	Extra2 string `json:"extra_2"`
	Extra3 string `json:"extra_3"`
}

type Provider

type Provider struct {
	ID           string `json:"id"`
	AuthorizeURL string `json:"authorize_url"`
	TokenURL     string `json:"token_url"`
	MeURL        string `json:"me_url"`
	Scope        string `json:"scope"`
	NameProp     string `json:"name_prop"`
	NamePrefix   string `json:"name_prefix"`
	IDProp       string `json:"id_prop"`
	Color        string `json:"color"`
}

type SaveInfoFunc

type SaveInfoFunc func(req http.ResponseWriter, res *http.Request, provider string, id string, name string, resp map[string]interface{})

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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