auth

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultAlgorithm = "RS256"
)

Some default values

Variables

This section is empty.

Functions

func IsExpiredJWTError

func IsExpiredJWTError(err error) bool

IsExpiredJWTError checks if err is JWT ValidationErrorExpired

func SaveUserClaims

func SaveUserClaims(ctx context.Context, claims *UserClaims) context.Context

SaveUserClaims saves authorized user claims to the context

Types

type Jwt

type Jwt struct {
	PrivateKey *rsa.PrivateKey
	PublicKey  *rsa.PublicKey
}

Jwt is to sign and verify JWT

func NewJWTFromPrivatePem

func NewJWTFromPrivatePem(pem string) (*Jwt, error)

NewJWTFromPrivatePem creates signer and verifier from PEM string

func NewJWTFromPublicPem

func NewJWTFromPublicPem(pem string) (*Jwt, error)

NewJWTFromPublicPem initializes jwt verifier from public string An error will be returned if returned object is used to sign a JWT

func NewRandomJwt

func NewRandomJwt() (*Jwt, error)

NewRandomJwt generates random RSA keys for RSA algorithm This is to simplify setup jwt verify for unit test

func (*Jwt) Sign

func (a *Jwt) Sign(ctx context.Context, claims *UserClaims) (string, error)

Sign creates a new JWT token signed by RSA method

func (*Jwt) Verify

func (a *Jwt) Verify(ctx context.Context, tokenString string) (*UserClaims, error)

Verify checks if provided token string is valid or not claims is always returned to respect the jwtgo's behavior caller should have decision depend on the error type

type Signer

type Signer interface {
	Sign(ctx context.Context, claims *UserClaims) (string, error)
}

Signer defines an interface to sign authentication token

type UserClaims

type UserClaims struct {
	jwt.RegisteredClaims
	Name          string `json:"name"`
	PictureURL    string `json:"picture_url"`
	UserID        string `json:"user_id"`
	Email         string `json:"email"`
	EmailVerified bool   `json:"email_verified"`
}

UserClaims is a custom claims that contains more user data beside standard claims's data

func GetUserClaims

func GetUserClaims(ctx context.Context) *UserClaims

GetUserClaims returns the authorized user's claims from context

type Verifier

type Verifier interface {
	Verify(ctx context.Context, tokenString string) (*UserClaims, error)
}

Verifier defines interface to verify authentication token

Jump to

Keyboard shortcuts

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