How to create Firebase Authentication claims?

左心房为你撑大大i 提交于 2020-05-09 10:25:11

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/47495678/how-to-create-firebase-authentication-claims

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!