public String createToken ( Map < String , Object > data , TokenOptions options ) { if ( ( data == null || data . size ( ) == 0 ) && ( options == null || ( ! options . isAdmin ( ) && ! options . isDebug ( ) ) ) ) { throw new IllegalArgumentException ( "TokenGenerator.createToken: data is empty and no options are set. This token will have no effect on Firebase." ) ; } JSONObject claims = new JSONObject ( ) ; try { claims . put ( "v" , TOKEN_VERSION ) ; claims . put ( "iat" , new Date ( ) . getTime ( ) / 1000 ) ; boolean isAdminToken = ( options != null && options . isAdmin ( ) ) ; validateToken ( "TokenGenerator.createToken" , data , isAdminToken ) ; if ( data != null && data . size ( ) > 0 ) { claims . put ( "d" , new JSONObject ( data ) ) ; } // Handle options if ( options != null ) { if ( options . getExpires ( ) != null ) { claims . put ( "exp" , options . getExpires ( ) . getTime ( ) / 1000 ) ; } if ( options . getNotBefore ( ) != null ) { claims . put ( "nbf" , options . getNotBefore ( ) . getTime ( ) / 1000 ) ; } // Only add these claims if they're true to avoid sending them over the wire when false. if ( options . isAdmin ( ) ) { claims . put ( "admin" , options . isAdmin ( ) ) ; } if ( options . isDebug ( ) ) { claims . put ( "debug" , options . isDebug ( ) ) ; } } } catch ( JSONException e ) { throw new RuntimeException ( e ) ; } String token = computeToken ( claims ) ; if ( token . length ( ) > 1024 ) { throw new IllegalArgumentException ( "TokenGenerator.createToken: Generated token is too long. The token cannot be longer than 1024 bytes." ) ; } return token ; }
Create a token for the given object and options .