Debian Codegraph @

token/token.go

raw symbols SCIP
1 package token
2
3 import (
4 "crypto"
5 "encoding/base64"
6 "time"
7
8 "github.com/pkg/errors"
9 "github.com/smallstep/cli/crypto/keys"
10 "github.com/smallstep/cli/jose"
11 )
12
13 const (
14 // DefaultIssuer when generating tokens.
15 DefaultIssuer = "step-cli"
16 // DefaultAudience when generating tokens.
17 DefaultAudience = "https://ca/sign"
18 // MinValidity token validity token duration.
19 MinValidity = 10 * time.Second
20 // MaxValidity token validity token duration.
21 MaxValidity = 1 * time.Hour
22 // DefaultValidity token validity duration.
23 DefaultValidity = 5 * time.Minute
24 // MaxValidityDelay allowable delay between Now and beginning of token validity period.
25 MaxValidityDelay = 30 * time.Minute
26 )
27
28 // RootSHAClaim is the property name for a JWT claim that stores the SHA256 of a root certificate.
29 const RootSHAClaim = "sha"
30
31 // SANSClaim is the property name for a JWT claim that stores the list of required subject alternative names.
32 const SANSClaim = "sans"
33
34 // StepClaim is the property name for a JWT claim the stores the custom information in the certificate.
35 const StepClaim = "step"
36
37 // Token interface which all token types should attempt to implement.
38 type Token interface {
39 SignedString(sigAlg string, priv interface{}) (string, error)
40 }
41
42 // Claims represents the claims that a token might have.
43 type Claims struct {
44 jose.Claims
45 ExtraClaims map[string]interface{}
46 ExtraHeaders map[string]interface{}
47 }
48
49 // Set adds the given key and value to the map of extra claims.
50 func (c *Claims) Set(key string, value interface{}) {
51 if c.ExtraClaims == nil {
52 c.ExtraClaims = make(map[string]interface{})
53 }
54 c.ExtraClaims[key] = value
55 }
56
57 // SetHeader adds the given key and value to the map of extra headers.
58 func (c *Claims) SetHeader(key string, value interface{}) {
59 if c.ExtraHeaders == nil {
60 c.ExtraHeaders = make(map[string]interface{})
61 }
62 c.ExtraHeaders[key] = value
63 }
64
65 // Sign creates a JWT with the claims and signs it with the given key.
66 func (c *Claims) Sign(alg jose.SignatureAlgorithm, key interface{}) (string, error) {
67 kid, err := GenerateKeyID(key)
68 if err != nil {
69 return "", err
70 }
71
72 so := new(jose.SignerOptions)
73 so.WithType("JWT")
74 so.WithHeader("kid", kid)
75
76 // Used to override the kid too
77 for k, v := range c.ExtraHeaders {
78 so.WithHeader(jose.HeaderKey(k), v)
79 }
80
81 signer, err := jose.NewSigner(jose.SigningKey{
82 Algorithm: alg,
83 Key: key,
84 }, so)
85 if err != nil {
86 return "", errors.Wrapf(err, "error creating JWT signer")
87 }
88
89 // Force aud to be a string
90 if len(c.Audience) == 1 {
91 c.Set("aud", c.Audience[0])
92 }
93
94 raw, err := jose.Signed(signer).Claims(c.Claims).Claims(c.ExtraClaims).CompactSerialize()
95 if err != nil {
96 return "", errors.Wrapf(err, "error serializing JWT")
97 }
98 return raw, nil
99 }
100
101 // NewClaims returns the default claims with the given options added.
102 func NewClaims(opts ...Options) (*Claims, error) {
103 c := DefaultClaims()
104 for _, fn := range opts {
105 if err := fn(c); err != nil {
106 return nil, err
107 }
108 }
109 return c, nil
110 }
111
112 // DefaultClaims returns the default claims of any token.
113 func DefaultClaims() *Claims {
114 now := time.Now().UTC()
115 return &Claims{
116 Claims: jose.Claims{
117 Issuer: DefaultIssuer,
118 Audience: jose.Audience{DefaultAudience},
119 Expiry: jose.NewNumericDate(now.Add(DefaultValidity)),
120 NotBefore: jose.NewNumericDate(now),
121 IssuedAt: jose.NewNumericDate(now),
122 },
123 ExtraClaims: make(map[string]interface{}),
124 }
125 }
126
127 // GenerateKeyID returns the SHA256 of a public key.
128 func GenerateKeyID(priv interface{}) (string, error) {
129 pub, err := keys.PublicKey(priv)
130 if err != nil {
131 return "", errors.Wrap(err, "error generating kid")
132 }
133 jwk := jose.JSONWebKey{Key: pub}
134 keyID, err := jwk.Thumbprint(crypto.SHA256)
135 if err != nil {
136 return "", errors.Wrap(err, "error generating kid")
137 }
138 return base64.RawURLEncoding.EncodeToString(keyID), nil
139 }