使用keycloak和openid_client验证flutter应用程序

问题描述 投票:0回答:1

我正在尝试通过openid_client对我的Flutter应用进行身份验证以进行密钥隐藏

在回购示例之后,我编写了这样的身份验证功能

authenticate() async {

  // parameters here just for the sake of the question
  var uri = Uri.parse('https://keycloak-url/auth/realms/myrealm');
  var clientId = 'my_client_id';
  var scopes = List<String>.of(['openid', 'profile']);
  var port = 4200;
  var redirectUri = Uri.parse('http://localhost:4200');

  var issuer = await Issuer.discover(uri);
  var client = new Client(issuer, clientId);

  urlLauncher(String url) async {
    if (await canLaunch(url)) {
      await launch(url, forceWebView: true);
    } else {
      throw 'Could not launch $url';
    }
  }

  var authenticator = new Authenticator(client,
      scopes: scopes,
      port: port,
      urlLancher: urlLauncher,
      redirectUri: redirectUri);

  var c = await authenticator.authorize();
  closeWebView();

  var token= await c.getTokenResponse();
  print(token);
  return token;
}

[当我调用该函数时,会出现一个webview弹出窗口,并且我可以通过密钥斗篷登录,但是当弹出窗口关闭时,我会在c.getTokenResponse()处收到此错误:

发生异常。NoSuchMethodError(NoSuchMethodError:在null上调用了getter'length'。接收者:null尝试调用:长度)

检查凭证c,我可以看到TokenResponse仅具有“状态”,“ session_state”和“代码”字段

我想念什么?

我正在尝试通过回购示例通过openid_client将我的Flutter应用验证为keycloak,我已经编写了一个类似authenticate()async {//这里的参数...]的验证函数, [

我已经在github(link)上得到了回答,所以我将解决方案复制到这里:
在移动设备上,您应该使用PKCE流程。当您在Authenticator构造函数中省略重定向uri时,将自动选择此选项。

所以应该是:


var authenticator = new Authenticator(client, scopes: scopes, port: port, urlLancher: urlLauncher,);

请确保将uri http://localhost:4200/(包括斜杠)添加到密钥斗篷中的Valid Redirect URIs

请确保将uri http://localhost:4200/(包括斜杠)添加到密钥斗篷中的有效重定向URI。
flutter client openid keycloak
1个回答
0
投票
在移动设备上,您应该使用PKCE流程。当您在Authenticator构造函数中省略重定向uri时,将自动选择此选项。

所以应该是:

© www.soinside.com 2019 - 2024. All rights reserved.