-
Notifications
You must be signed in to change notification settings - Fork 42
/
legacy.go
47 lines (40 loc) · 1.06 KB
/
legacy.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
package keysharecore
import (
"crypto/rand"
"github.com/go-errors/errors"
)
// ChangePinLegacy is like ChangePin() but for legacy clients that have not yet upgraded to
// challenge-response.
func (c *Core) ChangePinLegacy(secrets UserSecrets, oldpinRaw, newpinRaw string) (UserSecrets, error) {
s, err := c.decryptUserSecretsIfPinOK(secrets, oldpinRaw)
if err != nil {
return nil, err
}
if s.PublicKey != nil {
return nil, errors.New("JWT required")
}
// change and reencrypt
id := make([]byte, 32)
_, err = rand.Read(id)
if err != nil {
return nil, err
}
if err = s.setPin(newpinRaw); err != nil {
return nil, err
}
if err = s.setID(id); err != nil {
return nil, err
}
return c.encryptUserSecrets(s)
}
// ValidateAuthLegacy checks pin for validity and generates JWT for future access.
func (c *Core) ValidateAuthLegacy(secrets UserSecrets, pin string) (string, error) {
s, err := c.decryptUserSecretsIfPinOK(secrets, pin)
if err != nil {
return "", err
}
if s.PublicKey != nil {
return "", ErrChallengeResponseRequired
}
return c.authJWT(&s)
}