| 1 | package provision |
| 2 | |
| 3 | import ( |
| 4 | "github.com/smallstep/cli/jose" |
| 5 | "github.com/smallstep/cli/token" |
| 6 | ) |
| 7 | |
| 8 | // Token defines a one time use token that is intended to be exchanged |
| 9 | // for a newly provisioned certificate by an end entity. Token differs |
| 10 | // from BootstrapToken because it does not self contain networking information |
| 11 | // for connecting to the certificate authority or gatewayconsole. |
| 12 | // |
| 13 | // Token implements the Token interface. |
| 14 | type Token struct { |
| 15 | claims *token.Claims |
| 16 | } |
| 17 | |
| 18 | // New returns a new unsigned One-Time-Token for authorizing a |
| 19 | // single request from a newly provisioned end-entity. |
| 20 | // The current implementation uses jwt.Token and is generated using sane defaults |
| 21 | // for the claims. |
| 22 | // See token/options.go for default claim definitions. |
| 23 | func New(subject string, opts ...token.Options) (*Token, error) { |
| 24 | o := append([]token.Options{token.WithSubject(subject)}, opts...) |
| 25 | c, err := token.NewClaims(o...) |
| 26 | if err != nil { |
| 27 | return nil, err |
| 28 | } |
| 29 | return &Token{claims: c}, nil |
| 30 | } |
| 31 | |
| 32 | // SignedString implementation of the Token interface. It returns a JWT using |
| 33 | // the compact serialization. |
| 34 | func (t *Token) SignedString(sigAlg string, key interface{}) (string, error) { |
| 35 | return t.claims.Sign(jose.SignatureAlgorithm(sigAlg), key) |
| 36 | } |