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