Skip to content

fix(auth)!: Fetch Auth Session offline behavior #2585

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

Merged
merged 29 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
de04055
feat(auth): update fetch auth session behavior
Jordan-Nelson Jan 6, 2023
db43e95
fix(auth): remove getAWSCredentials arg from native plugin
Jordan-Nelson Jan 6, 2023
c728e66
chore: update federateToIdentityPool test
Jordan-Nelson Jan 6, 2023
09bf02a
chore: CognitoAuthSessionResult doc comments
Jordan-Nelson Jan 9, 2023
2075777
chore: add/update tests for fetchAuthSession
Jordan-Nelson Jan 17, 2023
453b6f2
chore: add tests for unauth access, fix failing tests
Jordan-Nelson Jan 18, 2023
558dbfe
chore: update fetchAuthSession integ tests
Jordan-Nelson Jan 18, 2023
c8a0992
fix(authenticator)!: Update authenticator offline behavior
Jordan-Nelson Jan 18, 2023
c022843
chore: add comment to CognitoAuthSession
Jordan-Nelson Jan 18, 2023
36c880d
chore: update failing android test
Jordan-Nelson Jan 18, 2023
5c6aadf
chore: expose results from CognitoAuthSession, throw AuthExceptions
Jordan-Nelson Jan 23, 2023
3a5806b
chore: make tests more realistic
Jordan-Nelson Jan 23, 2023
0130b1a
chore: make AWSResult AWSDebuggable
Jordan-Nelson Jan 23, 2023
116c625
chore: early exit for no identity pool
Jordan-Nelson Jan 23, 2023
f2ee6f3
chore: update CognitoAuthSession.toJson
Jordan-Nelson Jan 24, 2023
165e56f
chore: add doc comment to AWSResultType
Jordan-Nelson Jan 24, 2023
ed256cd
chore: update fetchAuthSession stub
Jordan-Nelson Jan 24, 2023
63700c1
chore: undo sample app changes
Jordan-Nelson Jan 24, 2023
17dc8b9
chore: simplify AuthProvider
Jordan-Nelson Jan 24, 2023
381931c
chore: revert changes to SignedOutException
Jordan-Nelson Jan 25, 2023
0ad80c3
chore: add valueOrNull to AWSResult
Jordan-Nelson Jan 25, 2023
16524b8
chore: add back members to CognitoAuthSession, update toJson
Jordan-Nelson Jan 25, 2023
84a3ee9
chore: add getAWSCredentials back as deprecated
Jordan-Nelson Jan 25, 2023
d220427
chore: remove empty CognitoSessionOptions
Jordan-Nelson Jan 25, 2023
c3cf041
chore: refactor exception handling
Jordan-Nelson Jan 25, 2023
760f539
chore: force unwrap awsCredentials & identityId
Jordan-Nelson Jan 25, 2023
2a94899
chore: update authenticator to catch Exception not object
Jordan-Nelson Jan 25, 2023
9c4ec36
chore: add stacktrace to AWSResult
Jordan-Nelson Jan 25, 2023
dc791a5
fix: amplify_test
Jordan-Nelson Jan 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ void main() {
final cognitoPlugin = Amplify.Auth.getPlugin(
AmplifyAuthCognito.pluginKey,
);
final session = await cognitoPlugin.fetchAuthSession(
options: const CognitoSessionOptions(getAWSCredentials: true),
);
final session = await cognitoPlugin.fetchAuthSession();
expect(session.credentials, isNotNull);

final restApi = config.api!.awsPlugin!.values
Expand Down Expand Up @@ -170,9 +168,7 @@ void main() {
final cognitoPlugin = Amplify.Auth.getPlugin(
AmplifyAuthCognito.pluginKey,
);
final session = await cognitoPlugin.fetchAuthSession(
options: const CognitoSessionOptions(getAWSCredentials: true),
);
final session = await cognitoPlugin.fetchAuthSession();
expect(session.credentials, isNotNull);

final restApi = config.api!.awsPlugin!.values
Expand Down Expand Up @@ -226,9 +222,7 @@ void main() {
final cognitoPlugin = Amplify.Auth.getPlugin(
AmplifyAuthCognito.pluginKey,
);
final session = await cognitoPlugin.fetchAuthSession(
options: const CognitoSessionOptions(getAWSCredentials: true),
);
final session = await cognitoPlugin.fetchAuthSession();
expect(session.credentials, isNotNull);

final restOperation = Amplify.API.post(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ void main() {

asyncTest('replaces unauthenticated identity', (_) async {
// Get unauthenticated identity
final unauthSession = await cognitoPlugin.fetchAuthSession(
options: const CognitoSessionOptions(getAWSCredentials: true),
);
final unauthSession = await cognitoPlugin.fetchAuthSession();

final authSession = await federateToIdentityPool();
expect(
Expand All @@ -124,9 +122,7 @@ void main() {

asyncTest('can specify identity ID', (_) async {
// Get unauthenticated identity (doesn't matter, just need identity ID)
final unauthSession = await cognitoPlugin.fetchAuthSession(
options: const CognitoSessionOptions(getAWSCredentials: true),
);
final unauthSession = await cognitoPlugin.fetchAuthSession();
final identityId = unauthSession.identityId!;

final signInResult = await cognitoPlugin.signIn(
Expand Down Expand Up @@ -176,7 +172,7 @@ void main() {
});

asyncTest('can clear federation', (_) async {
await federateToIdentityPool();
final federateToIdentityPoolResult = await federateToIdentityPool();

await expectLater(
cognitoPlugin.clearFederationToIdentityPool(),
Expand All @@ -186,13 +182,13 @@ void main() {
final clearedSession = await cognitoPlugin.fetchAuthSession();
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
final clearedSession = await cognitoPlugin.fetchAuthSession();
final unauthSession = await cognitoPlugin.fetchAuthSession();

expect(
clearedSession.identityId,
isNull,
reason: 'Should clear session',
isNot(federateToIdentityPoolResult.identityId),
reason: 'Should clear session and refetch',
);
expect(
clearedSession.credentials,
isNull,
reason: 'Should clear session',
isNot(federateToIdentityPoolResult.credentials),
reason: 'Should clear session and refetch',
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,64 +19,60 @@ void main() {
final username = generateUsername();
final password = generatePassword();

setUpAll(() async {
await configureAuth();
group('unauthenticated access enabled', () {
setUpAll(() async {
await configureAuth();

await adminCreateUser(
username,
password,
autoConfirm: true,
verifyAttributes: true,
);
});

tearDownAll(Amplify.reset);

setUp(() async {
await signOutUser();
final res = await Amplify.Auth.signIn(
username: username,
password: password,
);
expect(res.isSignedIn, isTrue);
});
await adminCreateUser(
username,
password,
autoConfirm: true,
verifyAttributes: true,
);
});

asyncTest(
'should return user credentials if getAWSCredentials is true',
(_) async {
final res = await Amplify.Auth.fetchAuthSession(
options: const CognitoSessionOptions(getAWSCredentials: true),
) as CognitoAuthSession;
tearDownAll(Amplify.reset);

setUp(() async {
await signOutUser();
final res = await Amplify.Auth.signIn(
username: username,
password: password,
);
expect(res.isSignedIn, isTrue);
expect(isValidUserSub(res.userSub), isTrue);
expect(isValidIdentityId(res.identityId), isTrue);
expect(isValidAWSCredentials(res.credentials), isTrue);
expect(isValidAWSCognitoUserPoolTokens(res.userPoolTokens), isTrue);
},
);

asyncTest(
'should return user credentials without getAWSCredentials',
(_) async {
final res = await Amplify.Auth.fetchAuthSession() as CognitoAuthSession;
});

expect(res.isSignedIn, isTrue);
expect(isValidUserSub(res.userSub), isTrue);
expect(isValidIdentityId(res.identityId), isTrue);
expect(isValidAWSCredentials(res.credentials), isTrue);
expect(isValidAWSCognitoUserPoolTokens(res.userPoolTokens), isTrue);
},
);
asyncTest(
'should return user credentials',
(_) async {
final res =
await Amplify.Auth.fetchAuthSession() as CognitoAuthSession;
expect(res.isSignedIn, isTrue);
expect(isValidUserSub(res.userSub), isTrue);
expect(isValidIdentityId(res.identityId), isTrue);
expect(isValidAWSCredentials(res.credentials), isTrue);
expect(isValidAWSCognitoUserPoolTokens(res.userPoolTokens), isTrue);
},
);

asyncTest(
'should return isSignedIn as false if the user is signed out',
(_) async {
await Amplify.Auth.signOut();
group('user is signed out', () {
asyncTest(
'should return isSignedIn as false with credentials present',
(_) async {
await Amplify.Auth.signOut();
final res =
await Amplify.Auth.fetchAuthSession() as CognitoAuthSession;
expect(res.isSignedIn, isFalse);
expect(res.userPoolTokens, isNull);
expect(res.userSub, isNull);
expect(isValidIdentityId(res.identityId), isTrue);
expect(isValidAWSCredentials(res.credentials), isTrue);
},
);
});
});

final res = await Amplify.Auth.fetchAuthSession();
expect(res.isSignedIn, isFalse);
},
);
// TODO(Jordan-Nelson): add tests for unauthenticated access NOT enabled
// and user pool only.
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ void main() {

asyncTest('identity ID should be the same between sessions', (_) async {
// Get unauthenticated identity
final unauthSession = await Amplify.Auth.fetchAuthSession(
options: const CognitoSessionOptions(getAWSCredentials: true),
) as CognitoAuthSession;
final unauthSession =
await Amplify.Auth.fetchAuthSession() as CognitoAuthSession;

// Sign in
{
Expand All @@ -108,9 +107,8 @@ void main() {
}

// Get authenticated identity
final authSession = await Amplify.Auth.fetchAuthSession(
options: const CognitoSessionOptions(getAWSCredentials: true),
) as CognitoAuthSession;
final authSession =
await Amplify.Auth.fetchAuthSession() as CognitoAuthSession;
final authenticatedIdentity = authSession.identityId;
expect(
authenticatedIdentity,
Expand All @@ -132,9 +130,8 @@ void main() {
expect(signInRes.nextStep.signInStep, 'DONE');
}

final newSession = await Amplify.Auth.fetchAuthSession(
options: const CognitoSessionOptions(getAWSCredentials: true),
) as CognitoAuthSession;
final newSession =
await Amplify.Auth.fetchAuthSession() as CognitoAuthSession;
expect(
newSession.identityId,
authenticatedIdentity,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions packages/auth/amplify_auth_cognito/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ class _HomeScreenState extends State<HomeScreen> {
}

Future<void> _fetchAuthSession() async {
final authSession = await Amplify.Auth.fetchAuthSession(
options: const CognitoSessionOptions(getAWSCredentials: true),
) as CognitoAuthSession;
final authSession = await Amplify.Auth.fetchAuthSession();
_logger.info(
prettyPrintJson(authSession.toJson()),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,9 @@ class _NativeAmplifyAuthCognito
final CognitoAuthStateMachine _stateMachine;

@override
Future<NativeAuthSession> fetchAuthSession(
bool getAwsCredentials,
) async {
Future<NativeAuthSession> fetchAuthSession() async {
try {
final authSession = await _basePlugin.fetchAuthSession(
options: CognitoSessionOptions(getAWSCredentials: getAwsCredentials),
);
final authSession = await _basePlugin.fetchAuthSession();
final nativeAuthSession = NativeAuthSession(
isSignedIn: authSession.isSignedIn,
userSub: authSession.userSub,
Expand Down

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
Expand Up @@ -35,7 +35,7 @@ abstract class NativeAuthPlugin {
void exchange(Map<String, String> params);

@async
NativeAuthSession fetchAuthSession(bool getAwsCredentials);
NativeAuthSession fetchAuthSession();
}

@HostApi()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class NativeAuthPlugin(
return
}
MainScope().launch {
nativePlugin.fetchAuthSession(true) { session ->
nativePlugin.fetchAuthSession() { session ->
val couldNotFetchException = UnknownException("Could not fetch")
val userPoolTokens = if (session.userPoolTokens != null) {
val tokens = FlutterFactory.createAWSCognitoUserPoolTokens(
Expand Down

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
Expand Up @@ -10,6 +10,7 @@ import org.junit.Test
import org.mockito.kotlin.any
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.check
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify

Expand All @@ -20,9 +21,9 @@ internal class NativeAuthPluginTests {
val nativeAuthPlugin = NativeAuthPluginBindingsPigeon.NativeAuthPlugin(mockBinaryMessenger)
val mockCallback =
mock<NativeAuthPluginBindingsPigeon.NativeAuthPlugin.Reply<NativeAuthPluginBindingsPigeon.NativeAuthSession>>()
nativeAuthPlugin.fetchAuthSession(true, mockCallback)
nativeAuthPlugin.fetchAuthSession(mockCallback)
val callback = argumentCaptor<BinaryMessenger.BinaryReply>()
verify(mockBinaryMessenger).send(any(), any(), callback.capture())
verify(mockBinaryMessenger).send(any(), eq(null), callback.capture())
val codec = NativeAuthPluginBindingsPigeon.NativeAuthPlugin.getCodec()
val authSession = NativeAuthPluginBindingsPigeon.NativeAuthSession.Builder().apply {
setIsSignedIn(isSignedIn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,7 @@ Future<void> changePassword({
}

Future<CognitoAuthSession> fetchAuthSession() async {
final res = await Amplify.Auth.fetchAuthSession(
options: const CognitoSessionOptions(
getAWSCredentials: true,
),
);
final res = await Amplify.Auth.fetchAuthSession();
return res as CognitoAuthSession;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,8 @@ class AppComponent extends StatefulComponent {
AuthState startingAuthState;

try {
final session = await Amplify.Auth.fetchAuthSession(
options: const CognitoSessionOptions(
getAWSCredentials: true,
),
) as CognitoAuthSession;
final session =
await Amplify.Auth.fetchAuthSession() as CognitoAuthSession;
startingAuthState =
session.isSignedIn ? AuthState.authenticated : AuthState.login;
} on Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface<
// Try to refresh AWS credentials since Cognito requests will require
// them.
await fetchAuthSession(
options: const CognitoSessionOptions(getAWSCredentials: true),
options: const CognitoSessionOptions(),
);
if (options.globalSignOut) {
// Revokes the refresh token
Expand Down
Loading