Skip to content
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

Allow NEW_PASSWORD_REQUIRED challenge completion. #22

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ncb000gt
Copy link
Contributor

Cognito allows users to be created via an admin user. The new user gets
a temporary password and is expected to change that password via a
challenge response. This allows that flow by making use of some existing
code.

Signed-off-by: Nick Campbell [email protected]

Cognito allows users to be created via an admin user. The new user gets
a temporary password and is expected to change that password via a
challenge response. This allows that flow by making use of some existing
code.

Signed-off-by: Nick Campbell <[email protected]>
@ncb000gt
Copy link
Contributor Author

As I looked through more of the code, it seemed like there may be other places that could use the common function than just the two for which I impl it. I can go through and make the others use that if you'd like before landing this pr. Let me know.

@ncb000gt
Copy link
Contributor Author

Bump.

@jonsaw
Copy link
Owner

jonsaw commented Nov 10, 2018

Thanks for the PR @ncb000gt. I'll try to find some time this week to look into this. Been super busy on my end here -- sorry for the late responses.

@ncb000gt
Copy link
Contributor Author

ncb000gt commented Nov 10, 2018

No worries. I know how it feels running a few open source projects myself. ;D

Thanks!

FWIW- This code is running in my Flutter app.

aneeshjoshi added a commit to spacedibs/amazon-cognito-identity-dart that referenced this pull request Nov 15, 2018
Cognito allows users to be created via an admin user. The new user gets
a temporary password and is expected to change that password via a
challenge response. This allows that flow by making use of some existing
code.

Signed-off-by: Nick Campbell <[email protected]>

jonsaw#22
@@ -657,6 +657,18 @@ class CognitoUser {
return data;
}

Future<void> completeNewPasswordChallenge(String newPassword) async {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason we don't want to pass back some status of whether the new password challenge was successful or failed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have any issue passing back the response personally. However, I understood this lib to use exceptions for flow control. So my thoughts were that if there was a problem that the client would except and that you'd have to appropriately deal with that.

Still, I don't have any reservations about responding with a good or bad signal to the caller. @jonsaw what are your thoughts on this?

@aneeshjoshi
Copy link

@ncb000gt Any chance you can provide a sample example of how to use this? I'm having some trouble where when I call completeNewPasswordChallenge, it's being invoked with a null cognito user and null session.

My current flow is:

  1. Call Login with username and password
  2. When it throws an exception that a new password is needed, I ask the user for a new password and call this function. However it seems when I do this the UserService doesn't have the necessary session/user information.

@ncb000gt
Copy link
Contributor Author

@aneeshjoshi I'm using this library in Flutter so YMMV.

I'm using this inside my mobile app. I attempt to login. If that fails, an exception is thrown and I catch that with:

} on CognitoUserNewPasswordRequiredException catch (e) {
  message = 'It looks like this is your first time logging in. You need to specify a new password.';
  ...

Then I render the "new password" form. I've held onto the CognitoUser() object I used to attempt the login in the first place and call:

  myCognitoUser.completeNewPasswordChallenge("MY new PASSWORD!");

At this point I just require that the user login again.

I do think your point above is relevant, re: if the user doesn't enter the right password conforming to whatever specifications you've laid out...but again, I'd expect that an exception would be thrown. I should probably verify that part somewhere. ;D

Does that help?

@aneeshjoshi
Copy link

aneeshjoshi commented Nov 16, 2018 via email

@ncb000gt
Copy link
Contributor Author

@aneeshjoshi Yea. I'm not entirely sure of the idiomatic dart/flutter approach, but the way I handled it was put a "service" layer that is a singleton inside the app. That maintains the CognitoUser state across the application. Then I have "listeners" that are notified of changes to auth state which will then return the user to the login screen. I settled on this approach, but I believe you could end up doing something similar or better using the BLoC approach in flutter. I just never got a chance to do that. Once the app is built and functional I'm expecting to rework a lot of parts of it. :)

aneeshjoshi added a commit to spacedibs/amazon-cognito-identity-dart that referenced this pull request Dec 31, 2018
Cognito allows users to be created via an admin user. The new user gets
a temporary password and is expected to change that password via a
challenge response. This allows that flow by making use of some existing
code.

Signed-off-by: Nick Campbell <[email protected]>

jonsaw#22
aneeshjoshi added a commit to spacedibs/amazon-cognito-identity-dart that referenced this pull request Mar 8, 2019
Cognito allows users to be created via an admin user. The new user gets
a temporary password and is expected to change that password via a
challenge response. This allows that flow by making use of some existing
code.

Signed-off-by: Nick Campbell <[email protected]>

jonsaw#22
@ncb000gt
Copy link
Contributor Author

This PR is no longer "clean" since I inadvertently pushed changes up to my branch that @BerndWessels made to one of his PRs...I can recreate the original pr if you really want.

@tigrenok00
Copy link

@ncb000gt @jonsaw
Hi guys, is this going to be available? We have the same scenario...

@ncb000gt
Copy link
Contributor Author

@jonsaw ping.

@sumeet07
Copy link

sumeet07 commented Oct 9, 2019

Hello there, Changes look safe. Can we merge this to master?

@grahamsmart
Copy link

@jonsaw any chance of this being merged into master?

@furaiev
Copy link

furaiev commented Dec 23, 2019

Hi all here,
I've copied this project in a separate package (because this one isn't supported anymore) https://pub.dev/packages/amazon_cognito_identity_dart_2
There are already fixes that were required by my project and NEW_PASSWORD_REQUIRED from maciejkozuch.
Pls take a look at latest PR furaiev/amazon-cognito-identity-dart-2#2 and welcome to contribute

@ncb000gt
Copy link
Contributor Author

I know a bunch of people have commented here. I've just implemented and tested amazon_cognito_identity_dart_2 in my project and it's working largely as expected for my use case with this feature. The only gotcha is that the function name is different from the one I specified, it is sendNewPasswordRequiredAnswer.

Hope that helps anyone here who is still struggling with this and thanks to @jonsaw for all the initial work on this project. It was really helpful. Thanks to @furaiev for picking it up for the next phase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants