File: options.go

package info (click to toggle)
golang-github-smallstep-crypto 0.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,800 kB
  • sloc: sh: 66; makefile: 50
file content (106 lines) | stat: -rw-r--r-- 2,578 bytes parent folder | download | duplicates (3)
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
106
package minica

import (
	"crypto"
	"crypto/x509"

	"go.step.sm/crypto/keyutil"
	"go.step.sm/crypto/x509util"
)

type options struct {
	Name                 string
	RootTemplate         string
	IntermediateTemplate string
	GetSigner            func() (crypto.Signer, error)
}

// Option is the type used to pass custom attributes to the constructor.
type Option func(o *options)

func newOptions() *options {
	return &options{
		Name:                 "MiniCA",
		RootTemplate:         x509util.DefaultRootTemplate,
		IntermediateTemplate: x509util.DefaultIntermediateTemplate,
		GetSigner:            keyutil.GenerateDefaultSigner,
	}
}

func (o *options) apply(opts []Option) *options {
	for _, fn := range opts {
		fn(o)
	}
	return o
}

// WithName is an option that allows to overwrite the default name MiniCA. With
// the default templates, the root and intermediate certificate common names
// would be "<name> Root CA" and "<name> Intermediate CA".
func WithName(name string) Option {
	return func(o *options) {
		o.Name = name
	}
}

// WithRootTemplate is an option that allows to overwrite the template used to
// create the root certificate.
func WithRootTemplate(template string) Option {
	return func(o *options) {
		o.RootTemplate = template
	}
}

// WithIntermediateTemplate is an option that allows to overwrite the template
// used to create the intermediate certificate.
func WithIntermediateTemplate(template string) Option {
	return func(o *options) {
		o.IntermediateTemplate = template
	}
}

// WithGetSignerFunc is an option that allows to overwrite the default function to
// create a signer.
func WithGetSignerFunc(fn func() (crypto.Signer, error)) Option {
	return func(o *options) {
		o.GetSigner = fn
	}
}

type signOptions struct {
	Template string
	Modify   func(*x509.Certificate) error
}

// SignOption is the type used to pass custom attributes when signing a
// certificate request.
type SignOption func(o *signOptions)

func newSignOptions() *signOptions {
	return &signOptions{
		Template: x509util.DefaultLeafTemplate,
	}
}

func (o *signOptions) apply(opts []SignOption) *signOptions {
	for _, fn := range opts {
		fn(o)
	}
	return o
}

// WithTemplate allows to update the template used to convert a CSR into a
// certificate.
func WithTemplate(template string) SignOption {
	return func(o *signOptions) {
		o.Template = template
	}
}

// WithModifyFunc allows to update the certificate template before the signing
// it.
func WithModifyFunc(fn func(*x509.Certificate) error) SignOption {
	return func(o *signOptions) {
		o.Modify = fn
	}
}