admin管理员组

文章数量:1123084

I have been looking at some examples on the internet of how to perform the authentication process of a flutter app using keycloak. I have seen some examples of how it could be done, but I have not been able to perform the correct authentication. I have already managed to get a webview to be created to place the authentication credentials, but I have still encountered some problems as shown below. I have seen this post, which has helped me guide this process, but I still cannot get it to work. I'm getting an error message when using that redirecturi structure. Keycloak tells me that the url is invalid and therefore it doesn't even allow me to get to the authentication step.

I've tried changing the urls, but if I put a normal website (http/https), the keycloak error is removed but the authentication window doesn't close and it doesn't return me to the app, so the authentication fails.

This is my authentication configuration in flutter:

Future<bool> login() async {
_assertInitialization();
try {
  tokenResponse = await _appAuth.authorizeAndExchangeCode(
    AuthorizationTokenRequest(
      "fe-dashboard-mobile", //client id
      "cu.havanaclub.fe_dashboard_mobile://auth", //redirect uri
      discoveryUrl: ":24001/realms/prod-env/.well-known/openid-configuration",
      issuer: _keycloakConfig.issuer,
      scopes: ['openid', 'profile'],
      promptValues: ['login'],
      allowInsecureConnections: true,
      clientSecret: _keycloakConfig.clientSecret,
    ),
  );

  if (tokenResponse.isValid) {
    if (refreshToken != null) {
      await _secureStorage.write(
        key: _refreshTokenKey,
        value: refreshToken,
      );
    }
  } else {
    developer.log('Invalid token response.', name: 'keycloak_wrapper');
  }

  _streamController.add(tokenResponse.isValid);
  return tokenResponse.isValid;
} catch (e, s) {
  print('$e');
  print('$s');
  onError('Failed to login.', e, s);
  return false;
}

}

And this is my keycloak configuration for that client. I am using version 23.0.4:

Please could you help me solve this problem?

I have been looking at some examples on the internet of how to perform the authentication process of a flutter app using keycloak. I have seen some examples of how it could be done, but I have not been able to perform the correct authentication. I have already managed to get a webview to be created to place the authentication credentials, but I have still encountered some problems as shown below. I have seen this post, which has helped me guide this process, but I still cannot get it to work. I'm getting an error message when using that redirecturi structure. Keycloak tells me that the url is invalid and therefore it doesn't even allow me to get to the authentication step.

I've tried changing the urls, but if I put a normal website (http/https), the keycloak error is removed but the authentication window doesn't close and it doesn't return me to the app, so the authentication fails.

This is my authentication configuration in flutter:

Future<bool> login() async {
_assertInitialization();
try {
  tokenResponse = await _appAuth.authorizeAndExchangeCode(
    AuthorizationTokenRequest(
      "fe-dashboard-mobile", //client id
      "cu.havanaclub.fe_dashboard_mobile://auth", //redirect uri
      discoveryUrl: "http://10.10.13.77:24001/realms/prod-env/.well-known/openid-configuration",
      issuer: _keycloakConfig.issuer,
      scopes: ['openid', 'profile'],
      promptValues: ['login'],
      allowInsecureConnections: true,
      clientSecret: _keycloakConfig.clientSecret,
    ),
  );

  if (tokenResponse.isValid) {
    if (refreshToken != null) {
      await _secureStorage.write(
        key: _refreshTokenKey,
        value: refreshToken,
      );
    }
  } else {
    developer.log('Invalid token response.', name: 'keycloak_wrapper');
  }

  _streamController.add(tokenResponse.isValid);
  return tokenResponse.isValid;
} catch (e, s) {
  print('$e');
  print('$s');
  onError('Failed to login.', e, s);
  return false;
}

}

And this is my keycloak configuration for that client. I am using version 23.0.4:

Please could you help me solve this problem?

Share Improve this question asked 5 hours ago Gabriel Alberto Pérez GuerraGabriel Alberto Pérez Guerra 557 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I have reviewed other articles and found a clue to the problem in this one. The root cause of the problem of not performing a proper redirection is that the package name contains "_", and keycloak does not interpret it correctly. I had to modify the build.gradle file by changing the appAuthRedirectScheme field to the value "fe".

Then I modified the flutter login code, leaving it as follows

tokenResponse = await _appAuth.authorizeAndExchangeCode(
    AuthorizationTokenRequest(
      "fe-dashboard-mobile", //client id
      "fe://auth", //redirect uri
      discoveryUrl: "http://10.10.13.77:24001/realms/prod-env/.well-known/openid-configuration",
      issuer: _keycloakConfig.issuer,
      scopes: ['openid', 'profile'],
      promptValues: ['login'],
      allowInsecureConnections: true,
      clientSecret: _keycloakConfig.clientSecret,
    ),
  );

In keycloak I have modified the option valid redirect uri, leaving it as follows:

This is how I got it to work for me. I hope this solution works for someone else who needs it.

本文标签: Authentication error in a flutter app using keycloakStack Overflow