如何在我的 php 应用程序中使用 identityserver4

how I can use identityserver4 from my php app

我需要使用来自 fiba 网站的最新消息,他们提供了一个 swagger API 使用 identityserver4 作为授权系统

我们的应用程序是 PHP,我试图找到与 php 和身份服务器相关的东西,但我找不到。

我想从 php 向身份服务器发送请求并从中获取访问令牌。我该怎么做?

要获取您可以使用的令牌(它适用于我的安装):

curl -d "client_id=<YOURCLIENT>&client_secret=<YOURCLIENTSECRET>&grant_type=password&username=<youruser>&password=<topsecretpassword>&scope=default openid" -X POST http://youtidentityserver4.tld/oauth/connect/token

然后你会得到类似的东西:

{
  "access_token": "eyJhbGciOiJSUz....long token",
  "expires_in": 3600,
  "token_type": "Bearer"
}

接下来您可以使用以下请求获取用户信息:

curl -i http://youtidentityserver4.tld/oauth/connect/userinfo \
-H "scope: default openid" \
-H "Authorization: Bearer eyJhbGciOiJSUz....long token"

执行此操作后,您应该得到如下内容:

{
    "sub": "j2h4kh42k4242jhg4j2hg42k34gb2k"
}

取决于您在 IdentityServer4 后面配置的用户数据库。

sub = “主题”的缩写 => 用户的唯一标识符

在 /token 请求上使用您稍后要在 /userinfo 上使用的相同范围。 否则你会得到一个错误 "insufficient_scope":

HTTP/1.1 403 Forbidden
Content-Type: text/plain; charset=UTF-8
Content-Length: 0
Connection: keep-alive
Date: Thu, 20 Sep 2018 15:52:40 GMT
Server: Kestrel
Cache-Control: no-store, no-cache, max-age=0
Pragma: no-cache
WwwAuthentication: Bearer
WwwAuthentication: error="insufficient_scope"

如果您需要更多示波器,请查看 http://youtidentityserver4.tld/oauth/.well-known/openid-configuration 和 "scopes_supported"。 (将 youtidentityserver4.tld 替换为您的域)

要在 PHP 中实现所有这些,请使用一些众所周知的客户端,例如 curl extension directly or Guzzle