To modify the PowerBuilder application source code to get a token from the JWT server:
Step 1: Declare the following global variables.
//Token expiresin Long gl_Expiresin //Refresh token clockskew Long gl_ClockSkew = 3
Step 2: Define a global function and name it f_Authorization().
Select from menu File > New; in the New dialog, select the PB Object tab and then select Function and click OK to add a global function.
This global function uses the HTTP Post method to send the user credentials to the authentication server and then gets the token from the HTTP Authorization header.
Add scripts to the f_Authorization() function to implement the following scenario: When the application starts, the application uses the username and password from the login window to get the token, and when the token expires, the login window displays for the user to input the username and password again.
Note: The following scripts use the username and password from the INI file instead of from the login window. You can change the scripts to use the login window after you implement the login window and return the username and password to the f_Authorization() function.
//Integer f_Authorization() for password //Token URL is obtained from the INI file //UserName & Password are obtained from the INI file RestClient lrc_Client String ls_url, ls_UserName, ls_UserPass, ls_PostData, ls_Response, ls_expires_in String ls_TokenType, ls_AccessToken String ls_type, ls_description, ls_uri, ls_state Integer li_Return, li_rtn JsonParser ljson_Parser li_rtn = -1 ls_url = profilestring("CloudSetting.ini","setup","TokenURL","") //login window (instead of INI file) can be implemented to pass the username & password. //Open(w_login) //Return UserName & Password ls_UserName = ProfileString("CloudSetting.ini", "users", "userName", "") ls_UserPass = ProfileString("CloudSetting.ini", "users", "userPass", "") If IsNull ( ls_UserName ) Or Len ( ls_UserName ) = 0 Then MessageBox( "Tips", "UserName is empty!" ) Return li_rtn End If If IsNull ( ls_UserPass ) Or Len ( ls_UserPass ) = 0 Then MessageBox( "Tips", "Password is empty!" ) Return li_rtn End If ls_PostData = '{"Username":"' + ls_UserName + '", "Password":"' + ls_UserPass + '"}' lrc_Client = Create RestClient lrc_Client.SetRequestHeader("Content-Type","application/json") li_Return = lrc_Client.GetJWTToken( ls_Url, ls_PostData, ls_Response ) If li_Return = 1 and Pos ( ls_Response, "access_token" ) > 0 Then ljson_Parser = Create JsonParser ljson_Parser.LoadString(ls_Response) ls_TokenType = ljson_Parser.GetItemString("/token_type") ls_AccessToken = ljson_Parser.GetItemString("/access_token") //Application Set Authorization Header Getapplication().SetHttpRequesTheader("Authorization", ls_TokenType + " " +ls_AccessToken, true) //Set Global Variables gl_Expiresin = Long (ljson_Parser.GetItemNumber("/expires_in")) li_rtn = 1 Else MessageBox( "AccessToken Falied", "Return :" + String ( li_Return ) ) End If If IsValid ( ljson_Parser ) Then DesTroy ( ljson_Parser ) If IsValid ( lrc_Client ) Then DesTroy ( lrc_Client ) Return li_rtn
Step 3: Insert a timing object (timing_1) to the application and add the following scripts to the Timer event of timing_1.
1) Open the application object and then select from menu Insert > Object > Timing to add a timing object to the application.
2) Add the following scripts to the Timer event of timing_1.
//Authorization f_Authorization()
When displayed in the source editor, the Timer event looks like this:
event timer;//Authorization f_Authorization() end event
Step 4: Add the following scripts to the SystemError event.
The scripts will trigger the SystemError event when the session or license encounters an error; and if the token is invalid or expires, the scripts will call the f_Authorization function to get the token again.
Choose Case error.Number Case 220 to 229 //Session Error MessageBox ("Session Error", "Number:" + String(error.Number) + "~r~nText:" + error.Text ) Case 230 to 239 //License Error MessageBox ("License Error", "Number:" + String(error.Number) + "~r~nText:" + error.Text ) Case 240 to 249 //Token Error MessageBox ("Token Error", "Number:" + String(error.Number) + "~r~nText:" + error.Text ) //Authorization f_Authorization() Case Else MessageBox ("SystemError", "Number:" + String(error.Number) + "~r~nText:" + error.Text ) End Choose