identity

package
v6.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2022 License: Apache-2.0 Imports: 10 Imported by: 1

README

Identity

This package details with common identity-related issues, such as logging in and generating JWT tokens. It helps make the mundane tasks of creating and validating authentication tokens simpler. Here's a sample.

JWTService

package main

import (
   "github.com/ResurgenceIT/kit/v6/identity"
   "github.com/golang-jwt/jwt"
)

func main() {
   jwtService := identity.NewJWTService(identity.JWTServiceConfig{
      AuthSalt: "salt",
      AuthSecret: "secret",
      Issuer: "issuer://com.some.domain",
      TimeoutInMinutes: 60,
   })

   // Create a token
   token, _ := jwtService.CreateToken(identity.CreateTokenRequest{
      UserID: "user",
      UserName: "My Name",
      AdditionalData: map[string]interface{}{
         "key": "value",
      },
   })

   // token == base64-encoded, encrypted JWT token

   // Parse an incoming token. The result is *jwt.Token and can be 
   // manipulated using the jwt-go library
   parsedToken, _ := jwtService.ParseToken(token)

   userID, userName := jwtService.GetUserFromToken(parsedToken)
   // userID == "user"
   // userName == "My Name"

   additionalData, _ := jwtService.GetAdditionalDataFromToken(parsedToken)
   // additionalData == map[string]interface{}{
   //    "key": "value"
   // }
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidIssuer error = fmt.Errorf("Invalid issuer")
View Source
var ErrInvalidToken error = fmt.Errorf("Invalid token")
View Source
var ErrInvalidUser error = fmt.Errorf("Invalid user")
View Source
var ErrTokenMissingClaims error = fmt.Errorf("Token is missing claims")

Functions

This section is empty.

Types

type Claims

type Claims struct {
	jwt.StandardClaims
	UserID         string `json:"userID"`
	UserName       string `json:"userName"`
	AdditionalData map[string]interface{}
}

type CreateTokenRequest

type CreateTokenRequest struct {
	UserID         string
	UserName       string
	AdditionalData map[string]interface{}
}

A CreateTokenRequest is used when creating a new JWT token. It contians basic information about a user, and then allows for additional data

type IJWTService

type IJWTService interface {
	CreateToken(createRequest CreateTokenRequest) (string, error)
	GetAdditionalDataFromToken(token *jwt.Token) map[string]interface{}
	GetUserFromToken(token *jwt.Token) (string, string)
	ParseToken(tokenFromHeader string) (*jwt.Token, error)
	IsTokenValid(token *jwt.Token) error
}

IJWTService describes methods for working with JWT tokens.

type JWTResponse

type JWTResponse struct {
	Token    string `json:"token"`
	UserID   string `json:"userID"`
	UserName string `json:"userName"`
}

JWTResponse is a generic reponse that can be used to communicate a new JWT token to a caller.

type JWTService

type JWTService struct {
	// contains filtered or unexported fields
}

JWTService provides methods for working with JWT tokens

func NewJWTService

func NewJWTService(config JWTServiceConfig) JWTService

NewJWTService creates a new instance of the JWTService struct

func (JWTService) CreateToken

func (s JWTService) CreateToken(createRequest CreateTokenRequest) (string, error)

CreateToken creates a new JWT token, encrypts it, and returns it Base64 encoded. Tokens are encrypted using AES-256

func (JWTService) GetAdditionalDataFromToken

func (s JWTService) GetAdditionalDataFromToken(token *jwt.Token) map[string]interface{}

GetAdditionalDataFromToken retrieves the additional data from the claims object

func (JWTService) GetUserFromToken

func (s JWTService) GetUserFromToken(token *jwt.Token) (string, string)

GetUserFromToken retrieves the user ID and name from the claims in a JWT token

func (JWTService) IsTokenValid

func (s JWTService) IsTokenValid(token *jwt.Token) error

IsTokenValid returns an error if there are any issues with the provided JWT token. Possible issues include:

  • Missing claims
  • Invalid token format
  • Invalid issuer
  • User doesn't have a corresponding entry in the credentials table

func (JWTService) ParseToken

func (s JWTService) ParseToken(tokenFromHeader string) (*jwt.Token, error)

ParseToken decrypts the provided token and returns a JWT token object

type JWTServiceConfig

type JWTServiceConfig struct {
	AuthSalt         string
	AuthSecret       string
	Issuer           string
	TimeoutInMinutes int
}

JWTServiceConfig is a configuration object for initializing the JWTService struct

type JWTServiceMock

type JWTServiceMock struct {
	CreateTokenFunc                func(createRequest CreateTokenRequest) (string, error)
	GetAdditionalDataFromTokenFunc func(token *jwt.Token) map[string]interface{}
	GetUserFromTokenFunc           func(token *jwt.Token) (string, string)
	ParseTokenFunc                 func(tokenFromHeader string) (*jwt.Token, error)
	IsTokenValidFunc               func(token *jwt.Token) error
}

func (JWTServiceMock) CreateToken

func (m JWTServiceMock) CreateToken(createRequest CreateTokenRequest) (string, error)

func (JWTServiceMock) GetAdditionalDataFromToken

func (m JWTServiceMock) GetAdditionalDataFromToken(token *jwt.Token) map[string]interface{}

func (JWTServiceMock) GetUserFromToken

func (m JWTServiceMock) GetUserFromToken(token *jwt.Token) (string, string)

func (JWTServiceMock) IsTokenValid

func (m JWTServiceMock) IsTokenValid(token *jwt.Token) error

func (JWTServiceMock) ParseToken

func (m JWTServiceMock) ParseToken(tokenFromHeader string) (*jwt.Token, error)

Jump to

Keyboard shortcuts

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