B4J Question how to use bearer authentication on my own jServer

Alexander Stolte

Expert
Licensed User
Longtime User
Hey, I'm writing an API for my app and while analyzing other APIs I've noticed that many are doing the authentication with oauth.

My question is how can i do a "bearer authentication" for my own jServer and is this free?

Greetings
 

DonManfred

Expert
Licensed User
Longtime User
My question is how can i do a "bearer authentication" for my own jServer
B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    Dim bearer As String = req.GetHeader("Bearer") ' to check just a Bearer Header
    Dim authheadercomplete As String = req.GetHeader("Authorization") ' check authorizationheader
    ' and split authheadercomplete to have only the token afterwards...
Check your database now for any valid token and only go on in request if the token is valid.

and is this free?
i dont´see anything problematic in reading a header. Sure it is free
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
    Dim j As HttpJob
    j.Initialize("",Me)
    j.Download2("http://192.168.192.121:8888/sendmsg", _
    Array As String("title", "the Title", "body", "this is the textbody","topic","general"))
    Dim token As String = "This is a dummystr used as a Token"
    j.GetRequest.SetHeader("Authorization","Bearer "&token )

Inside the Server handler
B4X:
    Dim bearerhdr As String = req.GetHeader("Authorization")
    If bearerhdr.Length > 7 Then
        Dim token As String = bearerhdr.SubString(7).Trim
        Log("Token from Auth-Header: "&token)
    End If

Result from serverlog
Token from Auth-Header: This is a dummystr used as a Token
 
Upvote 0
Top