This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
file_persister.go
107 lines (97 loc) · 2.07 KB
/
file_persister.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
95
96
97
98
99
100
101
102
103
104
105
106
107
package kr
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
type FilePersister struct {
PairingDir string
SSHDir string
}
func (fp FilePersister) SaveMe(me Profile) (err error) {
path := filepath.Join(fp.PairingDir, "me")
if err != nil {
return
}
profileJson, err := json.Marshal(me)
if err != nil {
return
}
err = ioutil.WriteFile(path, profileJson, 0700)
return
}
func (fp FilePersister) LoadMe() (me Profile, err error) {
path := filepath.Join(fp.PairingDir, "me")
if err != nil {
return
}
profileJson, err := ioutil.ReadFile(path)
if err != nil {
return
}
err = json.Unmarshal(profileJson, &me)
if err != nil {
return
}
if len(me.SSHWirePublicKey) == 0 {
err = fmt.Errorf("missing public key")
return
}
return
}
func (fp FilePersister) DeleteMe() (err error) {
path := filepath.Join(fp.PairingDir, "me")
if err != nil {
return
}
err = os.Remove(path)
return
}
func (fp FilePersister) SaveMySSHPubKey(me Profile) (err error) {
authString, err := me.AuthorizedKeyString()
if err != nil {
return
}
err = ioutil.WriteFile(filepath.Join(fp.SSHDir, ID_KRYPTON_FILENAME), []byte(authString), 0700)
return
}
func (fp FilePersister) LoadPairing() (pairingSecret *PairingSecret, err error) {
path := filepath.Join(fp.PairingDir, PAIRING_FILENAME)
if err != nil {
return
}
pairingJson, err := ioutil.ReadFile(path)
if err != nil {
return
}
var pp persistedPairing
err = json.Unmarshal(pairingJson, &pp)
if err != nil {
return
}
ps := pairingFromPersisted(&pp)
pairingSecret = ps
return
}
func (fp FilePersister) SavePairing(pairingSecret *PairingSecret) (err error) {
path := filepath.Join(fp.PairingDir, PAIRING_FILENAME)
if err != nil {
return
}
pairingJson, err := json.Marshal(pairingToPersisted(pairingSecret))
if err != nil {
return
}
err = ioutil.WriteFile(path, pairingJson, os.FileMode(0700))
return
}
func (fp FilePersister) DeletePairing() (pairingSecret *PairingSecret, err error) {
path := filepath.Join(fp.PairingDir, PAIRING_FILENAME)
if err != nil {
return
}
err = os.Remove(path)
return
}