Android Code Snippet Get the expiration time from firebase JWT token

B4X:
Private Sub GetExpiryTime(JWTToken As String) As Long
    Dim parts() As String = Regex.Split("\.", JWTToken)
    Dim b() As Byte = UrlSafeDecodeBase64(parts(1))
    Dim s As String = BytesToString(b, 0, b.Length, "utf8")
    Dim map As Map = s.As(JSON).ToMap
    Log(map.As(JSON).ToString)
    If map.ContainsKey("exp") Then
        Dim expire As Long = map.Get("exp") * 1000
        Return expire
    End If
    Return 0
End Sub

Private Sub UrlSafeDecodeBase64 (s As String) As Byte()
    Dim jo As JavaObject
    jo.InitializeStatic("android.util.Base64")
    Return jo.RunMethod("decode", Array(s, Bit.Or(8, Bit.Or(2, 1)))) 'URL_SAFE
End Sub
 

lip

Active Member
Licensed User
Longtime User
Using the code above (in B4J) I'm getting an error on line 16:
java.lang.ClassNotFoundException: android$util$Base64

I'm guessing I'm missing a library?
 

aeric

Expert
Licensed User
Longtime User
Using the code above (in B4J) I'm getting an error on line 16:
java.lang.ClassNotFoundException: android$util$Base64

I'm guessing I'm missing a library?
This Code Snippet is for B4A.
Post a question in B4J forum.
 

udg

Expert
Licensed User
Longtime User
Or search for "Base64 decode" in the forum and substitute function UrlSafeDecodeBase64 above with its equivalent.
 
Top