public

package
v0.0.0-...-b2296ca Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2020 License: BSD-3-Clause Imports: 16 Imported by: 0

README

Public version 2

GoDoc

go get -u github.com/Arveto/arvetoAuth/pkg/public2

The data generated by the version 1 and 2 are compatible, difference is in the design of this package.

Provider

His mission is to create JWT. The external app must check the signature of JWT with the public key, rechable with provider.PubHTTP HTTP handler.

	http.HandleFunc("/publickey", provider.PubHTTP)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	JWTWrongSyntax     = errors.New("JWT wrong syntax")
	JWTWrongSyntaxHead = errors.New("JWT wrong syntax in head")
	JWTWrongHead       = errors.New("JWT wrong head")
	JWTWrongAudience   = errors.New("This JWT is made for an other audience")
	JWTOutDate         = errors.New("This JWT is out date")
	JWTEmpty           = errors.New("JWT is empty")
)

Functions

This section is empty.

Types

type App

type App struct {
	// The address of the provider
	Provider string
	// The public key of the auth provider
	PublicKey *rsa.PublicKey
	// The audience field in JWT.
	Audience string
	// The name of the cookie who store the JWT
	// by default it's `auth`
	Cookie string
	//
	Mux http.ServeMux
	// A variant of http.Error used to send the error to the client. By default
	// its a binding of http.Error.
	Error func(w http.ResponseWriter, r *http.Request, err string, code int)
}

The application information. Fill all fields before use it.

func NewApp

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

Create a new app. id is the the id of the this application in the provider. provider is the url of this provider.

defaultHandler registers the handler for login and logout

func (*App) Call

func (a *App) Call(w http.ResponseWriter, r *http.Request, level UserLevel, handler Handler)

Call handler if the user who make the request has a enought level. If the level is less than LevelCandidate, the user can be nil.

func (*App) CallFunc

func (a *App) CallFunc(w http.ResponseWriter, r *http.Request, level UserLevel, f func(w http.ResponseWriter, r *Request))

Like App.Call but for a func(w http.ResponseWriter, r *Request)

func (*App) FromJWT

func (a *App) FromJWT(j string) (*UserInfo, error)

func (*App) Handle

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

Add a handler to a.Mux. The level of the user must be over level.

If the level is strict strict less than LevelCandidate, the user can be nil or with a lower level.

func (*App) HandleFunc

func (a *App) HandleFunc(p string, l UserLevel, f func(w http.ResponseWriter, r *Request))

Like App.Handle with a function inplace of a Handler.

func (*App) Login

func (a *App) Login(destination string) http.HandlerFunc

Return a handler for the login. This handler takes the jwt from the URL and save it a cookie. Finaly it redirect the client to the params r or destination.

func (*App) Logout

func (a *App) Logout(to string) http.HandlerFunc

Remove the cookie and redirect the client to the variable to (or "/" if empty)

func (*App) ProviderAuth

func (a *App) ProviderAuth(r string) string

Return the address top the provider to auth. r is the address to go after autentification work.

func (*App) ServeHTTP

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

Binding of a.Mux.ServeHTTP

func (*App) User

func (a *App) User(r *http.Request) *UserInfo

Get the user from a request cookie. If error occure, the function return nil. If you want the error details, use App.FromJWT inplace.

type ExternProvider

type ExternProvider func(r *http.Request) (*UserInfo, error)

Manage request from login by a provider

type HandleFunc

type HandleFunc func(w http.ResponseWriter, r *Request)

HandleFunc with Request inplace of http.Request

func (HandleFunc) ServeHTTP

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

type Handler

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

HTTP.Handler with the custom request

type Provider

type Provider struct {
	PrivKey *rsa.PrivateKey
	Pub     []byte // the PEM public key encoded
}

The information about the providers,

func NewProvider

func NewProvider(keyFile string) (*Provider, error)

Create a new app. id is the the id of the this application in the provider. serv is the url of this provider.

func (*Provider) CreateJWT

func (p *Provider) CreateJWT(u *UserInfo, audience string) (string, error)

Create a JWT for the user u to a specific audience.

func (*Provider) PubHTTP

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

type Request

type Request struct {
	http.Request
	User *UserInfo
}

Custom request. The user can be nil.

type UserInfo

type UserInfo struct {
	ID     string    `json:"id"`
	Pseudo string    `json:"pseudo"`
	Email  string    `json:"email"`
	Avatar string    `json:"avatar"`
	Level  UserLevel `json:"level"`
}

Public informations, send to web page.

type UserLevel

type UserLevel int
const (
	LevelCandidate UserLevel = iota
	LevelVisitor   UserLevel = iota
	LevelStd       UserLevel = iota
	LevelAdmin     UserLevel = iota
	LevelBan       UserLevel = -1
)

func (UserLevel) MarshalText

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

func (UserLevel) String

func (ul 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