middleware

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2020 License: BSD-3-Clause Imports: 17 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMissingToken = errors.New("JWT Token is missing")

ErrMissingToken when the auth token is missing in the headers, query paramters or cookies

Functions

func AdminAuthorizor

func AdminAuthorizor(account Account, c *gin.Context) bool

AdminAuthorizor is used to protect routes that are allowed only by administrator accounts

func CorsConfig

func CorsConfig() cors.Config

CorsConfig stores the Cross Origin Resource Sharing configuration for orchestra

func DeviceAuthorizor

func DeviceAuthorizor(account Account, c *gin.Context) bool

DeviceAuthorizor is used to protect routes that are allowed only by authenticated devices

func ExtractClaims

func ExtractClaims(c *gin.Context) jwt.MapClaims

ExtractClaims help to extract the JWT claims

func NullAuthorizor

func NullAuthorizor(account Account, c *gin.Context) bool

NullAuthorizor is used for routes where authentication is optional and it returns always true

func RunMigrations

func RunMigrations(db *sqlx.DB) error

RunMigrations runs the database migrations

Types

type Account

type Account struct {
	Username string
	Role     string
}

Account is the details of the account

type Authorizator

type Authorizator func(account Account, c *gin.Context) bool

Authorizator structure Callback function that should perform the authorization of the authenticated user. Called only after an authentication success. Must return true on success, false on failure.

type GinDatabaseMiddleware

type GinDatabaseMiddleware struct {
	DB *sqlx.DB
}

GinDatabaseMiddleware a database aware middleware. It will set the DB property, that can be accessed via: db := c.MustGet("DB").(*sqlx.DB)

func InitDatabaseMiddleware

func InitDatabaseMiddleware(dbType string, dbString string) (*GinDatabaseMiddleware, error)

InitDatabaseMiddleware create the middleware that injects the database

func (*GinDatabaseMiddleware) MiddlewareFunc

func (mw *GinDatabaseMiddleware) MiddlewareFunc() gin.HandlerFunc

MiddlewareFunc this is what you register as the middleware

type GinJWTMiddleware

type GinJWTMiddleware struct {
	// Realm name to display to the user. Required.
	Realm string

	// signing algorithm - possible values are HS256, HS384, HS512
	// Optional, default is HS256.
	SigningAlgorithm string

	// Secret key used for signing. Required.
	Key []byte

	// Duration that a jwt token is valid. Optional, defaults to one hour.
	Timeout time.Duration

	// This field allows clients to refresh their token until MaxRefresh has passed.
	// Note that clients can refresh their token in the last moment of MaxRefresh.
	// This means that the maximum validity timespan for a token is MaxRefresh + Timeout.
	// Optional, defaults to 0 meaning not refreshable.
	MaxRefresh time.Duration

	// Callback function that should perform the authentication of the user based on userID and
	// password. Must return true on success, false on failure. Required.
	// Option return user id, if so, user id will be stored in Claim Array.
	Authenticator func(userID string, password string, c *gin.Context) (Account, bool)

	// User can define own Unauthorized func.
	Unauthorized func(*gin.Context, int, string)

	// Set the identity handler function
	IdentityHandler func(*OrchestraClaims) Account

	// TokenLookup is a string in the form of "<source>:<name>" that is used
	// to extract token from the request.
	// Optional. Default value "header:Authorization".
	// Possible values:
	// - "header:<name>"
	// - "query:<name>"
	// - "cookie:<name>"
	TokenLookup string

	// TokenHeadName is a string in the header. Default value is "Bearer"
	TokenHeadName string

	// TimeFunc provides the current time. You can override it to use another time value. This is useful for testing or if your server uses a different time zone than your tokens.
	TimeFunc func() time.Time
}

GinJWTMiddleware provides a Json-Web-Token authentication implementation. On failure, a 401 HTTP response is returned. On success, the wrapped middleware is called, and the userID is made available as c.Get("userID").(string). Users can get a token by posting a json request to LoginHandler. The token then needs to be passed in the Authentication header. Example: `Authorization: Bearer XXX_TOKEN_XXX`

func InitAuthMiddleware

func InitAuthMiddleware(db *sqlx.DB) (*GinJWTMiddleware, error)

InitAuthMiddleware is called to initialise the authentication middleware

func (*GinJWTMiddleware) LoginHandler

func (mw *GinJWTMiddleware) LoginHandler(c *gin.Context)

LoginHandler can be used by clients to get a jwt token. Payload needs to be json in the form of {"username": "USERNAME", "password": "PASSWORD"}. Reply will be of the form {"token": "TOKEN"}.

func (*GinJWTMiddleware) MiddlewareFunc

func (mw *GinJWTMiddleware) MiddlewareFunc(auth Authorizator) gin.HandlerFunc

MiddlewareFunc makes GinJWTMiddleware implement the Middleware interface.

func (*GinJWTMiddleware) MiddlewareInit

func (mw *GinJWTMiddleware) MiddlewareInit() error

MiddlewareInit initialize jwt configs.

func (*GinJWTMiddleware) RefreshHandler

func (mw *GinJWTMiddleware) RefreshHandler(c *gin.Context)

RefreshHandler can be used to refresh a token. The token still needs to be valid on refresh. Shall be put under an endpoint that is using the GinJWTMiddleware. Reply will be of the form {"token": "TOKEN"}.

func (*GinJWTMiddleware) TokenGenerator

func (mw *GinJWTMiddleware) TokenGenerator(userID string, role string) string

TokenGenerator handler that clients can use to get a jwt token.

type Login

type Login struct {
	Username string `form:"username" json:"username" binding:"required"`
	Password string `form:"password" json:"password" binding:"required"`
}

Login form structure.

type OrchestraClaims

type OrchestraClaims struct {
	Role string `json:"role"`
	User string `json:"user"`
	jwt.StandardClaims
}

OrchestraClaims are claims for the JWT token

Jump to

Keyboard shortcuts

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