oauth2

package
v0.0.0-...-fe628ba Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: AGPL-3.0 Imports: 20 Imported by: 1

Documentation

Index

Constants

View Source
const (
	MethodGitHub       = "github"
	MethodGoogle       = "google"
	MethodDigitalOcean = "digitalocean"
	MethodBitbucket    = "bitbucket"
)

Sign in methods

Variables

View Source
var GitHubEndpoint = oauth2.Endpoint{
	AuthURL:  "https://github.com/login/oauth/authorize",
	TokenURL: "https://github.com/login/oauth/access_token",
}

GitHubEndpoint is the oauth2 endpoint for GitHub.

View Source
var GoogleEndpoint = oauth2.Endpoint{
	AuthURL:   "https://accounts.google.com/o/oauth2/auth",
	TokenURL:  "https://oauth2.googleapis.com/token",
	AuthStyle: oauth2.AuthStyleInParams,
}

GoogleEndpoint is google's oauth2 endpoint. This is copied from golang.org/x/oauth2/google, to avoid dragging in useless dependencies such as protobuf.

Google engineers really should be more careful with their package dependency management.

Functions

func MapGitHub

func MapGitHub(c *aries.C, u *UserMeta, _ string) (string, error)

MapGitHub is a login check function that only allows github login. It maps the user ID directly from GitHub users.

func ParseRedirect

func ParseRedirect(redirect string) (string, error)

ParseRedirect parses an in-site redirection URL. The server parts (scheme, host, port, user info) are discarded.

func SingleAdmin

func SingleAdmin(admin string) func(user string) (interface{}, int, error)

SingleAdmin returns a user check function for a single admin.

Types

type App

type App struct {
	ID          string
	Secret      string
	RedirectURL string `json:",omitempty"`

	Scopes []string `json:",omitempty"`

	// Used only in GitHub OAuth2
	WithEmail bool `json:",omitempty"`

	// Used only in Google OAuth2
	WithProfile bool `json:",omitempty"`
}

App stores the configuration of a general oauth2 application.

type Client

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

Client is an oauth client for oauth2 exchanges.

func NewClient

func NewClient(
	c *oauth2.Config, states *signer.Sessions, m string,
) *Client

NewClient creates a new oauth client for oauth2 exchnages.

func (*Client) Method

func (c *Client) Method() string

Method returns the method class of this oauth2 client.

func (*Client) OfflineSignInURL

func (c *Client) OfflineSignInURL(s *State) string

OfflineSignInURL returns the offline signin URL for redirection.

func (*Client) SignInURL

func (c *Client) SignInURL(s *State) string

SignInURL returns the online signin URL for redirection.

func (*Client) TokenState

func (c *Client) TokenState(ctx *aries.C) (*oauth2.Token, *State, error)

TokenState extracts the oauth2 access token and state from the request.

type Config

type Config struct {
	GitHub       *App
	Google       *App
	DigitalOcean *App

	StateKey        []byte
	SessionKey      []byte
	SessionLifeTime time.Duration

	Bypass         string
	Redirect       string
	SignInRedirect string

	KeyRegistry keyreg.KeyRegistry

	// SignInCheck exchanges OAuth2 ID's for user ID.
	SignInCheck func(c *aries.C, u *UserMeta, purpose string) (string, error)

	// Check checks the user id and returns the user account structure.
	Check func(user string) (interface{}, int, error)

	PreSignOut func(c *aries.C) error
}

Config is a module configuration for a GitHub Oauth handling module.

type GoogleUserInfo

type GoogleUserInfo struct {
	Email string `json:"email"`
	Name  string `json:"name"`
}

GoogleUserInfo stores a Google user's basic personal info.

func GetGoogleUserInfo

func GetGoogleUserInfo(
	ctx context.Context, c *Client, tok *oauth2.Token,
) (*GoogleUserInfo, error)

GetGoogleUserInfo queries Google OAuth endpoint for user info data.

type JSONConfig

type JSONConfig struct {
	GitHub       *App
	Google       *App
	DigitalOcean *App
	StateKey     string
	SessionKey   string
	SignInBypass string
	PublicKeys   map[string]string
}

JSONConfig is a JSON marshallable config that is commonly used for setting up a server.

func (*JSONConfig) Config

func (c *JSONConfig) Config() *Config

Config converts a JSON marshallable config to Config.

func (*JSONConfig) SimpleGitHubConfig

func (c *JSONConfig) SimpleGitHubConfig() *Config

SimpleGitHubConfig converts a JSON marshallable config to Config that uses Github as the direct user ID mapping. Users that has a public key assigned in c.PublicKeys are defined as admin.

type Module

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

Module is a module that handles stuff related to oauth.

func NewModule

func NewModule(config *Config) *Module

NewModule creates a new oauth module with the given config.

func (*Module) Auth

func (m *Module) Auth() aries.Auth

Auth makes a aries.Auth that executes the oauth flow on the server side.

func (*Module) Methods

func (m *Module) Methods() []string

Methods returns the list of supported methods.

func (*Module) Serve

func (m *Module) Serve(c *aries.C) error

Serve serves the routes for signing in and callbacks.

func (*Module) Setup

func (m *Module) Setup(c *aries.C) error

Setup sets up the credentials for the request.

func (*Module) SetupCookie

func (m *Module) SetupCookie(c *aries.C, user string)

SetupCookie sets up the session gate's cookie.

func (*Module) SignIn

func (m *Module) SignIn(c *aries.C, method string, s *State) error

SignIn redirects the incoming request to a particular client's sign-in URL. If the client is not found, it redirects to default redirect page.

func (*Module) Token

func (m *Module) Token(user string, ttl time.Duration) *signin.Token

Token returns a new session token for user that expires in ttl.

type SignUp

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

SignUp is an HTTP module that handles user signups.

func NewSignUp

func NewSignUp(m *Module, c *SignUpConfig) *SignUp

NewSignUp creates a new sign up module.

func (*SignUp) Purpose

func (s *SignUp) Purpose() string

Purpose returns the purpose string.

func (*SignUp) Serve

func (s *SignUp) Serve(c *aries.C) error

Serve serves the incoming HTTP request.

type SignUpConfig

type SignUpConfig struct {
	Redirect string

	// Whether keep user signed in after signing up.
	SignIn bool
}

SignUpConfig is the config for creating a signup module.

type State

type State struct {
	// URL to redirect to after signing in.
	Dest string

	// Sign in purpose.
	Purpose string `json:",omitempty"`

	// Whether set cookie after signing in.
	NoCookie bool `json:",omitempty"`
}

State contains a JSON marshalable state for OAuth2 sign in.

type UserMeta

type UserMeta struct {
	Method string
	ID     string
	Name   string // Screen name.
	Email  string
}

UserMeta returns the meta data returned by a sign in.

Jump to

Keyboard shortcuts

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