Android Code Snippet Firebase push-message sending: the token from a code string

ServiceAccount JSON-file is required for Firebase push-message sending. It's dangerous to use this JSON-file in the app as-is.
But it's possible to save it's content into a code string, like:

and use in the sending Android-app with "GetTokenValueFromJSON" sub:

B4X:
'from JSON-file, DANGEROUS to store ServiceAccount JSON file !
Private Sub GetTokenValue (FilePath As String) As String
    Dim GoogleCredentials As JavaObject
    GoogleCredentials.InitializeStatic("com.google.auth.oauth2.GoogleCredentials")
    Dim Credentials As JavaObject = GoogleCredentials.RunMethodJO("fromStream", Array(File.OpenInput(FilePath, ""))) _
        .RunMethod("createScoped", Array(Array As String("https://www.googleapis.com/auth/firebase.messaging")))
    Credentials.RunMethod("refreshIfExpired", Null)
    Return Credentials.RunMethodJO("getAccessToken", Null).RunMethod("getTokenValue", Null)
End Sub

'from string with JSON-file content
Private Sub GetTokenValueFromJSON (firebase_adminsdk_json_string As String) As String
    Dim bc As ByteConverter
    Dim bytes() As Byte = bc.StringToBytes(firebase_adminsdk_json_string, "UTF8")
    Dim InputStream As InputStream
    InputStream.InitializeFromBytesArray(bytes, 0, bytes.Length)
   
    Dim GoogleCredentials As JavaObject
    GoogleCredentials.InitializeStatic("com.google.auth.oauth2.GoogleCredentials")
    Dim Credentials As JavaObject = GoogleCredentials.RunMethodJO("fromStream", Array(InputStream)) _
        .RunMethod("createScoped", Array(Array As String("https://www.googleapis.com/auth/firebase.messaging")))
    Credentials.RunMethod("refreshIfExpired", Null)
    Return Credentials.RunMethodJO("getAccessToken", Null).RunMethod("getTokenValue", Null)
End Sub

Or is it also non-safe ?
 

hatzisn

Expert
Licensed User
Longtime User
Great code, although I wouldn't suggest it. I think it is not safe to do it.
 

peacemaker

Expert
Licensed User
Longtime User
Can be also reverse-engineered from code? As well as unpack .json file from assets ?
 

hatzisn

Expert
Licensed User
Longtime User
I cannot be completely sure but in the case it is possible why would you risk it?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…