31
loading...
This website collects cookies to deliver better user experience
Class Web.OAUTH2.Google Extends %CSP.Page
/// This refers to name of client registered with Google Authorization Server
Parameter APP = "Google";
classmethod OnPage() as %Status
#define LOCALHOST $System.INetInfo.LocalHostName()
// we define list of scopes, these define API we want to call
set scope="openid https://www.googleapis.com/auth/userinfo.email "_
"https://www.googleapis.com/auth/userinfo.profile "_
"https://www.googleapis.com/auth/drive.metadata.readonly "_
"https://www.googleapis.com/auth/calendar.readonly"
// 1. Check if we have an access token from oauth2 server stored locally with registered client application (client id, client secret, ...)
set isAuthorized=##class(%SYS.OAuth2.AccessToken).IsAuthorized(..#APP,,scope,.accessToken,.idtoken,.responseProperties,.error)
// Google has no introspection endpoint - nothing to call - the introspection endpoint and display result -- see RFC 7662.
// our API to retrieve user information from access token
$$$THROWONERROR(sc,##class(%SYS.OAuth2.AccessToken).GetUserinfo(..#APP,accessToken,,.jsonObject))
// here we perform actual calls to APIs (a separate class method in the CSP page, see below)
do ..RetrieveAPIInfo("/drive/v3/files")
do ..RetrieveAPIInfo("/calendar/v3/users/me/calendarList")
// here we construct redirect page (we redirect back to the same page)
// in this case, there is a virtual application called “app” defined at InterSystems Webserver Gateway
set redirect="https://"_$$$LOCALHOST_"/app/csp/demos2/Web.OAUTH2.Google.cls"
// these parameters are telling Google how to proceed with authorization
set properties("approval_prompt")="force"
set properties("include_granted_scopes")="true"
// 2. Obtain Access token from Google Authorization Server for our registered client
set url=##class(%SYS.OAuth2.Authorization).GetAuthorizationCodeEndpoint(..#APP,scope,redirect,.properties,.isAuthorized,.sc)
//ClassMethod RetrieveAPIInfo(api As %String)
#define OAUTH2ROOT "https://www.googleapis.com"
// construct HTTP request, add access token and call given api
set tHttpRequest=##class(%Net.HttpRequest).%New()
$$$THROWONERROR(sc,##class(%SYS.OAuth2.AccessToken).AddAccessToken(tHttpRequest,"query","GOOGLE",..#APP))
$$$THROWONERROR(sc,tHttpRequest.Get($$$OAUTH2ROOT_api))
set tHttpResponse=tHttpRequest.HttpResponse
// now feel free to parse received tHttpResponse object
31