OAuth2 Clients Object |
The OAuth2 clients object can be used to maintain OAuth2 clients programmatically and without the need for a user interface. The OAuth2 clients object can also be used to add OAuth2 security to any Web service built with PxPlus.
It is possible to implement your own OAuth2 access token server using the OAuth2 clients object if the one provided with PxPlus (/services/oauth2/token.pxp) does not meet an application's requirements. See Methods.
(The OAuth2 Clients object was added in PxPlus 2021.)
Instantiating the OAuth2 Clients Object
To instantiate the OAuth2 clients object using the handle oauth2_clients (where oauth2_clients can be any numeric variable), enter the following command:
oauth2_clients=new("*web/services/oauth2/clients", adminUsername$, adminPassword$)
The adminUsername$ and adminPassword$ arguments are optional and are only needed if you are using the DeleteClient, SaveClient and SaveNewClient$ methods so that the object can create/modify users.
To access any of the available methods, the OAuth2 clients object handle oauth2_clients is used, followed by an ' (apostrophe) and the method (with the desired parameters).
Examples:
NewClient$=oauth2_clients'SaveNewClient$(newClientName$,securityClass$)
if oauth2_clients'DeleteClient(oldClientName$)=0 \
then msgbox "Failed to delete client: "+oldClientName$
The interface supports the following methods:
Create a New OAuth2 Client:
oauth2_clients=new("*web/services/oauth2/clients",adminUsername$,adminPassword$)
read data from oauth2_clients'SaveNewClient$("ABC Shipping", "USER") to client_Id$,client_Secret$,access_Token_Key$
Revoke Access to Compromised Client by Changing Client Secret:
oauth2_clients=new("*web/services/oauth2/clients",adminUsername$,adminPassword$)
read data from oauth2_clients'GetClient$("ABC Shipping") to client_Id$,client_Secret$,access_Token_Key$,security_Class$
oauth2_clients'SaveClient("ABC Shipping", client_Id$, oauth2_clients'NewClientSecret$() ,access_Token_Key$,security_Class$)
Add OAuth2 Security to PxPlus-built Web Service:
!
! Allow access to secure query via OAuth2 authentication passed in the HTTP header as an authorization: bearer token
if len(%http_authorization$)>=7 and lcs(mid(%http_authorization$,1,7))="bearer " \
{
!
! Parse BASE64 bearer token and convert it to get the access token
base64AccessToken$=stp(mid(%http_authorization$,8,err=Return_auth_err),"B")
accessToken$=cvs(base64AccessToken$,"BASE64URL:ASCII",0)
!
! Validate the access token using the OAuth2 clients object, this will also do a logon as the client to allow access to secure resource
oauth2clients=new("*web/services/oauth2/clients",err=Return_auth_err)
if oauth2clients'ValidateAccessToken$(accessToken$)="" \
then gotoReturn_auth_err
drop object oauth2clients,err=*next
}