【问题标题】:How to get refresh token after authenticate via pkce flutter app with keycloak using openid_client?使用openid_client通过带有ke​​ycloak的pkce颤振应用程序进行身份验证后如何获取刷新令牌?
【发布时间】:2022-01-16 10:25:47
【问题描述】:

我有以下 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 模拟器使用 Flutter App 进行身份验证时 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 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,
    );

    var auth = await authenticator.authorize();
    var token= await auth.getTokenResponse();
    return token;
  }

我收到以下回复:

如何使用刷新令牌获取新的访问令牌?

我试过了:

POST http://localhost:8180/auth/realms/REALM/protocol/openid-connect/token
Content-Type: application/x-www-form-urlencoded
client_id: pkce-client
grant_type: refresh_token
refresh_token: "received refresh token"

但我明白了:

{"error":"invalid_client","error_description":"Invalid client credentials"}

我需要如何准备刷新访问令牌的请求?

提前致谢

【问题讨论】:

    标签: flutter keycloak openid-connect refresh-token pkce


    【解决方案1】:

    问题的一个原因可能是您需要在请求中也包含 client_secret。如果客户是“机密”客户,则可能需要这样做。

    请参阅此处的讨论以获取更多详细信息。 Refresh access_token via refresh_token in Keycloak

    【讨论】:

    • 我使用的是pkce,所以我的客户端没有client_secret。我是否需要在另一个具有 client_secret 的客户端上检索刷新令牌?我想避免在应用中使用 client_secret。
    • PKCE和client_secret没有关系?
    • 访问类型是公开的,所以没有提供client_secret。
    猜你喜欢
    • 2021-12-21
    • 2022-01-18
    • 2021-03-06
    • 2020-07-03
    • 2016-02-15
    • 1970-01-01
    • 2020-08-18
    • 2021-11-13
    • 2014-10-30
    相关资源
    最近更新 更多