Debian Codegraph @

jose/options.go

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