Skip to content

Commit 811707a

Browse files
authored
fix(auth): cognito integ test fixes (#1940)
1 parent ceedf2d commit 811707a

File tree

7 files changed

+125
-73
lines changed

7 files changed

+125
-73
lines changed

packages/amplify_core/lib/src/types/auth/sign_in/auth_next_sign_in_step.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class AuthNextSignInStep<Key extends UserAttributeKey> extends AuthNextStep
2727
super.additionalInfo,
2828
super.codeDeliveryDetails,
2929
required this.signInStep,
30-
this.challengeParameters,
3130
this.missingAttributes = const [],
3231
});
3332

@@ -41,7 +40,6 @@ class AuthNextSignInStep<Key extends UserAttributeKey> extends AuthNextStep
4140
);
4241

4342
final String signInStep;
44-
final Map<String, String>? challengeParameters;
4543

4644
/// Attributes which are required in your backend but have not yet been
4745
/// provided as part of the sign-in/sign-up flow for this user.
@@ -53,7 +51,6 @@ class AuthNextSignInStep<Key extends UserAttributeKey> extends AuthNextStep
5351
@override
5452
List<Object?> get props => [
5553
signInStep,
56-
challengeParameters,
5754
missingAttributes,
5855
];
5956

packages/amplify_core/lib/src/types/auth/sign_in/auth_next_sign_in_step.g.dart

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 82 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ void main() {
2626
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
2727
late String username;
2828
late String password;
29+
CognitoSignInOptions options = const CognitoSignInOptions(
30+
authFlowType: AuthenticationFlowType.customAuth);
2931
group(
3032
'custom auth passwordless signIn',
3133
() {
@@ -34,9 +36,7 @@ void main() {
3436
});
3537

3638
setUpAll(() async {
37-
await configureAuth(
38-
customAuth: true,
39-
);
39+
await configureAuth();
4040
// create new user for each test
4141
username = generateUsername();
4242
password = generatePassword();
@@ -50,35 +50,11 @@ void main() {
5050
});
5151

5252
testWidgets(
53-
'Unconfirmed user sign in throws UserNotConfirmedException (even when password not verified)',
54-
(WidgetTester tester) async {
55-
var unconfirmedUsername = '${generateUsername()}unconfirmedUSer';
56-
await Amplify.Auth.signUp(
57-
username: unconfirmedUsername,
58-
password: password,
59-
options: CognitoSignUpOptions(
60-
userAttributes: {
61-
CognitoUserAttributeKey.email: '[email protected]',
62-
CognitoUserAttributeKey.phoneNumber: '+15555555555',
63-
},
64-
),
65-
);
66-
67-
expect(
68-
Amplify.Auth.signIn(username: unconfirmedUsername, password: null),
69-
throwsA(
70-
isA<UserNotConfirmedException>(),
71-
),
72-
);
73-
},
74-
);
75-
76-
testWidgets(
77-
'signIn should return data from the auth challenge lambda',
53+
'signIn should return data from the auth challenge lambda (passwordless)',
7854
(WidgetTester tester) async {
7955
final res = await Amplify.Auth.signIn(
8056
username: username,
81-
password: null,
57+
options: options,
8258
);
8359
expect(
8460
res.isSignedIn,
@@ -89,6 +65,14 @@ void main() {
8965
res.nextStep!.additionalInfo?['test-key'],
9066
'test-value',
9167
);
68+
expect(
69+
res.nextStep!.additionalInfo?['USERNAME'],
70+
isNotNull,
71+
);
72+
expect(
73+
res.nextStep!.additionalInfo?.length,
74+
2,
75+
);
9276
expect(
9377
res.nextStep?.signInStep,
9478
'CONFIRM_SIGN_IN_WITH_CUSTOM_CHALLENGE',
@@ -101,7 +85,7 @@ void main() {
10185
(WidgetTester tester) async {
10286
await Amplify.Auth.signIn(
10387
username: username,
104-
password: null,
88+
options: options,
10589
);
10690
// '123' is the arbitrary challenge answer defined in lambda code
10791
final res = await Amplify.Auth.confirmSignIn(
@@ -117,7 +101,10 @@ void main() {
117101
testWidgets(
118102
'an incorrect challenge reply should throw a NotAuthorizedException',
119103
(WidgetTester tester) async {
120-
await Amplify.Auth.signIn(username: username, password: null);
104+
await Amplify.Auth.signIn(
105+
username: username,
106+
options: options,
107+
);
121108
// '123' is the arbitrary challenge answer defined in lambda code
122109
expect(
123110
Amplify.Auth.confirmSignIn(confirmationValue: 'wrong'),
@@ -127,6 +114,70 @@ void main() {
127114
);
128115
},
129116
);
117+
118+
testWidgets(
119+
'if a password is provided but is incorrect, throw NotAuthorizedException',
120+
(WidgetTester tester) async {
121+
// '123' is the arbitrary challenge answer defined in lambda code
122+
expect(
123+
Amplify.Auth.signIn(
124+
username: username,
125+
password: 'wrong',
126+
options: options,
127+
),
128+
throwsA(
129+
isA<NotAuthorizedException>(),
130+
),
131+
);
132+
},
133+
);
134+
135+
testWidgets(
136+
'a correct password and correct challenge reply should sign in the user',
137+
(WidgetTester tester) async {
138+
await Amplify.Auth.signIn(
139+
username: username,
140+
password: password,
141+
options: options,
142+
);
143+
// '123' is the arbitrary challenge answer defined in lambda code
144+
final res = await Amplify.Auth.confirmSignIn(
145+
confirmationValue: '123',
146+
);
147+
expect(
148+
res.isSignedIn,
149+
true,
150+
);
151+
},
152+
);
153+
154+
testWidgets(
155+
'signIn should return data from the auth challenge lambda (with password)',
156+
(WidgetTester tester) async {
157+
final res = await Amplify.Auth.signIn(
158+
username: username,
159+
password: password,
160+
options: options,
161+
);
162+
expect(
163+
res.isSignedIn,
164+
false,
165+
);
166+
// additionalInfo key values defined in lambda code
167+
expect(
168+
res.nextStep!.additionalInfo?['test-key'],
169+
'test-value',
170+
);
171+
expect(
172+
res.nextStep!.additionalInfo?.length,
173+
1,
174+
);
175+
expect(
176+
res.nextStep?.signInStep,
177+
'CONFIRM_SIGN_IN_WITH_CUSTOM_CHALLENGE',
178+
);
179+
},
180+
);
130181
},
131182
);
132183
}

packages/auth/amplify_auth_cognito/example/integration_test/sign_in_sign_out_test.dart

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,10 @@ void main() {
8282
);
8383
});
8484

85-
testWidgets(
86-
'should throw an InvalidStateException if a user is already signed in',
87-
(WidgetTester tester) async {
88-
await Amplify.Auth.signIn(username: username, password: password);
89-
expect(
90-
Amplify.Auth.signIn(username: username, password: password),
91-
throwsA(isA<InvalidStateException>()),
92-
);
85+
testWidgets('additionalInfo should be null', (WidgetTester tester) async {
86+
final result =
87+
await Amplify.Auth.signIn(username: username, password: password);
88+
expect(result.nextStep?.additionalInfo, isNull);
9389
});
9490
});
9591

packages/auth/amplify_auth_cognito/example/integration_test/utils/setup_utils.dart

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,16 @@
1313
// permissions and limitations under the License.
1414
//
1515

16-
import 'dart:convert';
17-
1816
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
1917
import 'package:amplify_auth_cognito_example/amplifyconfiguration.dart';
2018
import 'package:amplify_flutter/amplify_flutter.dart';
2119

2220
Future<void> configureAuth(
23-
{List<AmplifyPluginInterface> additionalPlugins = const [],
24-
bool customAuth = false}) async {
21+
{List<AmplifyPluginInterface> additionalPlugins = const []}) async {
2522
if (!Amplify.isConfigured) {
2623
final authPlugin = AmplifyAuthCognito();
27-
String config = _createConfig(amplifyconfig, customAuth: customAuth);
2824
await Amplify.addPlugins([authPlugin, ...additionalPlugins]);
29-
await Amplify.configure(config);
25+
await Amplify.configure(amplifyconfig);
3026
}
3127
}
3228

@@ -38,15 +34,3 @@ Future<void> signOutUser() async {
3834
// Ignore a signOut error because we only care when someone signed in.
3935
}
4036
}
41-
42-
// parse json, and switch auth mode if required by test
43-
String _createConfig(String amplifyconfig, {bool customAuth = false}) {
44-
String config = amplifyconfig;
45-
if (customAuth) {
46-
var configString = jsonDecode(amplifyconfig);
47-
configString['auth']['plugins']['awsCognitoAuthPlugin']['Auth']['Default']
48-
['authenticationFlowType'] = 'CUSTOM_AUTH';
49-
config = jsonEncode(configString);
50-
}
51-
return config;
52-
}
Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,43 @@
1+
// Scenario A: Password was not sent in client library API request, so SRP was not performed
2+
// Scenario B: Password was sent in client library API request, so SRP was performed and verification expected in Lambda
3+
14
exports.handler = async (event) => {
2-
if (event.request.session.length == 1 && event.request.session[0].challengeName == 'SRP_A') {
5+
// Scenario A: Step 1
6+
if (event.request.session.length == 0) {
37
event.response.issueTokens = false;
48
event.response.failAuthentication = false;
59
event.response.challengeName = 'CUSTOM_CHALLENGE';
6-
} else if (event.request.session.length == 2 && event.request.session[1].challengeName == 'CUSTOM_CHALLENGE' && event.request.session[1].challengeResult == true) {
7-
event.response.issueTokens = true;
8-
event.response.failAuthentication = false;
10+
// Scenario B: Step 1
11+
} else if (
12+
event.request.session.length == 1 &&
13+
event.request.session[0].challengeName == 'SRP_A'
14+
) {
15+
event.response.issueTokens = false;
16+
event.response.failAuthentication = false;
17+
event.response.challengeName = 'PASSWORD_VERIFIER'
18+
// Scenario A: Step 2
19+
} else if (
20+
event.request.session.length == 2 &&
21+
event.request.session[1].challengeName == 'PASSWORD_VERIFIER' &&
22+
event.request.session[1].challengeResult == true
23+
) {
24+
event.response.issueTokens = false;
25+
event.response.failAuthentication = false;
26+
event.response.challengeName = 'CUSTOM_CHALLENGE';
27+
// Scenario A: Step 3 OR Scenario B Step 2
28+
} else if (
29+
(event.request.session.length == 1 &&
30+
event.request.session[0].challengeName == 'CUSTOM_CHALLENGE' &&
31+
event.request.session[0].challengeResult == true) ||
32+
(event.request.session.length == 3 &&
33+
event.request.session[2].challengeName == 'CUSTOM_CHALLENGE' &&
34+
event.request.session[2].challengeResult == true)
35+
) {
36+
event.response.issueTokens = true;
37+
event.response.failAuthentication = false;
938
} else {
10-
event.response.issueTokens = false;
11-
event.response.failAuthentication = true;
39+
event.response.issueTokens = false;
40+
event.response.failAuthentication = true;
1241
}
1342
return event;
1443
};

packages/auth/amplify_auth_cognito_dart/lib/src/auth_plugin_impl.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface
454454
codeDeliveryDetails: _getChallengeDeliveryDetails(
455455
state.challengeParameters,
456456
),
457-
challengeParameters: state.challengeParameters,
457+
additionalInfo: state.challengeParameters,
458458
missingAttributes: state.requiredAttributes,
459459
),
460460
);
@@ -519,7 +519,7 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface
519519
codeDeliveryDetails: _getChallengeDeliveryDetails(
520520
state.challengeParameters,
521521
),
522-
challengeParameters: state.challengeParameters,
522+
additionalInfo: state.challengeParameters,
523523
missingAttributes: state.requiredAttributes,
524524
),
525525
);

0 commit comments

Comments
 (0)