/
create.go
37 lines (29 loc) · 1.03 KB
/
create.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
package account
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"os"
"path"
"github.com/alphatr/acme-lego/common/errors"
)
// CreateAccount 创建用户账户
func CreateAccount(email string, rootDir string) (*Account, *errors.Error) {
accountPath := path.Join(rootDir, "account")
if err := os.MkdirAll(accountPath, 0755); err != nil {
return nil, errors.NewError(errors.CommonMakeDirErrno, err, accountPath)
}
keyPath := path.Join(accountPath, "account.key")
if _, err := os.Stat(keyPath); err == nil || os.IsExist(err) {
return nil, errors.NewError(errors.CommonFileIsExistErrno, nil, keyPath)
}
configPath := path.Join(accountPath, "account.json")
if _, err := os.Stat(configPath); err == nil || os.IsExist(err) {
return nil, errors.NewError(errors.CommonFileIsExistErrno, nil, configPath)
}
secret, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
if err != nil {
return nil, errors.NewError(errors.ModelAccGenerateKeyErrno, err)
}
return &Account{Email: email, secret: secret, path: accountPath}, nil
}