auth

package module
v0.0.0-...-5ebfe49 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2020 License: BSD-3-Clause Imports: 18 Imported by: 1

README

auth-go

GoDoc

Authentication client application for people or bot.

id: string
pseudo: string
email: string
level: enum { no, candidate, visitor, standard, administrator }
bot: bool
teams: []string
go get -v -u github.com/Arveto/auth-go

Provider

URL Comments
/auth?app={APP}&r={REDIRECT} Gen teken and redirect user to /login?jwt=$JWT
/avatar Get the user's avatar (picture)
/publickey Get in PEM the provider's public key

Client Application

URL Comments
/login?r={REDIRECT} Redirect the user to the provider
/login?jwt={JWT}&r={REDIRECT} Save the tocken into a cookie
/avatar?u={ID} Redirect to the provider to get the avatar
/forget?jwt={JWT} (doit être un administrateur) Remove a user (must be an administrator) (DELETE method)
/me Get connected user information and expiration date (JSON)

The token can be in a cookie or in a HTTP header: Authorization: Bearer <JWT>

Documentation

Index

Constants

View Source
const (
	LevelNo            = iota
	LevelCandidate     = iota
	LevelVisitor       = iota
	LevelStandard      = iota
	LevelAdministrator = iota
)

Variables

View Source
var (
	ErrNotLogged = errors.New("You are not logged")
	ErrLowLevel  = errors.New("Your level is too low")
)
View Source
var (
	JWTNeedUserField   = errors.New("User need an ID, a Pseudo and an email")
	JWTEmpty           = errors.New("This JWT is empty")
	JWTOutDate         = errors.New("This JWT is out date")
	JWTWrongAudience   = errors.New("This JWT is made for an other audience")
	JWTWrongHead       = errors.New("JWT wrong head")
	JWTWrongSyntax     = errors.New("JWT wrong syntax")
	JWTWrongSyntaxHead = errors.New("JWT wrong syntax in head")
)
View Source
var UserLevelUnknown = errors.New("Unknown UserLevel")

Functions

func ErrorResponseDefault

func ErrorResponseDefault(w http.ResponseWriter, _ *http.Request, err error, code int)

A simple default ErrorResponse, it's a binding of http.Error.

func MarchalJWT

func MarchalJWT(key *rsa.PrivateKey, aud string, u *User) (string, error)

Create a new JWT to a specific audience.

Types

type App

type App struct {
	Key      *rsa.PublicKey // The public key of the auth provider.
	Audience string         // The audience claim in JWT.
	Cookie   string         // JWT cookie name. (by default it's "auth")
	Error    ErrorResponse  // Send error response.
	Mux      http.ServeMux  // Used direcly to handle no identification request
	Forget   func(u *User)  // Forget a user. Can be nil.
}

A client app, must be check in the provider. Fill all fields exept Forget.

func NewApp

func NewApp(id, provider string) (*App, error)

Get the public key of the providers, and fill Mux with standars handlers.

func (*App) Handle

func (a *App) Handle(pattern string, level UserLevel, handler Handler)

Wrap the handler with a user level checker: if the level is lower than level, return error and the handler is not call.

func (*App) HandleFunc

func (a *App) HandleFunc(pattern string, level UserLevel, handler func(http.ResponseWriter, *Request))

func (*App) ServeHTTP

func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)

type ErrorResponse

type ErrorResponse func(http.ResponseWriter, *http.Request, error, int)

Variant of http.Error function.

type Handler

type Handler interface {
	ServeHTTP(http.ResponseWriter, *Request)
}

Like http.handler but with a auth.Request.

type HandlerFunc

type HandlerFunc func(http.ResponseWriter, *Request)

func (HandlerFunc) ServeHTTP

func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *Request)

type Provider

type Provider struct {
	K   *rsa.PrivateKey
	PEM []byte
}

func NewProvier

func NewProvier(keyPath string) (*Provider, error)

func (*Provider) JWT

func (p *Provider) JWT(aud string, u *User) (string, error)

Return the JWT signed with the provider key.

func (*Provider) ServerKey

func (p *Provider) ServerKey(w http.ResponseWriter, r *http.Request)

type Request

type Request struct {
	http.Request
	// The User can be nil if the handler UserLevel is set to LevelNo, else it
	// is never nil.
	User *User
}

An HTTP request with the User. It compose with standard http.Request, so you can access with all method and field simply.

func (*Request) R

func (r *Request) R() *http.Request

The HTTP request to use when you need standard http.Request.

type Teams

type Teams map[string]bool

The user's teams. To save in JSON, it convert to an array.

func (Teams) MarshalJSON

func (t Teams) MarshalJSON() ([]byte, error)

func (*Teams) UnmarshalJSON

func (t *Teams) UnmarshalJSON(data []byte) error

type User

type User struct {
	ID     string    `json:"id"`
	Pseudo string    `json:"pseudo"`
	Email  string    `json:"email"`
	Level  UserLevel `json:"level"`
	Bot    bool      `json:"bot"`
	Teams  Teams     `json:"teams"`
}

One user

func UnmarshalJWT

func UnmarshalJWT(key *rsa.PublicKey, aud string, j string) (*User, error)

Parse the JWT from provider and return the user.

type UserLevel

type UserLevel uint

The user's or bot's accreditation level.

func (UserLevel) MarshalText

func (l UserLevel) MarshalText() ([]byte, error)

func (UserLevel) String

func (l UserLevel) String() string

func (*UserLevel) UnmarshalText

func (l *UserLevel) UnmarshalText(text []byte) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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