-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Add VerifyPassword to API #1486
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 |
---|---|---|
|
@@ -76,6 +76,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 |
---|---|---|
|
@@ -254,6 +254,37 @@ 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 password to verify supplied") | ||
} | ||
|
||
password, err := d.s.GetPassword(req.Email) | ||
if err != nil { | ||
if err == storage.ErrNotFound { | ||
return &api.VerifyPasswordResp{ | ||
NotFound: true, | ||
}, nil | ||
} | ||
d.logger.Errorf("api: there was an error retrieving the password: %v", err) | ||
return nil, fmt.Errorf("verify password: %v", err) | ||
} | ||
|
||
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. Is the if bcrypt.CompareHashAndPassword(password.Hash, []byte(req.Password)) != nil { instead. Or even verified := bcrypt.CompareHashAndPassword(password.Hash, []byte(req.Password)) == nil
return &api.VerifyPasswordResp{Verified: verified}, 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. I will add some log |
||
d.logger.Info("api: password check failed : %v", err) | ||
return &api.VerifyPasswordResp{ | ||
Verified: false, | ||
}, nil | ||
} | ||
return &api.VerifyPasswordResp{ | ||
Verified: true, | ||
}, 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.
This being GRPC, can we return
status.Error(codes.InvalidArguments, "no email supplied")
or something like that?errors.New()
, or, any error would translate tocodes.Internal
orcodes.Unknown
, can't remember.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.
I can change the error returned, but that will not be consistent with the rest of errors returned by the other functions. Do you want me to do it anyway ? and you will have some other tasks where you will change the other functions ?
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.
I'll deal with the others. 😃 You know what, you're correct, this is fine, let's keep them as-is and we can update all the errors in one go later.