2

For creating firebase custom auth tokens, I am using third party JWT library (https://github.com/jwtk/jjwt)

In this library, there is an option to add firebase Custom Token Claims like (alg, iss,sub,aud,iat etc.)

All firebase information is available at https://firebase.google.com/docs/auth/admin/create-custom-tokens#create_custom_tokens_using_a_third-party_jwt_library

private_key = "-----BEGIN PRIVATE KEY-----... -----END PRIVATE KEY-----";

I had passed private key in signWith method with encoding in base64

 val encodeKey = Base64.encode(privateKey.toByteArray(), android.util.Base64.DEFAULT)

 val jwt = Jwts.builder().setIssuer("firebase-adminsdk-kywht@...")
                .setSubject("firebase-adminsdk-kywht@...")
                .setAudience("https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit")
                .setExpiration(calendar.time) //a java.util.Date
                .setIssuedAt(Date())
                .setId(UUID.randomUUID().toString())signWith(SignatureAlgorithm.RS512 ,encodeKey).compact()

I have used the above code but it did not work. Anyone knows how to pass a private key to generate token?

Ujjwal Jung Thapa
  • 548
  • 2
  • 6
  • 27
kinjal patel
  • 555
  • 2
  • 4
  • 12

1 Answers1

0

first thing is whether you had missed a dot in your code acccidentally or not so put that and the parameters for signWith() is not correct i.e. first parameter is key and second one is signature algorithm. try this:

val encodeKey = Base64.encode(privateKey.toByteArray(), android.util.Base64.DEFAULT)

val jwt = Jwts.builder().setIssuer("firebase-adminsdk-kywht@...")
            .setSubject("firebase-adminsdk-kywht@...")
            .setAudience("https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit")
            .setExpiration(calendar.time) //a java.util.Date
            .setIssuedAt(Date())
            .setId(UUID.randomUUID().toString())
            .signWith(encodeKey, SignatureAlgorithm.RS512) //changed here
            .compact()
Ujjwal Jung Thapa
  • 548
  • 2
  • 6
  • 27
  • Ujjwal Jung Thapa, I put a dot at the end for security purpose. As you suggested that use **signWith(encodeKey, SignatureAlgorithm.RS512)**. But in the first parameter, it requires java.security.Key object and encodeKey is actually strings. – kinjal patel Jun 11 '19 at 10:25
  • Ujjwal Jung Thapa,Do you know How to convert this private_key into java.security.Key? – kinjal patel Jun 11 '19 at 10:35