gpt4 book ai didi

android - Firebase Auth 自定义声明未传播到客户端

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:11:50 27 4
gpt4 key购买 nike

我有一个 UID 为 1 的用户,其中自定义声明设置为,

frompos=true

我通过以下方式从 ADMIN SDK for java 为该用户设置新的自定义声明:

Map<String,Object> claims = new HashMap<>();

claims.put("frompos",false);

FirebaseAuth.getInstance().setCustomUserClaimsAsync("1", claims).get(10000,
TimeUnit.MILLISECONDS);

我在服务器端打印声明以检查声明是否已设置:

UserRecord user = FirebaseAuth.getInstance().getUserAsync("1").get(10000,
TimeUnit.MILLISECONDS);

LOG.debug("user new claims " + user.getCustomClaims());

如预期的那样,声明得到设置:

user new claims {frompos=false}

现在在 android sdk 端,我的用户已经登录,所以我手动刷新 ID token 以传播声明,如文档所述( https://firebase.google.com/docs/auth/admin/custom-claims )

FirebaseAuth.getInstance().getCurrentUser().getIdToken(true).addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
@Override
public void onComplete(@NonNull Task<GetTokenResult> task) {
if(task.isSuccessful()){
Log.d("FragmentCreate","Success refreshing token "+(FirebaseAuth.getInstance().getCurrentUser()==null));

Log.d("FragmentCreate","New token "+task.getResult().getToken());

}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("FragmentCreate","Failure refreshing token "+(FirebaseAuth.getInstance().getCurrentUser()==null)+" "+e.toString());
}
});

现在我使用这里打印的 Id Token 并在服务器端验证它并从中打印声明

 FirebaseToken tokenTest = FirebaseAuth.getInstance(ahmedabadRepoApp).verifyIdTokenAsync(token).get(10000,TimeUnit.MILLISECONDS);

LOG.debug("Token claims are "+tokenTest.getClaims());

但这里打印的声明是:

{"aud":"ahmedabadrepo","auth_time":1514724115,"email_verified":false,"exp":1514730425,"iat":1514726825,"iss":"https://securetoken.google.com/ahmedabadrepo","sub":"1","frompos":true,"user_id":"1","firebase":{"identities":{},"sign_in_provider":"custom"}}

因此,即使我手动刷新了 Id token ,frompos 值也没有传播到客户端 sdk。

最佳答案

我在角度上遇到了同样的问题 - 我在服务器上使用 Admin SDK 设置了声明,但是它们不会出现在客户端的用户中。

使用以下内容我可以看到负载中的声明:

  this.firebaseAuth.auth.currentUser.getIdToken().then(idToken => {

const payload = JSON.parse(this.b64DecodeUnicode(idToken.split('.')[1]))

console.log(payload);
}

)

b64DecodeUnicode(str) {
return decodeURIComponent(atob(str).replace(/(.)/g, function (m, p) {
var code = p.charCodeAt(0).toString(16).toUpperCase();
if (code.length < 2) {
code = '0' + code;
}
return '%' + code;
}));
}

这是一个good write up这是我复制以上内容的地方:

At the moment the client-side code must parse and decode the user’s ID token to extract the claims embedded within. In the future, the Firebase client SDKs are likely to provide a simpler API for this use case.

来自Firebase的相关信息Docs :

Custom claims can only be retrieved through the user's ID token. Access to these claims may be necessary to modify the client UI based on the user's role or access level. However, backend access should always be enforced through the ID token after validating it and parsing its claims. Custom claims should not be sent directly to the backend, as they can't be trusted outside of the token.

Once the latest claims have propagated to a user's ID token, you can get these claims by retrieving the ID token first and then parsing its payload (base64 decoded):

// https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
firebase.auth().currentUser.getIdToken()
.then((idToken) => {
// Parse the ID token.
const payload = JSON.parse(b64DecodeUnicode(idToken.split('.')[1]));
// Confirm the user is an Admin.
if (!!payload['admin']) {
showAdminUI();
}
})
.catch((error) => {
console.log(error);

关于android - Firebase Auth 自定义声明未传播到客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48042934/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com