我正在使用 laravel 和 tymondesigns 来处理 JWT 令牌。我想手动检查提供的令牌是否已过期或无效。我应该使用哪些功能?
我正在尝试在 node.js 中创建 JWT 令牌以与 firebase 中的 REST api 一起使用,但是当我尝试使用它们时,我收到错误“错误:身份验证标头中的无效声明 'kid'”。
这是我的代码
http.createServer(function (req, res) {
var payload = {
uid: "bruh"
};
var token = jwt.sign(payload, sact["private_key"], {
algorithm: 'RS256',
issuer: sact["client_email"],
subject: sact["client_email"],
audience: 'https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit',
expiresIn: '3600s',
jwtid: sact["private_key_id"],
header: {
"kid": sact["private_key_id"]
}
});
res.writeHead(200);
res.end("It worked. (" + token + ")");
}).listen(port);
Run Code Online (Sandbox Code Playgroud)
这些是我的要求
var http = require('http');
var jwt = require('jsonwebtoken');
Run Code Online (Sandbox Code Playgroud) 我有一个 ASP.NET Core 1.1 项目,使用 Azure AD OpenID 进行身份验证。身份验证在我的开发箱(Windows 10)上运行良好,无论是在 IIS express 还是完整的 IIS 上。但是,当我将项目部署到我的服务器(Server 2008R2)时,登录到 Microsoft 重定向站点后,服务器抛出此错误:
Microsoft.IdentityModel.Tokens.SecurityTokenInvalidSignatureException:IDX10503:签名验证失败。尝试的密钥:'Microsoft.IdentityModel.Tokens.X509SecurityKey,KeyId:VWVIc1WD1Tksbb301sasM5kOq5Q Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException:Internal.Cryptography.Helpers.OpenStorageProvider(CngProvider provider) at System.KeySecurity.Cryptography.CngProvider provider) 发生内部错误。 Byte[] keyBlob, String curveName, CngKeyBlobFormat 格式, CngProvider provider) at System.Security.Cryptography.CngKey.Import(Byte[] keyBlob, CngKeyBlobFormat format) at Internal.Cryptography.Pal.X509Pal.DecodePublicKey(Oid oid, Byte[]在 Internal.Cryptography.Pal.CertificateExtensionsCommon.GetPublicKey[T](X509Certificate2 证书,
当我获取从 Azure 收到的令牌并将其插入 JWT.io 时,它会成功验证签名。
回顾一下:
它与我在服务器上的https证书有什么关系吗?在我的开发箱上,我使用的是用 powershell 创建的自签名证书。在服务器上,我们有一个由 go-daddy 颁发的证书。
谢谢你的帮助。
我使用以下代码将证书转换为公钥并传递给 JwtConsumer:
jwt = "eU13VUDPQsLv2fvbCPEyeuQubditVOguIa2UWvaMhx2ES7cMlTL8F6IgplgpG_H7bXpduPnFUncn7zUYRXmvw_Bts8EfqICeGa5db6RGmofeA01OqowgCfxhWLwmU786riJIT0twMFe...............................BzR7DOvqsahbsx93yKqB_5Q";
// read public key from a file or config or something
String publicKeyPEM =
"-----BEGIN CERTIFICATE-----\n" +
"MIIFuDCCBKCgAwIBAgIQXQ/D2sE/XdZYvdViF83mMzANBgkqhkiG9w0BAQsFADB+\n" +
......................................................................................................... "saQRa7TBj6gAdlYwJVR+4hpLngANpwAG+bXHuEs+Ns/dE/s+b7aUb8/IJTWNtaaQ\n" +
"lMvr/4xtT6ZNCiaIM3uvIvzHqPxCn3sWa94FP9FIg3mbIia1ZbUx8NyMpETOjxaO\n" +
"X242VTjKf7mLCqibyn3kj93zZjgNa0AlbF/QdE9z4tQ58BwoDVlNK4mGv7Uq2nca\n" +
"2qTrgWcVVKyhKMnytiQ4LTs5O45R/YNbnEH7CA==\n" +
"-----END CERTIFICATE-----";
RsaKeyUtil rsaKeyUtil = new RsaKeyUtil();
PublicKey publicKey = rsaKeyUtil.fromPemEncoded(publicKeyPEM);
// create a JWT consumer
JwtConsumer jwtConsumer = new JwtConsumerBuilder()
.setRequireExpirationTime()
.setVerificationKey(publicKey)
.build();
// validate and decode the jwt
JwtClaims jwtDecoded = jwtConsumer.processToClaims(jwt);
Run Code Online (Sandbox Code Playgroud)
但是,在创建 PublicKey 实例时出现以下错误。
Starting Applicationjava.security.InvalidKeyException: IOException: ObjectIdentifier() -- data isn't an object ID …Run Code Online (Sandbox Code Playgroud) 当我创建 jwt 并调用 Zoom api 时,出现错误{'code': 124, 'message': 'Invalid access token.'}。这是什么意思?
ApiKey = 'xxx'
ApiSercret = 'xxx'
mail = request.POST['mail']
print(mail)
today = datetime.today()
header = {
'alg':'HS256'
}
payload = {
'iss': ApiKey,
'exp': today + timedelta(hours=1),
}
#https://docs.authlib.org/en/latest/specs/rfc7519.html#authlib.jose.rfc7519.JWT.check_sensitive_data
token = jwt.encode(header,payload,ApiSercret,check='true')
print(token)
import http.client
conn = http.client.HTTPSConnection("api.zoom.us")
headers = {
'authorization': "Bearer 39ug3j309t8unvmlmslmlkfw853u8",
'content-type': "application/json"
}
conn.request("GET", "/v2/users?status=active&page_size=30&page_number=1", headers=headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
params = {
mail:token
}
return render(request,'api/index.html',params)
Run Code Online (Sandbox Code Playgroud)
错误内容:
{'code': 124, …Run Code Online (Sandbox Code Playgroud) 我一直在尝试使用 Keycloak Impersonation API(最近添加的)来获取另一个用户的访问令牌。我根据文档和另一个StackOverflow 问题创建了一个半成功的 CURL 请求。CURL 请求(下面)返回一个501 Not Implemented,我正在尝试解决这个问题。如果这是另一个错误,我会假设我做错了什么,但这似乎至少部分正确。
curl --verbose -X POST "http://localhost:8081/auth/realms/master/protocol/openid-connect/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "grant_type=urn:ietf:params:oauth:grant-type:token-exchange" \
-d "client_id=admin-cli" \
-d "requested_subject={TARGET_USER_ID}" \
-d "subject_token={USER_MANAGER_TOKEN}"
Run Code Online (Sandbox Code Playgroud)
到目前为止,我的工作流程是获取 Keycloak 主域“admin”用户的访问令牌(成功)并在模拟请求中使用它,以及目标用户的 Keycloak ID。我做错了什么或错过了一步吗?
我没有更改任何 Keycloak 权限,这是必需的吗?
根据我的理解和文档,目前在Keycloak v5 - Sever Installation 中默认支持和启用模拟。然而,另一篇文章(Keycloak v5 - Token Exchange)似乎表明该功能默认是禁用的;这可能是我得到的原因501 Not Implemented吗?
编辑: @qdivision 提到需要启用令牌交换才能使其工作。但是,我们正在使用jboss/keycloakDocker 映像,我想知道应该在哪里添加profile.properties文件以启用此功能?
我目前正在为我的Go网络应用程序添加JWT身份验证,当涉及到go中的类型转换和自动恐慌(如果它失败)时我有一些担忧.我的代码如下所示:
(c是上下文包)
user := c.Get("user")
token := user.(*jwt.Token)
claims := token.Claims.(jwt.MapClaims)
fmt.Println("Username: ", claims["name"], "User ID: ", claims["jti"])
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我在多行上使用了类型转换,但如果此操作失败,它将会出现紧急情况并最终导致服务器崩溃.在这种情况下有没有可能的方法来检查错误?
我对Go的Web开发很新,所以我很抱歉,所有的帮助表示赞赏!
我正在开发 Django(1.10) 项目,并使用 Django Rest Framework 为我的项目创建了一个 api。我需要从我的视图内部生成 JWT 令牌。
这是我尝试过的:
settings.py中 REST Framework 的设置:
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoObjectPermissions'
],
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
),
}
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
)
Run Code Online (Sandbox Code Playgroud)
以下是手动获取令牌的 URL:
url(r'^api-token-auth/', obtain_jwt_token)
Run Code Online (Sandbox Code Playgroud)
以下是用户请求获取令牌的模板:
<div class="col-md-9 content">
<div class="world-map" style="width: 100%; margin-left:2%">
<h3> Get your Authorization Token to use REST Api :</h3>
</div>
<div class="col-lg-6" style="margin-top: 20%; margin-left: 25%;"><br> …Run Code Online (Sandbox Code Playgroud) 我正在现有的Web应用程序中实现Spring Security,当我尝试登录时,我尝试查看日志时收到的响应是错误的凭据(即使凭据正确)(即使凭据正确)
WARN osscbcrypt.BCryptPasswordEncoder-空的编码密码
这是凭证不当的原因吗?
这是我的代码
@PostConstruct
public void init() {
try {
authenticationManagerBuilder
.userDetailsService(accountDetailsService)
.passwordEncoder(passwordEncoder());
} catch (Exception e) {
throw new BeanInitializationException("Security configuration failed", e);
}
}
@Bean
public PasswordEncoder passwordEncoder()
{
return new BCryptPasswordEncoder();
}
Run Code Online (Sandbox Code Playgroud)
提前致谢 !!