/
pwclip.go
96 lines (89 loc) · 1.98 KB
/
pwclip.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
package main
import (
"fmt"
"os"
"strconv"
"syscall"
"github.com/davidlazar/go-libyaml/yaml"
"github.com/davidlazar/pwclip"
"golang.org/x/crypto/ssh/terminal"
)
func promptPassphrase() ([]byte, error) {
fmt.Fprint(os.Stderr, "Passphrase: ")
passphrase, err := terminal.ReadPassword(syscall.Stdin)
fmt.Fprintln(os.Stderr)
if err != nil {
return nil, fmt.Errorf("terminal.ReadPassword: %s", err)
}
key, err := pwclip.Key(passphrase)
if err != nil {
return nil, fmt.Errorf("key derivation failed: %s", err)
}
return key, nil
}
func newPWMFromYaml(yamlDoc []byte, question *int) (*pwclip.PWM, error) {
pwm := &pwclip.PWM{
Charset: pwclip.CharsetAlphaNumeric,
Length: 32,
}
y, err := yaml.Load(yamlDoc)
if err != nil {
return nil, err
}
m, ok := y.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("unexpected yaml structure")
}
if v, ok := m["url"]; ok {
if s, ok := v.(string); ok {
pwm.URL = s
} else {
return nil, fmt.Errorf("url must be a string")
}
}
if v, ok := m["username"]; ok {
if s, ok := v.(string); ok {
pwm.Username = s
} else {
return nil, fmt.Errorf("username must be a string")
}
}
if v, ok := m["prefix"]; ok {
if s, ok := v.(string); ok {
pwm.Prefix = s
} else {
return nil, fmt.Errorf("prefix must be a string")
}
}
if v, ok := m["charset"]; ok {
if s, ok := v.(string); ok {
pwm.Charset = s
} else {
return nil, fmt.Errorf("charset must be a string")
}
}
if v, ok := m["length"]; ok {
if s, ok := v.(string); ok {
i, err := strconv.Atoi(s)
if err != nil {
return nil, fmt.Errorf("length must be an int")
}
pwm.Length = i
} else {
return nil, fmt.Errorf("length must be an int")
}
}
if question != nil {
q := "q" + strconv.Itoa(*question)
v, ok := m[q]
if !ok {
return nil, fmt.Errorf("question %q not in settings", q)
}
if s, ok := v.(string); ok {
pwm.Extra = &s
} else {
return nil, fmt.Errorf("q%d must be a string", *question)
}
}
return pwm, nil
}