You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.1 KiB
55 lines
1.1 KiB
package nwjwt
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"errors"
|
|
)
|
|
|
|
/*
|
|
Sources:
|
|
https://auth0.com/docs/security/tokens/json-web-tokens/json-web-token-claims
|
|
https://en.wikipedia.org/wiki/JSON_Web_Token
|
|
https://connect2id.com/products/nimbus-jose-jwt/examples/jwk-generation
|
|
*/
|
|
|
|
type ClaimSet map[string]any
|
|
|
|
var (
|
|
ErrExpired = errors.New("token is expired")
|
|
ErrBeforeNotBefore = errors.New("token is before 'nbf'")
|
|
ErrBeforeIssuedAt = errors.New("token is before 'iat'")
|
|
ErrSignatureInvalid = errors.New("signature is invalid")
|
|
ErrConditionsDoNotMatch = errors.New("conditions do not match")
|
|
)
|
|
|
|
const (
|
|
Issuer = "iss"
|
|
Subject = "sub"
|
|
Audience = "aud"
|
|
Expiry = "exp"
|
|
NotBefore = "nbf"
|
|
IssuedAt = "iat"
|
|
ID = "jti"
|
|
)
|
|
|
|
func GenerateRandomSecret() []byte {
|
|
key := make([]byte, 32)
|
|
_, err := rand.Read(key)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
return key
|
|
}
|
|
|
|
func anyToNum(value any) (float64, bool) {
|
|
switch value.(type) {
|
|
case int:
|
|
return float64(value.(int)), true
|
|
case float64:
|
|
return value.(float64), true
|
|
default:
|
|
return 0, false
|
|
}
|
|
}
|