authenticate method
Authenticate to this CARP service using a BuildContext, that opens the authentication page of the Identity Server using a secure web view from the OS.
The discovery URL is used to find the Identity Server.
Return the signed in user (with an OAuthToken access token), if successful. Throws a CarpServiceException if not successful.
Implementation
Future<CarpUser> authenticate() async {
final AuthorizationTokenResponse? response =
await appAuth.authorizeAndExchangeCode(
AuthorizationTokenRequest(
app.clientId, "${app.redirectURI}",
clientSecret: app.clientSecret ?? '',
discoveryUrl: "${app.discoveryURL}",
scopes: ['openid'], // To get an ID token
),
);
if (response != null) {
_currentUser = getCurrentUserProfile(response);
currentUser.authenticated(OAuthToken.fromTokenResponse(response));
_authEventController.add(AuthEvent.authenticated);
return currentUser;
}
// All other cases are treated as a failed attempt and throws an error
_authEventController.add(AuthEvent.failed);
_currentUser = null;
// auth error response from CARP is in the form
throw CarpServiceException(
httpStatus: HTTPStatus(401),
message: 'Authentication failed.',
);
}