server

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2020 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package server provides a basic in-memory OAuth2 authentication server intended for testing purposes. The implementation may be used to as a reference or template to build a custom OAuth2 authentication server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustHash

func MustHash(clear string) []byte

MustHash will hash the specified clear text using bcrypt.

func SameHash

func SameHash(hash []byte, clear string) bool

SameHash verifies if the provided clear text and bcrypt hash are equal.

Types

type Config

type Config struct {
	Secret                    []byte
	KeyLength                 int
	AllowedScope              oauth2.Scope
	AccessTokenLifespan       time.Duration
	RefreshTokenLifespan      time.Duration
	AuthorizationCodeLifespan time.Duration
}

Config is used to configure a server.

func Default

func Default(secret []byte, allowed oauth2.Scope) Config

Default will return a default configuration.

func (Config) MustGenerate

func (c Config) MustGenerate() *hmacsha.Token

MustGenerate will generate a new token.

type Credential

type Credential struct {
	ClientID    string
	Username    string
	Signature   string
	ExpiresAt   time.Time
	Scope       oauth2.Scope
	RedirectURI string
	Code        string
	Used        bool
}

Credential represents an access token, refresh token or authorization code.

type Entity

type Entity struct {
	ID           string
	Secret       []byte
	RedirectURI  string
	Confidential bool
}

Entity represents a client or resource owner.

type Server

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

Server implements a basic in-memory OAuth2 authentication server intended for testing purposes.

func New

func New(config Config) *Server

New creates and returns a new server.

func (*Server) AddAccessToken

func (s *Server) AddAccessToken(token *Credential)

AddAccessToken will add the provided access token.

func (*Server) AddAuthorizationCode

func (s *Server) AddAuthorizationCode(code *Credential)

AddAuthorizationCode will add the provided authorization code.

func (*Server) AddClient

func (s *Server) AddClient(client *Entity)

AddClient will add the provided client.

func (*Server) AddRefreshToken

func (s *Server) AddRefreshToken(token *Credential)

AddRefreshToken will add the provided refresh token.

func (*Server) AddUser

func (s *Server) AddUser(user *Entity)

AddUser will add the provided user.

func (*Server) Authorize

func (s *Server) Authorize(w http.ResponseWriter, r *http.Request, required oauth2.Scope) bool

Authorize will authorize the request and require a valid access token. An error has already be written to the client if false is returned.

func (*Server) Config

func (s *Server) Config() Config

Config will return the used config.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP will handle the provided request based on the last path segment of the request URL.

Jump to

Keyboard shortcuts

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