Keycloak Rest API 获取所有可用资源

发布于 2025-01-10 05:53:18 字数 2201 浏览 0 评论 0原文

我正在尝试列出客户端有权访问的所有资源。我不知道如何拨打电话。 我已经使用了这个卷曲

curl -X GET \
  http://$URL/auth/realms/$RELM/authz/resource-server/resource \
  -H 'Authorization: Bearer$TOKEN' \
  -H 'cache-control: no-cache'

到目前为止 ,但我得到了这样的回应: {“error”:“RESTEASY003210:找不到完整路径的资源:http://localhost:8070/auth/realms/argo/authz/resource-server/resource”}

有人可以帮我弄清楚如何列出所有资源,如果资源不在列表中则创建新资源?


实施的解决方案:

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, $this->keyCloakURL . '/realms/' . $this->relmName . '/protocol/openid-connect/token');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,
        "audience=" . KEYCLOAK_CLIENT_NAME . "&grant_type=urn:ietf:params:oauth:grant-type:uma-ticket&response_include_resource_name=true");
    $authorization = "Authorization: Bearer " . $user_token['access_token'];
    curl_setopt($ch, CURLOPT_HTTPHEADER, array (
            'Content-Type: application/x-www-form-urlencoded',
            $authorization
    ));
    $result = curl_exec($ch);
    if(curl_errno($ch))
    {
        echo 'curl error';
        return false;
    }
    
    $result = json_decode($result, true);
    curl_close($ch);
    if(isset($result['access_token']) && !empty($result['access_token']))
    {
        $parts = explode('.', $result['access_token']);
        if(!isset($parts[1]))
        {
            return false;
        }
        $info = $this->base64UrlDecode($parts[1]);
        $info = json_decode($info, true);
        
        $return = array ();
        if(isset($info['authorization']['permissions']))
        {
            $permissions = $info['authorization']['permissions'];
            foreach($permissions as $ecahPermission)
            {
                if(isset($ecahPermission['scopes']))
                {
                    //                      $scopes = array_map('strtolower', $ecahPermission['scopes']);
                    $return[$ecahPermission['rsname']] = $ecahPermission['scopes'];
                }
            }
        }
        
        return $return;
    }
    
    return false;

i am trying to list all resources client have access to. I am unable to figure out how to to make the call. I have used this curl

curl -X GET \
  http://$URL/auth/realms/$RELM/authz/resource-server/resource \
  -H 'Authorization: Bearer$TOKEN' \
  -H 'cache-control: no-cache'

so far but i am getting this response :
{"error":"RESTEASY003210: Could not find resource for full path: http://localhost:8070/auth/realms/argo/authz/resource-server/resource"}

Can someone help me to figure out how i can list all resources and if resource is not in the list to create new one ?


SOLUTION that is implementd:

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, $this->keyCloakURL . '/realms/' . $this->relmName . '/protocol/openid-connect/token');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,
        "audience=" . KEYCLOAK_CLIENT_NAME . "&grant_type=urn:ietf:params:oauth:grant-type:uma-ticket&response_include_resource_name=true");
    $authorization = "Authorization: Bearer " . $user_token['access_token'];
    curl_setopt($ch, CURLOPT_HTTPHEADER, array (
            'Content-Type: application/x-www-form-urlencoded',
            $authorization
    ));
    $result = curl_exec($ch);
    if(curl_errno($ch))
    {
        echo 'curl error';
        return false;
    }
    
    $result = json_decode($result, true);
    curl_close($ch);
    if(isset($result['access_token']) && !empty($result['access_token']))
    {
        $parts = explode('.', $result['access_token']);
        if(!isset($parts[1]))
        {
            return false;
        }
        $info = $this->base64UrlDecode($parts[1]);
        $info = json_decode($info, true);
        
        $return = array ();
        if(isset($info['authorization']['permissions']))
        {
            $permissions = $info['authorization']['permissions'];
            foreach($permissions as $ecahPermission)
            {
                if(isset($ecahPermission['scopes']))
                {
                    //                      $scopes = array_map('strtolower', $ecahPermission['scopes']);
                    $return[$ecahPermission['rsname']] = $ecahPermission['scopes'];
                }
            }
        }
        
        return $return;
    }
    
    return false;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文