7.3 KiB
accesstoken
import "github.com/effective-security/xpki/jwt/accesstoken"
Package accesstoken wraps a jwt.Provider and a dataprotection.Provider to issue opaque encrypted access tokens of the form "pat.<base64url(AES-GCM (claims))>" while still parsing plain JWTs through the inner provider.
Every pat. token expires. Sign keeps a caller-supplied exp; otherwise it adds iat, nbf and an exp after WithTokenExpiry, or the inner provider's TokenExpiry, and fails when neither is set. ParseToken rejects pat. tokens without exp unless WithAllowNoExpiry is set to migrate perpetual tokens issued by older versions.
dp, err := dataprotection.NewSymmetric(secret)
if err != nil {
return err
}
p := accesstoken.New(dp, nil, accesstoken.WithTokenExpiry(time.Hour))
token, err := p.Sign(ctx, jwt.MapClaims{"sub": "user"})
if err != nil {
return err
}
claims, err := p.ParseToken(ctx, token, nil)
Index
- Constants
- func New(dp dataprotection.Provider, provider jwt.Provider, opts ...Option) jwt.Provider
- type Option
- type Provider
- func (p *Provider) GetRevocation() jwt.Revocation
- func (p *Provider) Issuer() string
- func (p *Provider) ParseToken(ctx context.Context, token string, cfg *jwt.VerifyConfig) (jwt.MapClaims, error)
- func (p *Provider) PublicKey() crypto.PublicKey
- func (p *Provider) SetRevocation(r jwt.Revocation)
- func (p *Provider) Sign(ctx context.Context, claims jwt.MapClaims) (string, error)
- func (p *Provider) TokenExpiry() time.Duration
Constants
const (
// TokenPrefix is the prefix of the opaque access tokens issued by Sign
TokenPrefix = "pat."
)
func New
func New(dp dataprotection.Provider, provider jwt.Provider, opts ...Option) jwt.Provider
New returns a Provider that issues pat. tokens protected by dp and delegates other tokens to the optional inner provider. A nil dp is allowed, but then pat. tokens can be neither issued nor parsed.
Example
ExampleNew compiles the usage sample from doc.go.
package main
import (
"context"
"fmt"
"time"
"github.com/effective-security/xpki/dataprotection"
"github.com/effective-security/xpki/jwt"
"github.com/effective-security/xpki/jwt/accesstoken"
)
func main() {
ctx := context.Background()
dp, err := dataprotection.NewSymmetric([]byte("secret"))
if err != nil {
panic(err)
}
p := accesstoken.New(dp, nil, accesstoken.WithTokenExpiry(time.Hour))
token, err := p.Sign(ctx, jwt.MapClaims{"sub": "user"})
if err != nil {
panic(err)
}
claims, err := p.ParseToken(ctx, token, nil)
if err != nil {
panic(err)
}
fmt.Println(claims.String("sub"), claims.Time("exp") != nil)
}
Output
user true
type Option
Option configures a Provider.
type Option func(*Provider)
func WithAllowNoExpiry
func WithAllowNoExpiry() Option
WithAllowNoExpiry makes ParseToken accept pat. tokens without an exp claim, such as tokens issued before Sign added one (XPKI-078). Use it only to migrate existing perpetual tokens; revocation is still checked.
func WithTokenExpiry
func WithTokenExpiry(d time.Duration) Option
WithTokenExpiry sets the lifetime Sign gives a token whose claims have no exp. It takes precedence over the inner provider's TokenExpiry. Zero keeps the inner provider's value; a negative value makes Sign fail.
type Provider
Provider of Access Token
type Provider struct {
jwt.Provider
// contains filtered or unexported fields
}
func (*Provider) GetRevocation
func (p *Provider) GetRevocation() jwt.Revocation
GetRevocation returns the revocation checker used for pat. tokens
func (*Provider) Issuer
func (p *Provider) Issuer() string
Issuer returns name of the issuer
func (*Provider) ParseToken
func (p *Provider) ParseToken(ctx context.Context, token string, cfg *jwt.VerifyConfig) (jwt.MapClaims, error)
ParseToken parses JWT Token with data protection. A pat. token must have an exp claim unless WithAllowNoExpiry is set; an unparsable exp is always rejected.
func (*Provider) PublicKey
func (p *Provider) PublicKey() crypto.PublicKey
PublicKey returns the public key of an asymmetric data protection provider, or nil for a symmetric or nil one.
func (*Provider) SetRevocation
func (p *Provider) SetRevocation(r jwt.Revocation)
SetRevocation installs the revocation checker used for pat. tokens, and forwards it to the wrapped jwt.Provider so that plain JWTs are checked against the same revocation list.
func (*Provider) Sign
func (p *Provider) Sign(ctx context.Context, claims jwt.MapClaims) (string, error)
Sign returns an encrypted pat. token for the claims. Caller-supplied exp, iat and nbf are kept and normalized to NumericDate; an unparsable one, or a zero time, is an error, see jwt.MapClaims.NormalizeTimeClaims. Without exp, the token expires after TokenExpiry, and iat and nbf are added when absent; Sign fails if TokenExpiry is not positive. The claims map is not modified.
func (*Provider) TokenExpiry
func (p *Provider) TokenExpiry() time.Duration
TokenExpiry returns the lifetime Sign gives a token without exp: the WithTokenExpiry value if set, else the inner provider's TokenExpiry, else 0. A negative WithTokenExpiry value is reported as 0.
Generated by gomarkdoc