token

package
v0.0.0-...-356ed1d Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RoleHierarchy = map[string][]string{
	"admin": {"read", "write", "admin"},
	"write": {"read", "write"},
	"read":  {"read"},
}

RoleHierarchy defines the relationship between roles and their associated scopes. Each role is mapped to a list of permissible scopes. This hierarchy is used to determine whether a user with a given role has access to specific actions.

Example:

  • "admin" includes "read", "write", and "admin" scopes.
  • "write" includes "read" and "write" scopes.
  • "read" includes only the "read" scope.

Functions

func GenerateAllowedRoles

func GenerateAllowedRoles(roleHierarchy map[string][]string) []string

GenerateAllowedRoles extracts the keys from RoleHierarchy to create a list of allowed roles.

Types

type CustomClaims

type CustomClaims struct {
	Roles []string `json:"roles" validate:"required,dive,oneof=read write admin"`
	jwt.RegisteredClaims
}

CustomClaims defines the structure of your token claims

type Manager

type Manager interface {
	// Generate generates a signed JWT with the given roles.
	Generate(
		signingKey string,
		roles []string,
		subject string,
	) (string, error)
	// Validate parses and validates the JWT.
	Validate(
		tokenString string,
		signingKey string,
	) (*CustomClaims, error)
}

Manager responsible for Token operations.

type Token

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

Token implementation of the token operations.

func New

func New(
	logger *slog.Logger,
) *Token

New factory to create a new instance.

func (*Token) Generate

func (t *Token) Generate(
	signingKey string,
	roles []string,
	subject string,
) (string, error)

Generate generates a signed JWT with the given roles.

func (*Token) Validate

func (t *Token) Validate(
	tokenString string,
	signingKey string,
) (*CustomClaims, error)

Validate parses and validates the JWT.

Jump to

Keyboard shortcuts

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