Debian Codegraph @

token/options.go

raw symbols SCIP
1 package token
2
3 import (
4 "crypto/sha256"
5 "encoding/hex"
6 "time"
7
8 "github.com/pkg/errors"
9 "github.com/smallstep/cli/crypto/pemutil"
10 "github.com/smallstep/cli/jose"
11 )
12
13 // Options is a function that set claims.
14 type Options func(c *Claims) error
15
16 // WithClaim is an Options function that adds a custom claim to the JWT.
17 func WithClaim(name string, value interface{}) Options {
18 return func(c *Claims) error {
19 if name == "" {
20 return errors.New("name cannot be empty")
21 }
22 c.Set(name, value)
23 return nil
24 }
25 }
26
27 // WithRootCA returns an Options function that calculates the SHA256 of the
28 // given root certificate to be used in the token claims. If this method it's
29 // not used the default root certificate in the $STEPPATH secrets directory will
30 // be used.
31 func WithRootCA(path string) Options {
32 return func(c *Claims) error {
33 cert, err := pemutil.ReadCertificate(path)
34 if err != nil {
35 return err
36 }
37 sum := sha256.Sum256(cert.Raw)
38 c.Set(RootSHAClaim, hex.EncodeToString(sum[:]))
39 return nil
40 }
41 }
42
43 // WithSHA returns an Options function that sets the SHA claim to the given
44 // value.
45 func WithSHA(sum string) Options {
46 return func(c *Claims) error {
47 c.Set(RootSHAClaim, sum)
48 return nil
49 }
50 }
51
52 // WithSANS returns an Options function that sets the list of required SANs
53 // in the token claims.
54 func WithSANS(sans []string) Options {
55 return func(c *Claims) error {
56 c.Set(SANSClaim, sans)
57 return nil
58 }
59 }
60
61 // WithStep returns an Options function that sets the step claim in the payload.
62 func WithStep(v interface{}) Options {
63 return func(c *Claims) error {
64 c.Set(StepClaim, v)
65 return nil
66 }
67 }
68
69 // WithSSH returns an Options function that sets the step claim with the ssh
70 // property in the value.
71 func WithSSH(v interface{}) Options {
72 return WithStep(map[string]interface{}{
73 "ssh": v,
74 })
75 }
76
77 // WithValidity validates boundary inputs and sets the 'nbf' (NotBefore) and
78 // 'exp' (expiration) options.
79 func WithValidity(notBefore, expiration time.Time) Options {
80 return func(c *Claims) error {
81 now := time.Now().UTC()
82 if expiration.Before(notBefore) {
83 return errors.Errorf("nbf < exp: nbf=%v, exp=%v", notBefore, expiration)
84 }
85 requestedDelay := notBefore.Sub(now)
86 if requestedDelay > MaxValidityDelay {
87 return errors.Errorf("requested validity delay is too long: 'requested validity delay'=%v, 'max validity delay'=%v", requestedDelay, MaxValidityDelay)
88 }
89 requestedValidity := expiration.Sub(notBefore)
90 if requestedValidity < MinValidity {
91 return errors.Errorf("requested token validity is too short: 'requested token validity'=%v, 'minimum token validity'=%v", requestedValidity, MinValidity)
92 } else if requestedValidity > MaxValidity {
93 return errors.Errorf("requested token validity is too long: 'requested token validity'=%v, 'maximum token validity'=%v", requestedValidity, MaxValidity)
94 }
95 c.NotBefore = jose.NewNumericDate(notBefore)
96 c.Expiry = jose.NewNumericDate(expiration)
97 return nil
98 }
99 }
100
101 // WithIssuer returns an Options function that sets the issuer to use in the
102 // token claims. If Issuer is not used the default issuer will be used.
103 func WithIssuer(s string) Options {
104 return func(c *Claims) error {
105 if s == "" {
106 return errors.New("issuer cannot be empty")
107 }
108 c.Issuer = s
109 return nil
110 }
111 }
112
113 // WithSubject returns an Options that sets the subject to use in the token
114 // claims.
115 func WithSubject(s string) Options {
116 return func(c *Claims) error {
117 if s == "" {
118 return errors.New("subject cannot be empty")
119 }
120 c.Subject = s
121 return nil
122 }
123 }
124
125 // WithAudience returns a Options that sets the audience to use in the token
126 // claims. If Audience is not used the default audience will be used.
127 func WithAudience(s string) Options {
128 return func(c *Claims) error {
129 if s == "" {
130 return errors.New("audience cannot be empty")
131 }
132 c.Audience = append(jose.Audience{}, s)
133 return nil
134 }
135 }
136
137 // WithJWTID returns a Options that sets the jwtID to use in the token
138 // claims. If WithJWTID is not used a random identifier will be used.
139 func WithJWTID(s string) Options {
140 return func(c *Claims) error {
141 if s == "" {
142 return errors.New("jwtID cannot be empty")
143 }
144 c.ID = s
145 return nil
146 }
147 }
148
149 // WithKid returns a Options that sets the header kid claims.
150 // If WithKid is not used a thumbprint using SHA256 will be used.
151 func WithKid(s string) Options {
152 return func(c *Claims) error {
153 if s == "" {
154 return errors.New("kid cannot be empty")
155 }
156 c.SetHeader("kid", s)
157 return nil
158 }
159 }
160
161 // WithX5CFile returns a Options that sets the header x5c claims.
162 func WithX5CFile(certFile string, key interface{}) Options {
163 return func(c *Claims) error {
164 certStrs, err := jose.ValidateX5C(certFile, key)
165 if err != nil {
166 return errors.Wrap(err, "error validating x5c certificate chain and key for use in x5c header")
167 }
168 c.SetHeader("x5c", certStrs)
169 return nil
170 }
171 }
172
173 // WithX5CInsecureFile returns a Options that sets the header x5cAllowInvalid claims.
174 // The `x5c` claims can only be accessed by running a method on the jose Token
175 // which validates the certificate chain before returning it. This option serves
176 // a use case where the user would prefer not to validate the certificate chain
177 // before returning it. Presumably the user would then perform their own validation.
178 // NOTE: here be dragons. Use WithX5CFile unless you know what you are doing.
179 func WithX5CInsecureFile(certFile string, key interface{}) Options {
180 return func(c *Claims) error {
181 certStrs, err := jose.ValidateX5C(certFile, key)
182 if err != nil {
183 return errors.Wrap(err, "error validating x5c certificate chain and key for use in x5c header")
184 }
185 c.SetHeader(jose.X5cInsecureKey, certStrs)
186 return nil
187 }
188 }
189
190 // WithSSHPOPFile returns a Options that sets the header sshpop claims.
191 func WithSSHPOPFile(certFile string, key interface{}) Options {
192 return func(c *Claims) error {
193 certStrs, err := jose.ValidateSSHPOP(certFile, key)
194 if err != nil {
195 return errors.Wrap(err, "error validating SSH certificate and key for use in sshpop header")
196 }
197 c.SetHeader("sshpop", certStrs)
198 return nil
199 }
200 }