php – 从Azure Active Directory验证JWT

前端之家收集整理的这篇文章主要介绍了php – 从Azure Active Directory验证JWT前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已按照此处的说明获取Web API的访问令牌.

https://msdn.microsoft.com/en-us/library/azure/dn645542.aspx

我有这个工作,但在弄清楚如何验证PHP中的令牌时,文档是模糊的.

You can use the access token that is returned in the response to authenticate to a protected resources,such as a web API. Typically,the token is presented to the web API in an HTTP request using the Bearer scheme,which is described in RFC 6750. This specification explains how to use bearer tokens in HTTP requests to access protected resources.

When the web API receives and validates the token,it gives the native client application access to the web API.

如何在应用程序中验证JWT?我有一个PHP框架,它使用PHP openssl_verify()函数与令牌,签名,密钥和算法,但我收到错误,当我使用Azure的私钥与SHA256算法时:

openssl_verify(): supplied key param cannot be coerced into a public key

这让我相信我在PHP中使用的密钥验证不正确.目前,我正在使用我为Active Directory应用程序生成的私钥,当我点击oauth2 / token url(任何其他值导致没有令牌)时,它恰好与我用于“client_secret”参数的值相同生成所以这可能是正确的).

关键是类似于(但不是实际):

cLDQWERTYUI12asdqwezxctlkjpoiAn7yhjeutl8jsP=

我相信openssl需要有证书…如果是这样我似乎无法找到此证书在Azure门户中的位置.

我在这里错过了什么?我应该使用openssl_verify()验证JWT以及在Azure中哪里找到它的关键是什么?

谢谢

更新:

我在这里找到了公钥:https://login.windows.net/common/discovery/keys

但是我仍然无法使用提供的X5C来验证签名.你是如何用PHP做到的?

更新2:

我使用转换来使用’e’和’n’参数为公钥创建.pem文件.这收到了一个公钥.

现在,在使用它解密时,我会收到OPEN SSL错误

error:0906D06C:PEM routines:PEM_read_bio:no start line
关闭这个问题,因为我已经从原始问题转移.更新了我的问题,评论显示了我的进步.

为新的特定问题创建了一个新问题:How do I verify a JSON Web Token using a Public RSA key?

以防万一它可以帮助其他人:

有关使用PHP从Microsoft获取公钥的解决方案的更多信息,我执行了以下操作:

$string_microsoftPublicKeyURL = 'https://login.windows.net/common/discovery/keys';

$array_publicKeysWithKIDasArrayKey = loadKeysFromAzure($string_microsoftPublicKeyURL);


function loadKeysFromAzure($string_microsoftPublicKeyURL) {
    $array_keys = array();

    $jsonString_microsoftPublicKeys = file_get_contents($string_microsoftPublicKeyURL);
    $array_microsoftPublicKeys = json_decode($jsonString_microsoftPublicKeys,true);

    foreach($array_microsoftPublicKeys['keys'] as $array_publicKey) {
        $string_certText = "-----BEGIN CERTIFICATE-----\r\n".chunk_split($array_publicKey['x5c'][0],64)."-----END CERTIFICATE-----\r\n";
        $array_keys[$array_publicKey['kid']] = getPublicKeyFromX5C($string_certText);
    }

    return $array_keys;
}


function getPublicKeyFromX5C($string_certText) {
    $object_cert = openssl_x509_read($string_certText);
    $object_pubkey = openssl_pkey_get_public($object_cert);
    $array_publicKey = openssl_pkey_get_details($object_pubkey);
    return $array_publicKey['key'];
}

然而,最好将这些缓存到磁盘,这样你就不会每次加载它们,但这只是一个如何做到这一点的简单例子.

然后,使用公钥数组,检查JWT标头中的’kid’值,找到要验证的正确公共证书,并在openssl_verify()中的参数3中使用它.我使用JWT类来为我处理这个问题.

使用上面创建的这个公钥数组和JWT类应该允许您验证Microsoft JWT.

从firebase链接到JWT类:https://github.com/firebase/php-jwt

用你的JWT的param 1,这个公钥数组的param 2和只有’RS256’的数组中的三个param调用JWT :: Decode.

JWT::decode($string_JSONWebToken,$array_publicKeysWithKIDasArrayKey,array('RS256'));

如果JWT无效或返回解密的JWT供您使用(并检查声明),这将抛出异常.

原文链接:https://www.f2er.com/php/240281.html

猜你在找的PHP相关文章