-
Notifications
You must be signed in to change notification settings - Fork 0
/
resetpasswordtoken.go
149 lines (121 loc) · 4.28 KB
/
resetpasswordtoken.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
Copyright 2015 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package local
import (
"bytes"
"context"
"github.com/gravitational/teleport/lib/backend"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/trace"
)
// GetResetPasswordTokens returns all ResetPasswordTokens
func (s *IdentityService) GetResetPasswordTokens(ctx context.Context) ([]services.ResetPasswordToken, error) {
startKey := backend.Key(passwordTokensPrefix)
result, err := s.GetRange(ctx, startKey, backend.RangeEnd(startKey), backend.NoLimit)
if err != nil {
return nil, trace.Wrap(err)
}
var tokens []services.ResetPasswordToken
for _, item := range result.Items {
if !bytes.HasSuffix(item.Key, []byte(paramsPrefix)) {
continue
}
token, err := services.GetResetPasswordTokenMarshaler().Unmarshal(item.Value)
if err != nil {
return nil, trace.Wrap(err)
}
tokens = append(tokens, token)
}
return tokens, nil
}
// DeleteResetPasswordToken deletes ResetPasswordToken by ID
func (s *IdentityService) DeleteResetPasswordToken(ctx context.Context, tokenID string) error {
_, err := s.GetResetPasswordToken(ctx, tokenID)
if err != nil {
return trace.Wrap(err)
}
startKey := backend.Key(passwordTokensPrefix, tokenID)
err = s.DeleteRange(ctx, startKey, backend.RangeEnd(startKey))
return trace.Wrap(err)
}
// GetResetPasswordToken returns a token by its ID
func (s *IdentityService) GetResetPasswordToken(ctx context.Context, tokenID string) (services.ResetPasswordToken, error) {
item, err := s.Get(ctx, backend.Key(passwordTokensPrefix, tokenID, paramsPrefix))
if err != nil {
if trace.IsNotFound(err) {
return nil, trace.NotFound("reset password token(%v) not found", tokenID)
}
return nil, trace.Wrap(err)
}
token, err := services.GetResetPasswordTokenMarshaler().Unmarshal(item.Value)
if err != nil {
return nil, trace.Wrap(err)
}
return token, nil
}
// CreateResetPasswordToken creates a token that is used for signups and resets
func (s *IdentityService) CreateResetPasswordToken(ctx context.Context, token services.ResetPasswordToken) (services.ResetPasswordToken, error) {
if err := token.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
value, err := services.GetResetPasswordTokenMarshaler().Marshal(token)
if err != nil {
return nil, trace.Wrap(err)
}
item := backend.Item{
Key: backend.Key(passwordTokensPrefix, token.GetName(), paramsPrefix),
Value: value,
Expires: token.Expiry(),
}
_, err = s.Create(ctx, item)
if err != nil {
return nil, trace.Wrap(err)
}
return token, nil
}
// GetResetPasswordTokenSecrets returns token secrets
func (s *IdentityService) GetResetPasswordTokenSecrets(ctx context.Context, tokenID string) (services.ResetPasswordTokenSecrets, error) {
item, err := s.Get(ctx, backend.Key(passwordTokensPrefix, tokenID, secretsPrefix))
if err != nil {
if trace.IsNotFound(err) {
return nil, trace.NotFound("reset password token(%v) secrets not found", tokenID)
}
return nil, trace.Wrap(err)
}
secrets, err := services.UnmarshalResetPasswordTokenSecrets(item.Value)
if err != nil {
return nil, trace.Wrap(err)
}
return secrets, nil
}
// UpsertResetPasswordTokenSecrets upserts token secrets
func (s *IdentityService) UpsertResetPasswordTokenSecrets(ctx context.Context, secrets services.ResetPasswordTokenSecrets) error {
if err := secrets.CheckAndSetDefaults(); err != nil {
return trace.Wrap(err)
}
value, err := services.MarshalResetPasswordTokenSecrets(secrets)
if err != nil {
return trace.Wrap(err)
}
item := backend.Item{
Key: backend.Key(passwordTokensPrefix, secrets.GetName(), secretsPrefix),
Value: value,
Expires: secrets.Expiry(),
}
_, err = s.Put(ctx, item)
return trace.Wrap(err)
}
const (
passwordTokensPrefix = "resetpasswordtokens"
secretsPrefix = "secrets"
)