promoteAnonymousUser method

Future<UserInfo> promoteAnonymousUser({
  1. required String redirectURI,
  2. String? wechatRedirectURI,
  3. List<String>? uiLocales,
  4. ColorScheme? colorScheme,
})

Implementation

Future<UserInfo> promoteAnonymousUser({
  required String redirectURI,
  String? wechatRedirectURI,
  List<String>? uiLocales,
  ColorScheme? colorScheme,
}) async {
  final kid = await _storage.getAnonymousKeyID(name);
  if (kid == null) {
    throw Exception("anonymous kid not found");
  }
  final challengeResponse =
      await _apiClient.getChallenge("anonymous_request");
  final now = DateTime.now().toUtc().millisecondsSinceEpoch / 1000;
  final payload = {
    "iat": now,
    "exp": now + 300,
    "challenge": challengeResponse.token,
    "action": "promote",
  };
  final jwt = await native.signWithAnonymousPrivateKey(
    kid: kid,
    payload: payload,
  );
  final loginHint =
      Uri.parse("https://authgear.com/login_hint").replace(queryParameters: {
    "type": "anonymous",
    "jwt": jwt,
  }).toString();

  final codeVerifier = CodeVerifier(_rng);
  final oidcRequest = OIDCAuthenticationRequest(
    clientID: clientID,
    redirectURI: redirectURI,
    responseType: ResponseType.code,
    scope: [
      "openid",
      "offline_access",
      "https://authgear.com/scopes/full-access",
    ],
    isSsoEnabled: isSsoEnabled,
    codeChallenge: codeVerifier.codeChallenge,
    prompt: [PromptOption.login],
    loginHint: loginHint,
    uiLocales: uiLocales,
    colorScheme: colorScheme,
    wechatRedirectURI: wechatRedirectURI,
  );
  final config = await _apiClient.fetchOIDCConfiguration();
  final authenticationURL = Uri.parse(config.authorizationEndpoint)
      .replace(queryParameters: oidcRequest.toQueryParameters());

  if (wechatRedirectURI != null) {
    await native.registerWechatRedirectURI(
        onWechatRedirectURI: _onWechatRedirectURI,
        wechatRedirectURI: wechatRedirectURI);
  }

  final resultURL = await _uiImplementation.openAuthorizationURL(
    url: authenticationURL.toString(),
    redirectURI: redirectURI,
    shareCookiesWithDeviceBrowser: isSsoEnabled,
  );
  final userInfo = await internalFinishAuthentication(
      url: Uri.parse(resultURL),
      redirectURI: redirectURI,
      codeVerifier: codeVerifier);
  await _disableAnonymous();
  await disableBiometric();
  return userInfo;
}