Hec*_*tor 1 base64 android jwt jjwt
我正在调查 Google 在我的 Android 应用程序中提供的 SafetyNet。
首先,我简单地调用了 SafetyNet attest API,然后 Base64 对这些部分进行了解码,如 Google 提供的示例所示。
SafetyNet.getClient(this).attest(NONCE, <API KEY>)
.addOnSuccessListener(this, new OnSuccessListener<SafetyNetApi.AttestationResponse>() {
@Override
public void onSuccess(final SafetyNetApi.AttestationResponse attestationResponse) {
initialDataExtraction(attestationResponse.getJwsResult());
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull final Exception exception) {
if (exception instanceof ApiException) {
final ApiException apiException = (ApiException) exception;
Log.e(TAG, "onFailure: " + apiException.getMessage() + " " + apiException.getStatusCode());
} else {
Log.e(TAG, "Error: ", exception);
}
}
});
Run Code Online (Sandbox Code Playgroud)
我提取 JWS 部分如下:-
private byte[] initialDataExtraction(final String jwsResult) {
final String[] jwsResultParts = jwsResult.split("[.]");
if (jwsResultParts.length == 3) {
final byte[] header = Base64.decode(jwsResultParts[0], Base64.NO_WRAP);
final byte[] data = Base64.decode(jwsResultParts[1], Base64.NO_WRAP);
final byte[] signature = Base64.decode(jwsResultParts[2], Base64.NO_WRAP);
Log.d(TAG, "initialDataExtraction: header = " + new String(header, UTF_8));
Log.d(TAG, "initialDataExtraction: data = " + new String(data, UTF_8));
Log.d(TAG, "initialDataExtraction: signature = " + new String(signature, UTF_8));
return data;
} else {
Log.e(TAG, "initialDataExtraction: Failure: Illegal JWS signature format. The JWS consists of " + jwsResultParts.length + " parts instead of 3.");
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用android.util.Base64解码部分,大部分时间解码都可以完成。
有时我会收到此异常:-
java.lang.IllegalArgumentException: bad base-64
at android.util.Base64.decode(Base64.java:161)
at android.util.Base64.decode(Base64.java:136)
at android.util.Base64.decode(Base64.java:118)
Run Code Online (Sandbox Code Playgroud)
解码签名部分时。
解码以查看此间歇性错误时我做错了什么?
然后我开始使用 JWT 库来解码令牌。
首先我试过 group: 'com.auth0.android', name: 'jwtdecode', version: '1.1.1'
我试过的代码是
final JWT jwt = new JWT(jwsResult);
Run Code Online (Sandbox Code Playgroud)
始终失败并出现以下错误
com.auth0.android.jwt.DecodeException: The token's payload had an invalid JSON format.
at com.auth0.android.jwt.JWT.parseJson(JWT.java:235)
at com.auth0.android.jwt.JWT.decode(JWT.java:203)
at com.auth0.android.jwt.JWT.<init>(JWT.java:40)
Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 23 path $.
at com.google.gson.Gson.fromJson(Gson.java:899)
at com.google.gson.Gson.fromJson(Gson.java:852)
at com.google.gson.Gson.fromJson(Gson.java:801)
Run Code Online (Sandbox Code Playgroud)
此异常似乎是由 Auth0 库无法解析标头4.1.6. "x5c" (X.509 Certificate Chain) Header格式引起的,这很奇怪,因为 JWS 规范明确指出该值由 JSON 数组表示:-
The "x5c" (X.509 Certificate Chain) Header Parameter contains the
X.509 public key certificate or certificate chain [RFC5280]
corresponding to the key used to digitally sign the JWS. The
certificate or certificate chain is represented as a JSON array of
certificate value strings.
Run Code Online (Sandbox Code Playgroud)
但是,如果我将相同的 jws 结果字符串复制并粘贴到纯 Java 项目中compile 'com.auth0:java-jwt:3.3.0'并使用此代码:-
String token = "<JWS TOKEN>";
try {
final DecodedJWT jwt = JWT.decode(token);
System.out.println("Header = " + jwt.getHeader());
System.out.println("Payload = " + jwt.getPayload());
System.out.println("Signature = " + jwt.getSignature());
} catch (JWTDecodeException exception){
throw new RuntimeException(exception);
}
Run Code Online (Sandbox Code Playgroud)
Jws Token 解码成功。
我在我的 Android 应用程序中做错了什么,阻止了 auth0 android jwt 库按需要工作?
然后'io.jsonwebtoken:jjwt:0.9.0'我在我的 Android 应用程序中尝试了库。
当我执行此代码时:-
Jwts.parser().parse(jwsResult).getBody();
Run Code Online (Sandbox Code Playgroud)
它失败了:-
java.lang.IllegalArgumentException: A signing key must be specified if the specified JWT is digitally signed.
at io.jsonwebtoken.lang.Assert.notNull(Assert.java:85)
at io.jsonwebtoken.impl.DefaultJwtParser.parse(DefaultJwtParser.java:331)
Run Code Online (Sandbox Code Playgroud)
我需要传递给 Jwts 的签名密钥是什么?我拥有的唯一密钥是我保存在 Google API 控制台中的 API 密钥,这是我应该使用的密钥吗?
当我通过它如下:
Jwts.parser().setSigningKey<API KEY>.parse(jwsResult).getBody();
Run Code Online (Sandbox Code Playgroud)
这失败了:-
java.lang.IllegalArgumentException: Key bytes can only be specified for HMAC signatures. Please specify a PublicKey or PrivateKey instance.
at io.jsonwebtoken.lang.Assert.isTrue(Assert.java:38)
at io.jsonwebtoken.impl.DefaultJwtParser.parse(DefaultJwtParser.java:324)
Run Code Online (Sandbox Code Playgroud)
解码和使用从 SafetyNet attest API 调用收到的 Jws 结果的正确方法是什么?
我java.lang.IllegalArgumentException: bad base-64从这个问题Base64: java.lang.IllegalArgumentException: Illegal character 中发现了该问题的解决方法
在解码之前简单地替换 jws 标记中的字符
token.replace('-', '+').replace('_', '/')
Run Code Online (Sandbox Code Playgroud)
我发现这个库不仅可以在 Android 上正常工作。
// https://mvnrepository.com/artifact/com.nimbusds/nimbus-jose-jwt
implementation group: 'com.nimbusds', name: 'nimbus-jose-jwt', version: '5.1'
try {
final JWSObject jwsObject = JWSObject.parse(jwsResult);
System.out.println("header = " + jwsObject.getHeader());
System.out.println("header = " + jwsObject.getHeader().getX509CertChain());
System.out.println("payload = " + jwsObject.getPayload().toJSONObject());
System.out.println("signature = " + jwsObject.getSignature());
System.out.println("signature = " + jwsObject.getSignature().decodeToString());
} catch (ParseException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
提供了一些很好的例子:-
https://connect2id.com/products/nimbus-jose-jwt/examples
| 归档时间: |
|
| 查看次数: |
2624 次 |
| 最近记录: |