| 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 |
|
| 14 |
type Options func(c *Claims) error |
| 15 |
|
| 16 |
|
| 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 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 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 |
|
| 44 |
|
| 45 |
func WithSHA(sum string) Options { |
| 46 |
return func(c *Claims) error { |
| 47 |
c.Set(RootSHAClaim, sum) |
| 48 |
return nil |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
func WithSANS(sans []string) Options { |
| 55 |
return func(c *Claims) error { |
| 56 |
c.Set(SANSClaim, sans) |
| 57 |
return nil |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
func WithStep(v interface{}) Options { |
| 63 |
return func(c *Claims) error { |
| 64 |
c.Set(StepClaim, v) |
| 65 |
return nil |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
func WithSSH(v interface{}) Options { |
| 72 |
return WithStep(map[string]interface{}{ |
| 73 |
"ssh": v, |
| 74 |
}) |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 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 |
|
| 102 |
|
| 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 |
|
| 114 |
|
| 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 |
|
| 126 |
|
| 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 |
|
| 138 |
|
| 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 |
|
| 150 |
|
| 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 |
|
| 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 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 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 |
|
| 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 |
} |