oauth2

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2019 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ProviderList

func ProviderList() []string

ProviderList returns the names of all registered provider

func RegisterProvider

func RegisterProvider(p Provider)

RegisterProvider an Oauth provider

func StartFlow

func StartFlow(cfg Config, w http.ResponseWriter)

StartFlow by redirecting the user to the login provider. A state parameter to protect against cross-site request forgery attacks is randomly generated and stored in a cookie

func UnRegisterProvider

func UnRegisterProvider(name string)

UnRegisterProvider removes a provider

Types

type Config

type Config struct {
	// ClientID is the application's ID.
	ClientID string

	// ClientSecret is the application's secret.
	ClientSecret string

	// The oauth authentication url to redirect to
	AuthURL string

	// The url for token exchange
	TokenURL string

	// RedirectURL is the URL to redirect users going through
	// the OAuth flow, after the resource owner's URLs.
	RedirectURI string

	// Scope specifies optional requested permissions, this is a *space* separated list.
	Scope string

	// The oauth provider
	Provider Provider
}

Config describes a typical 3-legged OAuth2 flow, with both the client application information and the server's endpoint URLs.

type GithubUser

type GithubUser struct {
	Login     string `json:"login,omitempty"`
	AvatarURL string `json:"avatar_url,omitempty"`
	Name      string `json:"name,omitempty"`
	Email     string `json:"email,omitempty"`
}

GithubUser is used for parsing the github response

type GitlabGroup added in v1.3.0

type GitlabGroup struct {
	FullPath string `json:"full_path,omitempty"`
}

type GitlabUser added in v1.3.0

type GitlabUser struct {
	Username  string `json:"username,omitempty"`
	AvatarURL string `json:"avatar_url,omitempty"`
	Name      string `json:"name,omitempty"`
	Email     string `json:"email,omitempty"`
}

GitlabUser is used for parsing the gitlab response

type GoogleUser added in v1.1.0

type GoogleUser struct {
	Name               string `json:"name"`
	Email              string `json:"email"`
	EmailVerified      bool   `json:"email_verified"`
	Picture            string `json:"picture"`
	HostedGsuiteDomain string `json:"hd"`
}

type JSONError

type JSONError struct {
	Error string `json:"error"`
}

JSONError represents an oauth error response in json form.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager has the responsibility to handle the user user requests in an oauth flow. It has to pick the right configuration and start the oauth redirecting.

func NewManager

func NewManager() *Manager

NewManager creates a new Manager

func (*Manager) AddConfig

func (manager *Manager) AddConfig(providerName string, opts map[string]string) error

AddConfig for a provider

func (*Manager) GetConfigFromRequest

func (manager *Manager) GetConfigFromRequest(r *http.Request) (Config, error)

GetConfigFromRequest returns the oauth configuration matching the current path. The configuration name is taken from the last path segment.

func (*Manager) GetConfigs

func (manager *Manager) GetConfigs() map[string]Config

GetConfigs of the manager

func (*Manager) Handle

func (manager *Manager) Handle(w http.ResponseWriter, r *http.Request) (
	startedFlow bool,
	authenticated bool,
	userInfo model.UserInfo,
	err error)

Handle is managing the oauth flow. Dependent on the code parameter of the url, the oauth flow is started or the call is interpreted as the redirect callback and the token exchange is done. Return parameters:

startedFlow - true, if this was the initial call to start the oauth flow
authenticated - if the authentication was successful or not
userInfo - the user info from the provider in case of a successful authentication
err - an error

type Provider

type Provider struct {
	// The name to access the provider in the configuration
	Name string

	// The oauth authentication url to redirect to
	AuthURL string

	// The url for token exchange
	TokenURL string

	// Default Scopes is a space separated list of oauth scopes to use for this provider.
	// This list can be overwritten by configuration.
	DefaultScopes string

	// GetUserInfo is a provider specific Implementation
	// for fetching the user information.
	// Possible keys in the returned map are:
	// username, email, name
	GetUserInfo func(token TokenInfo) (u model.UserInfo, rawUserJson string, err error)
}

Provider is the description of an oauth provider adapter

func GetProvider

func GetProvider(providerName string) (Provider, bool)

GetProvider returns a provider

type TokenInfo

type TokenInfo struct {
	// AccessToken is the token that authorizes and authenticates
	// the requests.
	AccessToken string `json:"access_token"`

	// TokenType is the type of token.
	TokenType string `json:"token_type,omitempty"`

	// The scopes for this tolen
	Scope string `json:"scope,omitempty"`
}

TokenInfo represents the credentials used to authorize the requests to access protected resources on the OAuth 2.0 provider's backend.

func Authenticate

func Authenticate(cfg Config, r *http.Request) (TokenInfo, error)

Authenticate after coming back from the oauth flow. Verify the state parameter againt the state cookie from the request.

Jump to

Keyboard shortcuts

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