forked from dexidp/dex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
password.go
283 lines (228 loc) · 6.68 KB
/
password.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package user
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/url"
"os"
"time"
"golang.org/x/crypto/bcrypt"
"github.com/coreos/go-oidc/jose"
"github.com/coreos/go-oidc/key"
"github.com/coreos/go-oidc/oidc"
"github.com/coreos/dex/repo"
)
const (
bcryptHashCost = 10
// Blowfish, the algorithm underlying bcrypt, has a maximum
// password length of 72. We explicitly track and check this
// since the bcrypt library will silently ignore portions of
// a password past the first 72 characters.
maxSecretLength = 72
)
var (
PasswordHasher = DefaultPasswordHasher
ErrorInvalidPassword = errors.New("invalid Password")
ErrorPasswordHashNoMatch = errors.New("password and hash don't match")
ErrorPasswordExpired = errors.New("password has expired")
)
type Hasher func(string) ([]byte, error)
func DefaultPasswordHasher(s string) ([]byte, error) {
pwHash, err := bcrypt.GenerateFromPassword([]byte(s), bcryptHashCost)
if err != nil {
return nil, err
}
return Password(pwHash), nil
}
type Password []byte
func NewPasswordFromPlaintext(plaintext string) (Password, error) {
return PasswordHasher(plaintext)
}
type PasswordInfo struct {
UserID string
Password Password
PasswordExpires time.Time
}
func (p PasswordInfo) Authenticate(plaintext string) (*oidc.Identity, error) {
if err := bcrypt.CompareHashAndPassword(p.Password, []byte(plaintext)); err != nil {
return nil, ErrorPasswordHashNoMatch
}
if !p.PasswordExpires.IsZero() && time.Now().After(p.PasswordExpires) {
return nil, ErrorPasswordExpired
}
ident := p.Identity()
return &ident, nil
}
func (p PasswordInfo) Identity() oidc.Identity {
return oidc.Identity{
ID: p.UserID,
}
}
type PasswordInfoRepo interface {
Get(tx repo.Transaction, id string) (PasswordInfo, error)
Update(repo.Transaction, PasswordInfo) error
Create(repo.Transaction, PasswordInfo) error
}
func NewPasswordInfoRepo() PasswordInfoRepo {
return &memPasswordInfoRepo{
pws: make(map[string]PasswordInfo),
}
}
type memPasswordInfoRepo struct {
pws map[string]PasswordInfo
}
func (m *memPasswordInfoRepo) Get(_ repo.Transaction, id string) (PasswordInfo, error) {
pw, ok := m.pws[id]
if !ok {
return PasswordInfo{}, ErrorNotFound
}
return pw, nil
}
func (m *memPasswordInfoRepo) Create(_ repo.Transaction, pw PasswordInfo) error {
_, ok := m.pws[pw.UserID]
if ok {
return ErrorDuplicateID
}
if pw.UserID == "" {
return ErrorInvalidID
}
if len(pw.Password) == 0 {
return ErrorInvalidPassword
}
m.pws[pw.UserID] = pw
return nil
}
func (m *memPasswordInfoRepo) Update(_ repo.Transaction, pw PasswordInfo) error {
if pw.UserID == "" {
return ErrorInvalidID
}
_, ok := m.pws[pw.UserID]
if !ok {
return ErrorNotFound
}
if len(pw.Password) == 0 {
return ErrorInvalidPassword
}
m.pws[pw.UserID] = pw
return nil
}
func (u *PasswordInfo) UnmarshalJSON(data []byte) error {
var dec struct {
UserID string `json:"userId"`
PasswordHash string `json:"passwordHash"`
PasswordPlaintext string `json:"passwordPlaintext"`
PasswordExpires time.Time `json:"passwordExpires"`
}
err := json.Unmarshal(data, &dec)
if err != nil {
return fmt.Errorf("invalid User entry: %v", err)
}
u.UserID = dec.UserID
u.PasswordExpires = dec.PasswordExpires
if len(dec.PasswordHash) != 0 {
if dec.PasswordPlaintext != "" {
return ErrorInvalidPassword
}
u.Password = Password(dec.PasswordHash)
return nil
}
if dec.PasswordPlaintext != "" {
u.Password, err = NewPasswordFromPlaintext(dec.PasswordPlaintext)
if err != nil {
return err
}
}
return nil
}
func newPasswordInfosFromReader(r io.Reader) ([]PasswordInfo, error) {
var pws []PasswordInfo
err := json.NewDecoder(r).Decode(&pws)
return pws, err
}
func readPasswordInfosFromFile(loc string) ([]PasswordInfo, error) {
pwf, err := os.Open(loc)
if err != nil {
return nil, fmt.Errorf("unable to read password info from file %q: %v", loc, err)
}
return newPasswordInfosFromReader(pwf)
}
func LoadPasswordInfos(repo PasswordInfoRepo, pws []PasswordInfo) error {
for i, pw := range pws {
err := repo.Create(nil, pw)
if err != nil {
return fmt.Errorf("error loading PasswordInfo[%d]: %q", i, err)
}
}
return nil
}
func NewPasswordInfoRepoFromPasswordInfos(pws []PasswordInfo) PasswordInfoRepo {
memRepo := NewPasswordInfoRepo().(*memPasswordInfoRepo)
for _, pw := range pws {
memRepo.pws[pw.UserID] = pw
}
return memRepo
}
func NewPasswordInfoRepoFromFile(loc string) (PasswordInfoRepo, error) {
pws, err := readPasswordInfosFromFile(loc)
if err != nil {
return nil, err
}
return NewPasswordInfoRepoFromPasswordInfos(pws), nil
}
func NewPasswordReset(userID string, password Password, issuer url.URL, clientID string, callback url.URL, expires time.Duration) PasswordReset {
claims := oidc.NewClaims(issuer.String(), userID, clientID, clock.Now(), clock.Now().Add(expires))
claims.Add(ClaimPasswordResetPassword, string(password))
claims.Add(ClaimPasswordResetCallback, callback.String())
return PasswordReset{claims}
}
type PasswordReset struct {
Claims jose.Claims
}
// ParseAndVerifyPasswordResetToken parses a string into a an
// PasswordReset, verifies the signature, and ensures that required
// claims are present. In addition to the usual claims required by
// the OIDC spec, "aud" and "sub" must be present as well as
// ClaimPasswordResetCallback and ClaimPasswordResetPassword.
func ParseAndVerifyPasswordResetToken(token string, issuer url.URL, keys []key.PublicKey) (PasswordReset, error) {
tokenClaims, err := parseAndVerifyTokenClaims(token, issuer, keys)
if err != nil {
return PasswordReset{}, err
}
pw, ok, err := tokenClaims.Claims.StringClaim(ClaimPasswordResetPassword)
if err != nil {
return PasswordReset{}, err
}
if !ok || pw == "" {
return PasswordReset{}, fmt.Errorf("no %q claim", ClaimPasswordResetPassword)
}
cb, ok, err := tokenClaims.Claims.StringClaim(ClaimPasswordResetCallback)
if err != nil {
return PasswordReset{}, err
}
if _, err := url.Parse(cb); err != nil {
return PasswordReset{}, fmt.Errorf("callback URL not parseable: %v", cb)
}
return PasswordReset{tokenClaims.Claims}, nil
}
func (e PasswordReset) UserID() string {
return assertStringClaim(e.Claims, "sub")
}
func (e PasswordReset) Password() Password {
pw := assertStringClaim(e.Claims, ClaimPasswordResetPassword)
return Password(pw)
}
func (e PasswordReset) Callback() *url.URL {
cb, ok, err := e.Claims.StringClaim(ClaimPasswordResetCallback)
if err != nil {
panic("PasswordReset: error getting string claim. This should be impossible.")
}
if !ok || cb == "" {
return nil
}
cbURL, err := url.Parse(cb)
if err != nil {
panic("PasswordReset: can't parse callback. This should be impossible.")
}
return cbURL
}