-
Notifications
You must be signed in to change notification settings - Fork 393
/
resetpasstokens.go
108 lines (87 loc) · 2.81 KB
/
resetpasstokens.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
108
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb
import (
"context"
"errors"
"storj.io/common/uuid"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/satellitedb/dbx"
)
// ensures that resetPasswordTokens implements console.ResetPasswordTokens.
var _ console.ResetPasswordTokens = (*resetPasswordTokens)(nil)
type resetPasswordTokens struct {
db dbx.Methods
}
// Create creates new reset password token.
func (rpt *resetPasswordTokens) Create(ctx context.Context, ownerID uuid.UUID) (_ *console.ResetPasswordToken, err error) {
defer mon.Task()(&ctx)(&err)
secret, err := console.NewResetPasswordSecret()
if err != nil {
return nil, err
}
resToken, err := rpt.db.Create_ResetPasswordToken(
ctx,
dbx.ResetPasswordToken_Secret(secret[:]),
dbx.ResetPasswordToken_OwnerId(ownerID[:]),
)
if err != nil {
return nil, err
}
return resetPasswordTokenFromDBX(ctx, resToken)
}
// GetBySecret retrieves ResetPasswordToken with given Secret.
func (rpt *resetPasswordTokens) GetBySecret(ctx context.Context, secret console.ResetPasswordSecret) (_ *console.ResetPasswordToken, err error) {
defer mon.Task()(&ctx)(&err)
resToken, err := rpt.db.Get_ResetPasswordToken_By_Secret(
ctx,
dbx.ResetPasswordToken_Secret(secret[:]),
)
if err != nil {
return nil, err
}
return resetPasswordTokenFromDBX(ctx, resToken)
}
// GetByOwnerID retrieves ResetPasswordToken by ownerID.
func (rpt *resetPasswordTokens) GetByOwnerID(ctx context.Context, ownerID uuid.UUID) (_ *console.ResetPasswordToken, err error) {
defer mon.Task()(&ctx)(&err)
resToken, err := rpt.db.Get_ResetPasswordToken_By_OwnerId(
ctx,
dbx.ResetPasswordToken_OwnerId(ownerID[:]),
)
if err != nil {
return nil, err
}
return resetPasswordTokenFromDBX(ctx, resToken)
}
// Delete deletes ResetPasswordToken by ResetPasswordSecret.
func (rpt *resetPasswordTokens) Delete(ctx context.Context, secret console.ResetPasswordSecret) (err error) {
defer mon.Task()(&ctx)(&err)
_, err = rpt.db.Delete_ResetPasswordToken_By_Secret(
ctx,
dbx.ResetPasswordToken_Secret(secret[:]),
)
return err
}
// resetPasswordTokenFromDBX is used for creating ResetPasswordToken entity from autogenerated dbx.ResetPasswordToken struct.
func resetPasswordTokenFromDBX(ctx context.Context, resetToken *dbx.ResetPasswordToken) (_ *console.ResetPasswordToken, err error) {
defer mon.Task()(&ctx)(&err)
if resetToken == nil {
return nil, errors.New("token parameter is nil")
}
var secret [32]byte
copy(secret[:], resetToken.Secret)
result := &console.ResetPasswordToken{
Secret: secret,
OwnerID: nil,
CreatedAt: resetToken.CreatedAt,
}
if resetToken.OwnerId != nil {
ownerID, err := uuid.FromBytes(resetToken.OwnerId)
if err != nil {
return nil, err
}
result.OwnerID = &ownerID
}
return result, nil
}