auth

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2020 License: GPL-3.0 Imports: 10 Imported by: 0

README

auth-utils

Repository for commonly used authentication utilities for Gin

Documentation

Index

Constants

View Source
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

func ExtractRequestUser(ctx context.Context) *User

ExtractRequestUser gets a context.Context and extracts the user's details from c.

Jump to

Keyboard shortcuts

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