-
Notifications
You must be signed in to change notification settings - Fork 34
/
user.go
94 lines (79 loc) · 1.95 KB
/
user.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
88
89
90
91
92
93
94
//
// simplecert
//
// Created by Philipp Mieden
// Contact: dreadl0ck@protonmail.ch
// Copyright © 2018 bestbytes. All rights reserved.
//
package simplecert
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"path/filepath"
"github.com/go-acme/lego/v3/registration"
)
const sslUserFileName = "SSLUser.json"
/*
* SSLUser
*/
// SSLUser implements the ACME User interface
type SSLUser struct {
Email string
Registration *registration.Resource
Key *rsa.PrivateKey
}
// GetEmail returns the users email
func (u SSLUser) GetEmail() string {
return u.Email
}
// GetRegistration returns the users registration resource
func (u SSLUser) GetRegistration() *registration.Resource {
return u.Registration
}
// GetPrivateKey returns the users private key
func (u SSLUser) GetPrivateKey() crypto.PrivateKey {
return u.Key
}
// get SSL User from cacheDir or create a new one
func getUser() (SSLUser, error) {
// no cached cert. start from scratch
var u SSLUser
// do we have a user?
b, err := ioutil.ReadFile(filepath.Join(c.CacheDir, sslUserFileName))
if err == nil {
// user exists. load
err = json.Unmarshal(b, &u)
if err != nil {
return u, fmt.Errorf("simplecert: failed to unmarshal SSLUser: %s", err)
}
} else {
// create private key
privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return u, fmt.Errorf("simplecert: failed to generate private key: %s", err)
}
// Create new user
u = SSLUser{
Email: c.SSLEmail,
Key: privateKey,
}
}
return u, nil
}
// save the user on disk
// fatals on error
func saveUserToDisk(u SSLUser, cacheDir string) {
b, err := json.MarshalIndent(u, "", " ")
if err != nil {
log.Fatal("[FATAL] simplecert: failed to marshal user: ", err)
}
err = ioutil.WriteFile(filepath.Join(c.CacheDir, sslUserFileName), b, c.CacheDirPerm)
if err != nil {
log.Fatal("[FATAL] simplecert: failed to write user to disk: ", err)
}
}