-
Notifications
You must be signed in to change notification settings - Fork 399
/
registration.go
75 lines (67 loc) · 1.73 KB
/
registration.go
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
package acme
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"github.com/go-acme/lego/certcrypto"
"github.com/go-acme/lego/lego"
"github.com/go-acme/lego/registration"
)
func (c *certManager) getOrCreateAccount() (*Account, error) {
account, err := c.storage.GetAccount(c.acmeHost)
if err != nil {
return nil, err
}
if account != nil {
return account, nil
}
// register new
account, err = c.createAccount(c.email)
if err != nil {
return nil, err
}
err = c.storage.StoreAccount(c.acmeHost, account)
return account, err
}
func (c *certManager) createAccount(email string) (*Account, error) {
privateKey, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
if err != nil {
return nil, err
}
acct := &Account{
key: privateKey,
Email: c.email,
}
config := lego.NewConfig(acct)
config.CADirURL = c.acmeDirectory
config.Certificate.KeyType = certcrypto.EC384
client, err := lego.NewClient(config)
if err != nil {
return nil, err
}
reg, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
if err != nil {
return nil, err
}
acct.Registration = reg
return acct, nil
}
// Account stores the data related to an ACME account.
type Account struct {
Email string `json:"email"`
Registration *registration.Resource `json:"registration"`
key *ecdsa.PrivateKey
}
// GetEmail is a getter for the Email field.
func (a *Account) GetEmail() string {
return a.Email
}
// GetPrivateKey is a getter for the PrivateKey field.
func (a *Account) GetPrivateKey() crypto.PrivateKey {
return a.key
}
// GetRegistration is a getter for the registration field.
func (a *Account) GetRegistration() *registration.Resource {
return a.Registration
}