-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add VerifyPassword to API #1272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,39 @@ func createPassword(cli api.DexClient) error { | |
log.Printf("%+v", pass) | ||
} | ||
|
||
// Verifying correct and incorrect passwords | ||
log.Print("Verifying Password:\n") | ||
verifyReq := &api.VerifyPasswordReq{ | ||
Email: "[email protected]", | ||
Password: "test1", | ||
} | ||
verifyResp, err := cli.VerifyPassword(context.TODO(), verifyReq) | ||
if err != nil { | ||
return fmt.Errorf("failed to run VerifyPassword for correct password: %v", err) | ||
} | ||
if !verifyResp.Verified { | ||
return fmt.Errorf("failed to verify correct password: %v", verifyResp) | ||
} | ||
log.Printf("properly verified correct password: %t\n", verifyResp.Verified) | ||
|
||
badVerifyReq := &api.VerifyPasswordReq{ | ||
Email: "[email protected]", | ||
Password: "wrong_password", | ||
} | ||
badVerifyResp, err := cli.VerifyPassword(context.TODO(), badVerifyReq) | ||
if err != nil { | ||
return fmt.Errorf("failed to run VerifyPassword for incorrect password: %v", err) | ||
} | ||
if badVerifyResp.Verified { | ||
return fmt.Errorf("verify returned true for incorrect password: %v", badVerifyResp) | ||
} | ||
log.Printf("properly failed to verify incorrect password: %t\n", badVerifyResp.Verified) | ||
|
||
log.Print("Listing Passwords:\n") | ||
for _, pass := range resp.Passwords { | ||
log.Printf("%+v", pass) | ||
} | ||
|
||
deleteReq := &api.DeletePasswordReq{ | ||
Email: p.Email, | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -223,6 +223,40 @@ func (d dexAPI) ListPasswords(ctx context.Context, req *api.ListPasswordReq) (*a | |
|
||
} | ||
|
||
func (d dexAPI) VerifyPassword(ctx context.Context, req *api.VerifyPasswordReq) (*api.VerifyPasswordResp, error) { | ||
if req.Email == "" { | ||
return nil, errors.New("no email supplied") | ||
} | ||
|
||
if req.Password == "" { | ||
return nil, errors.New("no passwored to verify supplied") | ||
} | ||
|
||
// TODO we can dry this up if https://github.com/coreos/dex/pull/1271/files merges | ||
password, err := d.s.GetPassword(req.Email) | ||
if err != nil { | ||
if err == storage.ErrNotFound { | ||
return &api.VerifyPasswordResp{ | ||
NotFound: true, | ||
Verified: false, | ||
}, nil | ||
} | ||
d.logger.Errorf("api: there was an error retrieving the password: %v", err) | ||
return nil, fmt.Errorf("verify password: %v", err) | ||
} | ||
|
||
resp := &api.VerifyPasswordResp{ | ||
NotFound: false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] It's ok to be explicit, but this is equivalent to resp := &api.VerifyPasswordResp{} ❓ It might be easier to reason about what's happening downward from here if each return declared its own |
||
} | ||
|
||
if err := bcrypt.CompareHashAndPassword(password.Hash, []byte(req.Password)); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think, would it make sense to inspect the I'd propose something along the lines of if err := bcrypt.CompareHashAndPassword(password.Hash, []byte(req.Password)); err != nil {
if err == bcrypt.ErrMismatchedHashAndPassword {
resp.Verified = false
return resp, nil
}
return nil, err
} There might be some further simplifications around those three returns, so, just illustrating what I had in mind. |
||
resp.Verified = false | ||
return resp, nil | ||
} | ||
resp.Verified = true | ||
return resp, nil | ||
} | ||
|
||
func (d dexAPI) ListRefresh(ctx context.Context, req *api.ListRefreshReq) (*api.ListRefreshResp, error) { | ||
id := new(internal.IDTokenSubject) | ||
if err := internal.Unmarshal(req.UserId, id); err != nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[typo]
s/passwored/password/