Skip to content

changed initiateAuth to getTokensFromRefreshTokenAPI in refresh token… #6293

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

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft

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 @@ -308,7 +308,7 @@ class _PublicViewState extends State<PublicView>
}
},
activeTrackColor: Colors.lightGreenAccent,
activeColor: Colors.green,
activeThumbColor: Colors.green,
),

Padding(padding: EdgeInsets.all(5.0)),
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 @@ -7,14 +7,11 @@ import 'package:amplify_auth_cognito_dart/amplify_auth_cognito_dart.dart';
import 'package:amplify_auth_cognito_dart/src/credentials/auth_plugin_credentials_provider.dart';
import 'package:amplify_auth_cognito_dart/src/credentials/cognito_keys.dart';
import 'package:amplify_auth_cognito_dart/src/credentials/device_metadata_repository.dart';
import 'package:amplify_auth_cognito_dart/src/flows/constants.dart';
import 'package:amplify_auth_cognito_dart/src/flows/helpers.dart';
import 'package:amplify_auth_cognito_dart/src/model/session/cognito_sign_in_details.dart';
import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity.dart'
hide NotAuthorizedException;
import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart'
as cognito_idp;
import 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/model/analytics_metadata_type.dart';
import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart';
import 'package:amplify_auth_cognito_dart/src/state/state.dart';
import 'package:amplify_core/amplify_core.dart';
Expand Down Expand Up @@ -359,7 +356,6 @@ final class FetchAuthSessionStateMachine
AuthResult<String> userSubResult;
AuthResult<AWSCredentials> credentialsResult;
AuthResult<String> identityIdResult;

final hasUserPool = _authConfig?.userPoolId != null;
var userPoolTokens = result.userPoolTokens;
if (!hasUserPool) {
Expand Down Expand Up @@ -511,38 +507,25 @@ final class FetchAuthSessionStateMachine
final deviceSecrets = await getOrCreate<DeviceMetadataRepository>().get(
userPoolTokens.username,
);
final refreshRequest = cognito_idp.InitiateAuthRequest.build((b) {
b
..authFlow = cognito_idp.AuthFlowType.refreshTokenAuth
..clientId = _authConfig?.userPoolClientId
..authParameters.addAll({
CognitoConstants.refreshToken: userPoolTokens.refreshToken,
})
..analyticsMetadata = get<AnalyticsMetadataType>()?.toBuilder();

// ignore: invalid_use_of_internal_member
if (_authConfig?.appClientSecret != null &&
_authConfig?.userPoolClientId != null) {
b.authParameters[CognitoConstants.challengeParamSecretHash] =
computeSecretHash(
userPoolTokens.username,
_authConfig!.userPoolClientId!,
// ignore: invalid_use_of_internal_member
_authConfig!.appClientSecret!,
);
}

// seems we dont support client metadata
final refreshRequest = cognito_idp.GetTokensFromRefreshTokenRequest.build((
b,
) {
b
..refreshToken = userPoolTokens.refreshToken
..clientId = _authConfig?.userPoolClientId;
final deviceKey = deviceSecrets?.deviceKey;
if (deviceKey != null) {
b.authParameters[CognitoConstants.challengeParamDeviceKey] = deviceKey;
b.deviceKey = deviceKey;
}
});
try {
final result = await _withZoneOverrides(
() => _cognitoIdpClient.initiateAuth(refreshRequest).result,
() =>
_cognitoIdpClient.getTokensFromRefreshToken(refreshRequest).result,
);
final authResult = result.authenticationResult;

final accessToken = authResult?.accessToken;
final refreshToken = authResult?.refreshToken;
final idToken = authResult?.idToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ void main() {

stateMachine.addInstance<CognitoIdentityProviderClient>(
MockCognitoIdentityProviderClient(
initiateAuth: expectAsync1(
(_) async => throw const AuthNotAuthorizedException(
'Refresh Token has expired.',
),
),
getTokensFromRefreshToken: () async =>
throw const AuthNotAuthorizedException(
'Refresh Token has expired.',
),
),
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ void main() {
final mockIdp = MockCognitoIdentityProviderClient(
initiateAuth: (p0) async =>
throw InternalErrorException(message: 'Invalid token'),
getTokensFromRefreshToken: () async =>
throw const AuthNotAuthorizedException('Auth not authorized'),
);
stateMachine.addInstance<CognitoIdentityProviderClient>(mockIdp);

Expand Down
Loading
Loading