middleware

package
v0.0.0-...-d0dd730 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2019 License: Apache-2.0 Imports: 17 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractClaims

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

ExtractClaims help to extract the JWT claims

func GinLogger

func GinLogger(log *logrus.Logger) gin.HandlerFunc

GinLogger is the logrus logger handler for gin

func Language

func Language() gin.HandlerFunc

Language sets the language inside the gin context, based on the passed in Accept-[User-]Language header if nothing specified, english will be used

func MessageCount

func MessageCount() gin.HandlerFunc

MessageCount sets the users message count in the header

func RequestID

func RequestID() gin.HandlerFunc

RequestID generates a new requestID and stores it in the gin context

func UseIcopContext

func UseIcopContext(f func(uc *IcopContext, c *gin.Context)) gin.HandlerFunc

UseIcopContext is used to wrap the gin context

Types

type AuthUser

type AuthUser struct {
	UserID            int64
	MailConfirmed     bool
	MnemonicConfirmed bool
	TfaConfirmed      bool
	TfaSecret         string
	Email             string
	MessageCount      int
	PublicKey0        string
}

AuthUser is the userdata that is stored in every request

func GetAuthUser

func GetAuthUser(c *gin.Context) *AuthUser

GetAuthUser returns the stored authUser, or an empty one

type IcopContext

type IcopContext struct {
	RequestID string
	Language  string
	Log       *logrus.Entry
}

IcopContext context used in the apis to store some default values

type IcopContextMiddleware

type IcopContextMiddleware struct {
	ServiceName string
}

IcopContextMiddleware general middleware for all endpint services we store global values in here and reread them in the explizit middleware in order to store them in typed structs

func (*IcopContextMiddleware) MiddlewareFunc

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

MiddlewareFunc handler func for the middleware

type IcopJWTMiddleware

type IcopJWTMiddleware struct {
	ServiceName string
	JwtClient   func() pb.JwtServiceClient //the jwtClient is used for getting the current jwts
	DbClient    func() pb.DBServiceClient  //the dbClient is used for getting the current userData

	// Key name in database
	AuthDBKey string

	// Realm name to display to the user. Required.
	Realm string

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

	// 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) (string, bool)

	// 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.
	// Optional, default to success.
	Authorizator func(userID string, c *gin.Context) bool

	// Callback function that will be called during login.
	// Using this function it is possible to add additional payload data to the webtoken.
	// The data is then made available during requests via c.Get("JWT_PAYLOAD").
	// Note that the payload is not encrypted.
	// The attributes mentioned on jwt.io can't be used as keys for the map.
	// Optional, by default no additional data will be set.
	PayloadFunc func(userID string) map[string]interface{}

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

	// Set the identity handler function
	IdentityHandler func(jwt.MapClaims) string

	// 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

	// Name of the header key. Default value "Authorization".
	TokenLookupName 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
	// contains filtered or unexported fields
}

IcopJWTMiddleware 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 (*IcopJWTMiddleware) MiddlewareFunc

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

MiddlewareFunc makes IcopJWTMiddleware implement the Middleware interface.

func (*IcopJWTMiddleware) MiddlewareInit

func (mw *IcopJWTMiddleware) MiddlewareInit() error

MiddlewareInit initialize jwt configs.

func (*IcopJWTMiddleware) RefreshHandler

func (mw *IcopJWTMiddleware) 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 IcopJWTMiddleware. Reply will be of the form {"token": "TOKEN"}.

func (*IcopJWTMiddleware) SetAuthHeader

func (mw *IcopJWTMiddleware) SetAuthHeader(c *gin.Context, userID int64)

SetAuthHeader sets the token into the gin header

func (*IcopJWTMiddleware) SetAuthUserData

func (mw *IcopJWTMiddleware) SetAuthUserData(c *gin.Context, userID int64) bool

SetAuthUserData general functions for setting the user in the middleware

func (*IcopJWTMiddleware) TokenGenerator

func (mw *IcopJWTMiddleware) TokenGenerator(c *gin.Context, userID 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.

Jump to

Keyboard shortcuts

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