如何创建 Firebase 身份验证声明? [英] How to create Firebase Authentication claims?

查看:17
本文介绍了如何创建 Firebase 身份验证声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要通过服务器应用程序进行身份验证的 Android 应用程序.服务器应用程序是一个使用 Spring Security 的 Spring Boot 应用程序.服务器应用程序使用自定义身份验证提供程序为用户设置授予权限.

这是我用来检索 idToken 的代码:

 FirebaseToken firebaseToken = null;尝试 {FirebaseApp app = FirebaseUtil.getFirebaseApp();firebaseToken = FirebaseAuth.getInstance(app).verifyIdTokenAsync(authenticationToken.getIdToken()).get();} catch ( InterruptedException | ExecutionException | CancellationException e ) {抛出新的 AuthenticationServiceException(e.getMessage());}返回 firebaseToken;

我有 Firebase 令牌,我可以像这样检索声明:

Map索赔 = token.getClaims();

然后我可以迭代声明并像这样创建权限:

List权限 = Lists.newArrayList();authority.add(new SimpleGrantedAuthority("some_role"));

我不知道如何使用 Firebase 控制台创建声明.这可能吗?我怀疑我需要使用 Firebase 数据库,但在 firebase 文档中找不到我正在寻找的确切内容.

解决方案

Firebase 身份验证的自定义声明目前只能通过 Firebase Admin SDK 创建.来自有关创建自定义声明的文档:

<块引用>

//为uid对应的用户设置admin权限.admin.auth().setCustomUserClaims(uid, {admin: true}).then(() => {//新的自定义声明将传播到用户的 ID 令牌//下次发布新的时.});

允许从控制台创建声明也不错,因此我建议您提交功能请求.

I am developing an Android app that needs to authenticate with a server application. The server application is a Spring Boot app that uses Spring Security. The server app uses a custom authentication provider to set the Granted Authorities for the user.

This is the code that I am using to retrieve the idToken:

    FirebaseToken firebaseToken = null;

    try {
        FirebaseApp app = FirebaseUtil.getFirebaseApp();
        firebaseToken = FirebaseAuth.getInstance(app).verifyIdTokenAsync(authenticationToken.getIdToken()).get();
    } catch ( InterruptedException | ExecutionException | CancellationException e ) {
        throw new AuthenticationServiceException(e.getMessage());
    } 

    return firebaseToken; 

One I have the Firebase Token I can retrieve the claims like this:

Map<String, Object> claims = token.getClaims();

I can then iterate over the claims and create the authorities like so:

List<GrantedAuthority> authorities = Lists.newArrayList(); 
authorities.add(new SimpleGrantedAuthority("some_role"));

What I can't figure out is how to create the claims using the Firebase Console. Is this possible? I suspect that I need to use the Firebase Database but can't find exactly what I'm looking for in the firebase docs.

解决方案

Custom claims for Firebase Authentication can currently only be created through the Firebase Admin SDKs. From the documentation on creating custom claims:

// Set admin privilege on the user corresponding to uid.
admin.auth().setCustomUserClaims(uid, {admin: true}).then(() => {
  // The new custom claims will propagate to the user's ID token the
  // next time a new one is issued.
});

It's not a bad idea to allow creating claims from the console too, so I'd recommend you file a feature request.

这篇关于如何创建 Firebase 身份验证声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆