Documentation ¶
Index ¶
Constants ¶
const ( // ContextUserKey is the context key used to get and set the user's data in the context. ContextUserKey = "User" // AuthCookie is the name of the authorization cookie. AuthCookie = "kd-token" // AuthHeader is the key of the authorization header. AuthHeader = "Authorization" // AuthHeaderBearer is the prefix for the authorization token in AuthHeader. AuthHeaderBearer = "Bearer" // FirstNameClaim is the claim name for the firstname of the user FirstNameClaim = "firstName" // LastNameClaim is the claim name for the lastname of the user LastNameClaim = "lastName" // UserNameLabel is the label for the full user name. UserNameLabel = "username" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Authenticator ¶
type Authenticator struct {
// contains filtered or unexported fields
}
Authenticator is a structure that handels the authentication middleware.
func NewAuthenticator ¶
func NewAuthenticator(logger *logrus.Logger) *Authenticator
NewAuthenticator creates a new Authenticator. If logger is non-nil then it will be set as-is, otherwise logger would default to logrus.New().
func (*Authenticator) ExtractToken ¶
func (r *Authenticator) ExtractToken(secret string, authURL string, c *gin.Context) *jwt.Token
ExtractToken extract the jwt token from c.Cookie(AuthCookie) or c.GetHeader(AuthHeader). If the token is invalid or expired, it will redirect the client to authURL, and return nil. If the token is valid, it will return the token.
func (*Authenticator) Middleware ¶
func (r *Authenticator) Middleware(secret string, authURL string) gin.HandlerFunc
Middleware validates the jwt token in c.Cookie(AuthCookie) or c.GetHeader(AuthHeader). If the token is not valid or expired, it will redirect the client to authURL. If the token is valid, it will set the user's data into the gin context at user.ContextUserKey.
type User ¶
type User struct { ID string `json:"id"` FirstName string `json:"firstname"` LastName string `json:"lastname"` Bucket string `json:"bucket"` }
User is a structure of an authenticated user.
func ExtractRequestUser ¶
ExtractRequestUser gets a context.Context and extracts the user's details from c.