-
Notifications
You must be signed in to change notification settings - Fork 4
/
user.go
46 lines (37 loc) · 991 Bytes
/
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
// Copyright 2016 Daniel Krawisz.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"io/ioutil"
"github.com/DanielKrawisz/bmagent/idmgr"
)
// User contains the information relevant from the
// standpoint of the server that is associated with the individual user.
type User struct {
Keys *idmgr.Manager
Path string
Username string
Pass []byte
}
// SaveKeyfile saves the key file to a file.
func (u *User) SaveKeyfile() {
saveKeyfile(u.Keys, u.Path, u.Pass)
}
func saveKeyfile(keys *idmgr.Manager, path string, pass []byte) {
var serialized []byte
var err error
if pass == nil {
serialized, err = keys.ExportPlaintext()
} else {
serialized, err = keys.ExportEncrypted(pass)
}
if err != nil {
log.Criticalf("Failed to serialize key file: %v", err)
return
}
err = ioutil.WriteFile(path, serialized, 0600)
if err != nil {
log.Criticalf("Failed to write key file: %v", err)
}
}