/
account.go
87 lines (70 loc) · 2.02 KB
/
account.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
76
77
78
79
80
81
82
83
84
85
86
87
package account
import (
"crypto"
"crypto/ecdsa"
"crypto/x509"
"encoding/json"
"encoding/pem"
"io/ioutil"
"os"
"path"
"github.com/go-acme/lego/v3/registration"
"github.com/alphatr/acme-lego/common/errors"
)
// Account 用户账户
type Account struct {
Email string `json:"email"`
Registration *registration.Resource `json:"registration"`
secret *ecdsa.PrivateKey
path string
}
// GetEmail 获取邮箱
func (acc *Account) GetEmail() string {
return acc.Email
}
// GetRegistration 获取注册源
func (acc *Account) GetRegistration() *registration.Resource {
return acc.Registration
}
// GetPrivateKey 获取用户私钥
func (acc *Account) GetPrivateKey() crypto.PrivateKey {
return acc.secret
}
// Save 保存用户信息
func (acc *Account) Save() *errors.Error {
if err := acc.saveAccountConfig(); err != nil {
return errors.NewError(errors.ModelAccSaveConfigErrno, err)
}
if err := acc.saveAccountPrivateKey(); err != nil {
return errors.NewError(errors.ModelAccSavePrivateErrno, err)
}
return nil
}
func (acc *Account) saveAccountConfig() *errors.Error {
content, err := json.MarshalIndent(acc, "", " ")
if err != nil {
return errors.NewError(errors.CommonJSONMarshalErrno, err)
}
accountFile := path.Join(acc.path, "account.json")
if err := ioutil.WriteFile(accountFile, content, 0600); err != nil {
return errors.NewError(errors.CommonFileWriteErrno, err)
}
return nil
}
func (acc *Account) saveAccountPrivateKey() *errors.Error {
bytes, err := x509.MarshalECPrivateKey(acc.secret)
if err != nil {
return errors.NewError(errors.CommonMarshalPrivateErrno, err, "ecc")
}
accKeyPath := path.Join(acc.path, "account.key")
block := pem.Block{Type: "EC PRIVATE KEY", Bytes: bytes}
output, err := os.Create(accKeyPath)
if err != nil {
return errors.NewError(errors.CommonFileCreateErrno, err, accKeyPath)
}
pem.Encode(output, &block)
if err := output.Close(); err != nil {
return errors.NewError(errors.CommonFileCloseErrno, err, accKeyPath)
}
return nil
}