|
1 | 1 | package jose |
2 | 2 |
|
| 3 | +import ( |
| 4 | + "github.com/smallstep/cli/utils" |
| 5 | +) |
| 6 | + |
3 | 7 | type context struct { |
4 | 8 | use, alg, kid string |
5 | 9 | subtle, insecure bool |
6 | 10 | noDefaults bool |
7 | 11 | password []byte |
8 | 12 | } |
9 | 13 |
|
10 | | -// apply the options to the context and returns it. |
11 | | -func (ctx *context) apply(opts ...Option) *context { |
| 14 | +// apply the options to the context and returns an error if one of the options |
| 15 | +// fails. |
| 16 | +func (ctx *context) apply(opts ...Option) (*context, error) { |
12 | 17 | for _, opt := range opts { |
13 | | - opt(ctx) |
| 18 | + if err := opt(ctx); err != nil { |
| 19 | + return nil, err |
| 20 | + } |
14 | 21 | } |
15 | | - return ctx |
| 22 | + return ctx, nil |
16 | 23 | } |
17 | 24 |
|
18 | 25 | // Option is the type used to add attributes to the context. |
19 | | -type Option func(ctx *context) |
| 26 | +type Option func(ctx *context) error |
20 | 27 |
|
21 | 28 | // WithUse adds the use claim to the context. |
22 | 29 | func WithUse(use string) Option { |
23 | | - return func(ctx *context) { |
| 30 | + return func(ctx *context) error { |
24 | 31 | ctx.use = use |
| 32 | + return nil |
25 | 33 | } |
26 | 34 | } |
27 | 35 |
|
28 | 36 | // WithAlg adds the alg claim to the context. |
29 | 37 | func WithAlg(alg string) Option { |
30 | | - return func(ctx *context) { |
| 38 | + return func(ctx *context) error { |
31 | 39 | ctx.alg = alg |
| 40 | + return nil |
32 | 41 | } |
33 | 42 | } |
34 | 43 |
|
35 | 44 | // WithKid adds the kid property to the context. |
36 | 45 | func WithKid(kid string) Option { |
37 | | - return func(ctx *context) { |
| 46 | + return func(ctx *context) error { |
38 | 47 | ctx.kid = kid |
| 48 | + return nil |
39 | 49 | } |
40 | 50 | } |
41 | 51 |
|
42 | 52 | // WithSubtle marks the context as subtle. |
43 | 53 | func WithSubtle(subtle bool) Option { |
44 | | - return func(ctx *context) { |
| 54 | + return func(ctx *context) error { |
45 | 55 | ctx.subtle = subtle |
| 56 | + return nil |
46 | 57 | } |
47 | 58 | } |
48 | 59 |
|
49 | 60 | // WithInsecure marks the context as insecure. |
50 | 61 | func WithInsecure(insecure bool) Option { |
51 | | - return func(ctx *context) { |
| 62 | + return func(ctx *context) error { |
52 | 63 | ctx.insecure = insecure |
| 64 | + return nil |
53 | 65 | } |
54 | 66 | } |
55 | 67 |
|
56 | 68 | // WithNoDefaults avoids that the parser loads defaults values, specially the |
57 | 69 | // default algorithms. |
58 | 70 | func WithNoDefaults(val bool) Option { |
59 | | - return func(ctx *context) { |
| 71 | + return func(ctx *context) error { |
60 | 72 | ctx.noDefaults = val |
| 73 | + return nil |
61 | 74 | } |
62 | 75 | } |
63 | 76 |
|
64 | 77 | // WithPassword is a method that adds the given password to the context. |
65 | 78 | func WithPassword(pass []byte) Option { |
66 | | - return func(ctx *context) { |
| 79 | + return func(ctx *context) error { |
67 | 80 | ctx.password = pass |
| 81 | + return nil |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// WithPasswordFile is a method that adds the password in a file to the context. |
| 86 | +func WithPasswordFile(filename string) Option { |
| 87 | + return func(ctx *context) error { |
| 88 | + b, err := utils.ReadPasswordFromFile(filename) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + ctx.password = b |
| 93 | + return nil |
68 | 94 | } |
69 | 95 | } |
0 commit comments