Skip to content

Commit 4b5b1be

Browse files
authored
feat(auth): Refresh token rotation (#6293)
* changed initiateAuth to getTokensFromRefreshTokenAPI in refresh token function, changed tests to test rotation functionality * updated new refresh api to include app secret if available
1 parent bb432da commit 4b5b1be

File tree

6 files changed

+128
-121
lines changed

6 files changed

+128
-121
lines changed

packages/auth/amplify_auth_cognito_dart/lib/src/sdk/src/cognito_identity_provider/model/get_tokens_from_refresh_token_request.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/auth/amplify_auth_cognito_dart/lib/src/sdk/src/cognito_identity_provider/operation/get_tokens_from_refresh_token_operation.dart

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/auth/amplify_auth_cognito_dart/lib/src/state/machines/fetch_auth_session_state_machine.dart

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@ import 'package:amplify_auth_cognito_dart/amplify_auth_cognito_dart.dart';
77
import 'package:amplify_auth_cognito_dart/src/credentials/auth_plugin_credentials_provider.dart';
88
import 'package:amplify_auth_cognito_dart/src/credentials/cognito_keys.dart';
99
import 'package:amplify_auth_cognito_dart/src/credentials/device_metadata_repository.dart';
10-
import 'package:amplify_auth_cognito_dart/src/flows/constants.dart';
11-
import 'package:amplify_auth_cognito_dart/src/flows/helpers.dart';
1210
import 'package:amplify_auth_cognito_dart/src/model/session/cognito_sign_in_details.dart';
1311
import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity.dart'
1412
hide NotAuthorizedException;
1513
import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart'
1614
as cognito_idp;
17-
import 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/model/analytics_metadata_type.dart';
1815
import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart';
1916
import 'package:amplify_auth_cognito_dart/src/state/state.dart';
2017
import 'package:amplify_core/amplify_core.dart';
@@ -359,7 +356,6 @@ final class FetchAuthSessionStateMachine
359356
AuthResult<String> userSubResult;
360357
AuthResult<AWSCredentials> credentialsResult;
361358
AuthResult<String> identityIdResult;
362-
363359
final hasUserPool = _authConfig?.userPoolId != null;
364360
var userPoolTokens = result.userPoolTokens;
365361
if (!hasUserPool) {
@@ -511,38 +507,30 @@ final class FetchAuthSessionStateMachine
511507
final deviceSecrets = await getOrCreate<DeviceMetadataRepository>().get(
512508
userPoolTokens.username,
513509
);
514-
final refreshRequest = cognito_idp.InitiateAuthRequest.build((b) {
515-
b
516-
..authFlow = cognito_idp.AuthFlowType.refreshTokenAuth
517-
..clientId = _authConfig?.userPoolClientId
518-
..authParameters.addAll({
519-
CognitoConstants.refreshToken: userPoolTokens.refreshToken,
520-
})
521-
..analyticsMetadata = get<AnalyticsMetadataType>()?.toBuilder();
522-
523-
// ignore: invalid_use_of_internal_member
524-
if (_authConfig?.appClientSecret != null &&
525-
_authConfig?.userPoolClientId != null) {
526-
b.authParameters[CognitoConstants.challengeParamSecretHash] =
527-
computeSecretHash(
528-
userPoolTokens.username,
529-
_authConfig!.userPoolClientId!,
530-
// ignore: invalid_use_of_internal_member
531-
_authConfig!.appClientSecret!,
532-
);
533-
}
534510

535-
final deviceKey = deviceSecrets?.deviceKey;
511+
final deviceKey = deviceSecrets?.deviceKey;
512+
// ignore: invalid_use_of_internal_member
513+
final appClientSecret = _authConfig?.appClientSecret;
514+
515+
final refreshRequest = cognito_idp.GetTokensFromRefreshTokenRequest.build((
516+
b,
517+
) {
518+
b
519+
..refreshToken = userPoolTokens.refreshToken
520+
..clientId = _authConfig?.userPoolClientId;
536521
if (deviceKey != null) {
537-
b.authParameters[CognitoConstants.challengeParamDeviceKey] = deviceKey;
522+
b.deviceKey = deviceKey;
523+
}
524+
if (appClientSecret != null) {
525+
b.clientSecret = appClientSecret;
538526
}
539527
});
540528
try {
541529
final result = await _withZoneOverrides(
542-
() => _cognitoIdpClient.initiateAuth(refreshRequest).result,
530+
() =>
531+
_cognitoIdpClient.getTokensFromRefreshToken(refreshRequest).result,
543532
);
544533
final authResult = result.authenticationResult;
545-
546534
final accessToken = authResult?.accessToken;
547535
final refreshToken = authResult?.refreshToken;
548536
final idToken = authResult?.idToken;

packages/auth/amplify_auth_cognito_test/test/plugin/fetch_auth_session_test.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,10 @@ void main() {
4848

4949
stateMachine.addInstance<CognitoIdentityProviderClient>(
5050
MockCognitoIdentityProviderClient(
51-
initiateAuth: expectAsync1(
52-
(_) async => throw const AuthNotAuthorizedException(
53-
'Refresh Token has expired.',
54-
),
55-
),
51+
getTokensFromRefreshToken: () async =>
52+
throw const AuthNotAuthorizedException(
53+
'Refresh Token has expired.',
54+
),
5655
),
5756
);
5857
});

packages/auth/amplify_auth_cognito_test/test/plugin/sign_out_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ void main() {
275275
final mockIdp = MockCognitoIdentityProviderClient(
276276
initiateAuth: (p0) async =>
277277
throw InternalErrorException(message: 'Invalid token'),
278+
getTokensFromRefreshToken: () async =>
279+
throw const AuthNotAuthorizedException('Auth not authorized'),
278280
);
279281
stateMachine.addInstance<CognitoIdentityProviderClient>(mockIdp);
280282

0 commit comments

Comments
 (0)