auth

package
v0.0.0-...-b7287e2 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidPassword invalid passwoird
	ErrInvalidPassword    = errors.New(models.ErrInvalidPassword)
	ErrInvalidAccount     = errors.New(models.ErrInvalidAccount)
	ErrAccountOccupied    = errors.New(models.ErrOccupiedAccount)
	ErrNilAccount         = errors.New(models.ErrNilAccount)
	ErrInvalidOrgJoinCode = errors.New(models.ErrInvalidOrgJoinCode)
	ErrOrgUnactivated     = errors.New(models.ErrOrgUnActivated)
)
View Source
var DefaultRefreshTokenHandler = func(context *Context, refreshTokenHandler func(*Context) (*tokens.Token, error)) {
	var (
		w          = context.Writer
		token, err = refreshTokenHandler(context)
	)

	if err == nil && token != nil {
		apiResult := models.NewAPIResult(models.ErrNil, token, "refresh-token succeeded")
		models.SendJSON(w, apiResult)
	} else {

		if _, ok := models.ErrText[err.Error()]; ok {
			apiError := models.NewAPIError(err.Error(), "refresh-token failed with error: %v", models.ErrText[err.Error()])
			models.SendJSON(w, apiError)
		} else {

			apiError := models.NewHTTPError(http.StatusInternalServerError, models.ErrInternal, "internal server error: %v", err.Error())
			models.SendJSON(w, apiError)
		}
	}
}

DefaultRefreshTokenHandler default refresh token handler

View Source
var DefaultSignInHandler = func(context *Context, authenticateHandler func(*Context) (*tokens.Token, error)) {
	var (
		// req         = context.Request
		w          = context.Writer
		token, err = authenticateHandler(context)
	)

	if err == nil && token != nil {
		apiResult := models.NewAPIResult(models.ErrNil, token, "sign-in succeeded")
		models.SendJSON(w, apiResult)
	} else {

		if _, ok := models.ErrText[err.Error()]; ok {
			apiError := models.NewAPIError(err.Error(), "sign-in failed with error: %v", models.ErrText[err.Error()])
			models.SendJSON(w, apiError)
		} else {

			apiError := models.NewHTTPError(http.StatusInternalServerError, models.ErrInternal, "internal server error: %v", err.Error())
			models.SendJSON(w, apiError)
		}
	}
}

DefaultSignInHandler default sign-in behavior

View Source
var DefaultSignOutHandler = func(context *Context) {
}

DefaultSignOutHandler default sign-out behavior

View Source
var DefaultSignUpHandler = func(context *Context, registerHandler func(*Context) (*tokens.Token, error)) {
	var (
		w          = context.Writer
		token, err = registerHandler(context)
	)
	if err == nil && token != nil {
		apiResult := models.NewAPIResult(models.ErrNil, token, "sign-up succeeded!")
		models.SendJSON(w, apiResult)
	} else {

		if _, ok := models.ErrText[err.Error()]; ok {
			apiError := models.NewAPIError(err.Error(), "sign-up failed with error: %v", models.ErrText[err.Error()])
			models.SendJSON(w, apiError)
		} else {

			apiError := models.NewHTTPError(http.StatusInternalServerError, models.ErrInternal, "internal server error: %v", err.Error())
			models.SendJSON(w, apiError)
		}
	}
}

DefaultSignUpHandler default sign-up behavior

Functions

This section is empty.

Types

type Auth

type Auth struct {
	*Config
	// contains filtered or unexported fields
}

Auth authentication main struct

func New

func New(config *Config) *Auth

New create Auth inst

func (*Auth) GetProvider

func (auth *Auth) GetProvider(name string) Provider

GetProvider get provider with name

func (*Auth) GetProviders

func (auth *Auth) GetProviders() (providers []Provider)

GetProviders return registered providers

func (*Auth) RegisterProvider

func (auth *Auth) RegisterProvider(provider Provider)

RegisterProvider register auth provider

type Config

type Config struct {
	DB        *gorm.DB
	AuthModel interface{}
	URLPrefix string
	MachineID string
	Logger    *logger.AppLogger

	SignInHandler       func(*Context, func(*Context) (*tokens.Token, error))
	RefreshTokenHandler func(*Context, func(*Context) (*tokens.Token, error))
	SignUpHandler       func(*Context, func(*Context) (*tokens.Token, error))
	SignOutHandler      func(*Context)
}

Config the config of auth

type Context

type Context struct {
	*Auth
	Claims   *claims.Claims
	Provider Provider
	Request  *http.Request
	Writer   http.ResponseWriter
}

Context the auth context

type Provider

type Provider interface {
	GetName() string

	ConfigAuth(*Auth)
	SignIn(*Context)
	RefreshToken(*Context)
	SignOut(*Context)
	SignUp(*Context)
	Callback(*Context)
	ServeHTTP(*Context)
}

Provider authentication provider interface

Directories

Path Synopsis
providers
sms

Jump to

Keyboard shortcuts

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