Chilkat Online Tools

lianja / ForgeRock Identity Cloud Collection / Step 3: Introspect the Access Token

Back to Collection Items

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

loHttp = createobject("CkHttp")

loReq = createobject("CkHttpRequest")

loJsonParam1 = createobject("CkJsonObject")
loReq.AddParam("token",loJsonParam1.Emit())

loJsonParam2 = createobject("CkJsonObject")
loReq.AddParam("client_id",loJsonParam2.Emit())

loReq.AddHeader("Authorization","Bearer <access_token>")

loResp = loHttp.PostUrlEncoded("https://<tenant-name>.forgeblocks.com/am/oauth2/realms/root/realms/alpha/introspect",loReq)
if (loHttp.LastMethodSuccess = .F.) then
    ? loHttp.LastErrorText
    release loHttp
    release loReq
    release loJsonParam1
    release loJsonParam2
    return
endif

loSbResponseBody = createobject("CkStringBuilder")
loResp.GetBodySb(loSbResponseBody)

loJResp = createobject("CkJsonObject")
loJResp.LoadSb(loSbResponseBody)
loJResp.EmitCompact = .F.

? "Response Body:"
? loJResp.Emit()

lnRespStatusCode = loResp.StatusCode
? "Response Status Code = " + str(lnRespStatusCode)
if (lnRespStatusCode >= 400) then
    ? "Response Header:"
    ? loResp.Header
    ? "Failed."
    release loResp
    release loHttp
    release loReq
    release loJsonParam1
    release loJsonParam2
    release loSbResponseBody
    release loJResp
    return
endif

release loResp

// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)

// {
//   "active": true,
//   "scope": "write",
//   "realm": "/",
//   "client_id": "forgerockDemoPublicClient",
//   "user_id": "demo",
//   "token_type": "Bearer",
//   "exp": 1597324518,
//   "sub": "demo",
//   "iss": "http://openam.example.com:8080/openam/oauth2",
//   "auth_level": 0,
//   "authGrantId": "6-n-MF-dK0b_Nx6NqEAQBvEXCls",
//   "auditTrackingId": "037f02f9-d821-4f72-8563-c5050c40fdc3-51399",
//   "expires_in": 3600
// }

// Sample code for parsing the JSON response...
// Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

llActive = loJResp.BoolOf("active")
lcScope = loJResp.StringOf("scope")
lcRealm = loJResp.StringOf("realm")
lcClient_id = loJResp.StringOf("client_id")
lcUser_id = loJResp.StringOf("user_id")
lcToken_type = loJResp.StringOf("token_type")
lnExp = loJResp.IntOf("exp")
lcV_sub = loJResp.StringOf("sub")
lcIss = loJResp.StringOf("iss")
lnAuth_level = loJResp.IntOf("auth_level")
lcAuthGrantId = loJResp.StringOf("authGrantId")
lcAuditTrackingId = loJResp.StringOf("auditTrackingId")
lnExpires_in = loJResp.IntOf("expires_in")


release loHttp
release loReq
release loJsonParam1
release loJsonParam2
release loSbResponseBody
release loJResp

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	--data-urlencode 'token={{implicitGrantAccessToken}}'
	--data-urlencode 'client_id={{postmanPublicClientId}}'
https://<tenant-name>.forgeblocks.com/am/oauth2/realms/root/realms/alpha/introspect

Postman Collection Item JSON

{
  "name": "Step 3: Introspect the Access Token",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "// Tests",
          "",
          "const jsonData = JSON.parse(responseBody);",
          "",
          "pm.test(\"Status code is 200\", () => {",
          "  pm.expect(pm.response.code).to.eql(200);",
          "});",
          "",
          "pm.test(\"Response contains correct `client_id`.\", function () {",
          "    pm.expect(jsonData.client_id).to.eql(pm.collectionVariables.get(\"postmanPublicClientId\"));",
          "});",
          ""
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "urlencoded",
      "urlencoded": [
        {
          "key": "token",
          "value": "{{implicitGrantAccessToken}}",
          "description": "Access token you want to introspect.",
          "type": "text"
        },
        {
          "key": "client_id",
          "value": "{{postmanPublicClientId}}",
          "description": "The ID of the public OAuth Client.",
          "type": "text"
        }
      ]
    },
    "url": {
      "raw": "{{amUrl}}/oauth2{{realm}}/introspect",
      "host": [
        "{{amUrl}}"
      ],
      "path": [
        "oauth2{{realm}}",
        "introspect"
      ]
    },
    "description": "Retrieve metadata about the active access token, such as, approved scopes, the user that authorized the token, and the expiry time."
  },
  "response": [
    {
      "name": "Example",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "urlencoded",
          "urlencoded": [
            {
              "key": "token",
              "value": "{{implicitGrantAccessToken}}",
              "description": "Access token you want to introspect.",
              "type": "text"
            },
            {
              "key": "client_id",
              "value": "{{postmanPublicClientId}}",
              "description": "The ID of the public OAuth Client.",
              "type": "text"
            }
          ]
        },
        "url": {
          "raw": "{{amUrl}}/oauth2{{realm}}/introspect",
          "host": [
            "{{amUrl}}"
          ],
          "path": [
            "oauth2{{realm}}",
            "introspect"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "X-Frame-Options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=UTF-8"
        },
        {
          "key": "Content-Length",
          "value": "345"
        },
        {
          "key": "Date",
          "value": "Thu, 13 Aug 2020 12:16:33 GMT"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"active\": true,\n    \"scope\": \"write\",\n    \"realm\": \"/\",\n    \"client_id\": \"forgerockDemoPublicClient\",\n    \"user_id\": \"demo\",\n    \"token_type\": \"Bearer\",\n    \"exp\": 1597324518,\n    \"sub\": \"demo\",\n    \"iss\": \"http://openam.example.com:8080/openam/oauth2\",\n    \"auth_level\": 0,\n    \"authGrantId\": \"6-n-MF-dK0b_Nx6NqEAQBvEXCls\",\n    \"auditTrackingId\": \"037f02f9-d821-4f72-8563-c5050c40fdc3-51399\",\n    \"expires_in\": 3600\n}"
    }
  ]
}