java - 如何为以下方法编写单元测试 JWKS RSA 算法,使用 Java 中的 junit 或 mockito 验证签名和测试角色
问题描述
How to mock JWKS RSA Algorithm , validate signature and test roles using junit or mockito in Java for below method microsoftJwksUrl is an AzureActiveDirectory endpoint .
以下是需要模拟的方法:
如何模拟 JWKS RSA 算法,使用 Java 中的 junit 或 mockito 验证签名和测试角色,用于 AzureActiveDirectory 端点的以下方法 microsoftJwksUrl
public String[] getAadRoles(String token, String intendedAudience, String microsoftJwksUrl)
throws AadTokenAuthenticationFailedException {
//Decode AAD token
DecodedJWT jwt = JWT.decode(token);
//Get Provider from the endpoint :
//microsoftJwksUrl=https://login.microsoftonline.com/common/discovery/v2.0/keys
JwkProvider provider = new UrlJwkProvider(new URL(microsoftJwksUrl));
Jwk jwk = provider.get(jwt.getKeyId());
Algorithm algorithm=Algorithm.RSA256((RSAPublicKey) jwk.getPublicKey(), null);
//Verify Algorithm
algorithm.verify(jwt);
String audience = jwt.getAudience().get(0);
Date expiresAt = jwt.getExpiresAt();
Date currentDateTime = new Date();
Claim roles = jwt.getClaim("roles");
//checks if it's audience is correct and token is valid
if (audience.contentEquals(intendedAudience) && currentDateTime.before(expiresAt)) {
return roles!=null ? roles.asArray(String.class) : new String[] {};
} else {
String errorMessage = String.format("Token not valid for Audience {%s} "
+ ", with Expiry Time as {%s}.", audience,
expiresAt.toString());
LOG.error(errorMessage);
throw new AadTokenAuthenticationFailedException();
}
}
解决方案
推荐阅读
- ios - 保存单个行值列表数据的最佳方法是什么?
- postgresql - 如何将 postgreSQL 连接到 C++
- android - 测试数据库时使用 mockito 测试失败
- javascript - SyntaxError:在 heroku 上托管 Discord 机器人时出现意外的令牌 '??='
- sass - 与本地相比,全球 SASS 架构如何适用于 7 合 1、5 合 1 等?
- php - 的意义是什么?在 POO 参数 PHP 中?
- python - 从 C# 关闭 Flask 应用程序?
- ruby-on-rails - 如何存根活动存储 URL?
- python - python和pyspark中的准泊松回归
- javascript - 如何转换对象数组以匹配其他数组中的对象?