-
Notifications
You must be signed in to change notification settings - Fork 1
/
change_key.go
71 lines (61 loc) · 1.54 KB
/
change_key.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
package totps
import (
"github.com/valyala/fasthttp"
"src.goblgobl.com/authen"
"src.goblgobl.com/authen/storage"
"src.goblgobl.com/authen/storage/data"
"src.goblgobl.com/utils"
"src.goblgobl.com/utils/encryption"
"src.goblgobl.com/utils/http"
"src.goblgobl.com/utils/typed"
"src.goblgobl.com/utils/validation"
)
var (
changeKeyValidation = validation.Input().
Field(typeValidation).
Field(userIdValidation).
Field(keyValidation).
Field(newKeyValidation)
)
func ChangeKey(conn *fasthttp.RequestCtx, env *authen.Env) (http.Response, error) {
input, err := typed.Json(conn.PostBody())
if err != nil {
return http.InvalidJSON, nil
}
validator := env.Validator
if !changeKeyValidation.Validate(input, validator) {
return http.Validation(validator), nil
}
tpe := input.String("type")
userId := input.String("user_id")
projectId := env.Project.Id
result, err := storage.DB.TOTPGet(data.TOTPGet{
Type: tpe,
UserId: userId,
Pending: false,
ProjectId: projectId,
})
if err != nil {
return nil, err
}
if result.Status == data.TOTP_GET_NOT_FOUND {
return resNotFound, nil
}
key := *(*[32]byte)(input.Bytes("key"))
secret, ok := encryption.Decrypt(key, result.Secret)
if !ok {
return resIncorrectKey, nil
}
newKey := *(*[32]byte)(input.Bytes("new_key"))
encrypted, err := encryption.Encrypt(newKey, utils.B2S(secret))
if err != nil {
return nil, err
}
_, err = storage.DB.TOTPCreate(data.TOTPCreate{
UserId: userId,
Type: tpe,
ProjectId: projectId,
Secret: encrypted,
})
return resOK, err
}