【问题标题】:Cannot authenticate via pkce flutter app with keycloak using openid_client无法使用 openid_client 通过带有 keycloak 的 pkce Flutter 应用程序进行身份验证
【发布时间】:2021-12-21 15:05:49
【问题描述】:

我有以下 KeyCloak 客户端配置,以使用 pkce 身份验证流程:

Realm: REALM

Client ID:              pkce-client
Client Protocol:        openid-connect
Access Type:            public
Standard Flow Enabled:  ON
Valid Redirect URIs:    http://localhost:4200/ 

Advanced Settings:
Proof Key for Code Exchange Code Challenge Method: S256

我尝试通过 openid_client 在带有 iOS 模拟器的颤振应用程序中进行身份验证 https://pub.dev/packages/openid_client这样的

  authenticate() async {

    var uri = Uri.parse('http://$localhost:8180/auth/realms/REALM');
    var clientId = 'pkce-client';
    var scopes = List<String>.of(['profile', 'openid']);
    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 auth = await authenticator.authorize();
    var token= await auth.getTokenResponse();
    return token;
  }

但它只给了我这个网络视图:

运行 KeyCloak 的终端给了我以下几行:

INFO  [org.keycloak.protocol.oidc.endpoints.AuthorizationEndpointChecker] (default task-34) PKCE enforced Client without code challenge method.
WARN  [org.keycloak.events] (default task-34) type=LOGIN_ERROR, realmId=REALM, clientId=pkce-client, userId=null, ipAddress=127.0.0.1, error=invalid_request, response_type=code, redirect_uri=http://localhost:4200/, response_mode=query 

当使用 Postman 时,它可以工作,它为我提供了登录页面:

但是我那里有额外的参数,不知道在Authenticator(..)哪里添加,使用openid_client时,状态会自动添加。

state: <state>
code_challenge: <code-challenge>
code_challenge_method: S256 

我需要在 openid_client 方法的某处添加这些 code_challenge 参数吗?

或者我在使用应用程序时是否需要更改重定向 URL?我尝试使用此处建议的 package_name (https://githubmemory.com/repo/appsup-dart/openid_client/issues/32),但它也不起作用。

【问题讨论】:

    标签: flutter keycloak openid-connect pkce


    【解决方案1】:

    the source code:

          : flow = redirectUri == null
                ? Flow.authorizationCodeWithPKCE(client)
                : Flow.authorizationCode(client)
    

    您已指定redirectUri,因此使用了authorizationCode 流。但是在这种情况下你想要authorizationCodeWithPKCE 流。因此,只需确保 redirectUri 为 null 并使用正确的 PKCE 流(带有正确的 url 参数,例如 code_challenge)。

    【讨论】:

    • 是否有可能使用使用颤振构建的自定义登录和注册页面并仅发送凭据?
    猜你喜欢
    • 2022-01-18
    • 2020-07-03
    • 2022-01-16
    • 2019-01-14
    • 2017-03-03
    • 2019-07-28
    • 1970-01-01
    • 2022-10-07
    • 2020-09-01
    相关资源
    最近更新 更多