Chilkat Online Tools

C++ / ForgeRock Identity Cloud Collection / Step 2: Introspect the Access Token

Back to Collection Items

#include <CkHttp.h>
#include <CkHttpRequest.h>
#include <CkJsonObject.h>
#include <CkHttpResponse.h>
#include <CkStringBuilder.h>

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

    CkHttp http;
    bool success;

    CkHttpRequest req;

    CkJsonObject jsonParam1;
    req.AddParam("token",jsonParam1.emit());

    CkJsonObject jsonParam2;
    req.AddParam("client_id",jsonParam2.emit());

    CkJsonObject jsonParam3;
    req.AddParam("client_secret",jsonParam3.emit());

    req.AddHeader("Authorization","Bearer <access_token>");

    CkHttpResponse *resp = http.PostUrlEncoded("https://<tenant-name>.forgeblocks.com/am/oauth2/realms/root/realms/alpha/introspect",req);
    if (http.get_LastMethodSuccess() == false) {
        std::cout << http.lastErrorText() << "\r\n";
        return;
    }

    CkStringBuilder sbResponseBody;
    resp->GetBodySb(sbResponseBody);

    CkJsonObject jResp;
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    std::cout << "Response Body:" << "\r\n";
    std::cout << jResp.emit() << "\r\n";

    int respStatusCode = resp->get_StatusCode();
    std::cout << "Response Status Code = " << respStatusCode << "\r\n";
    if (respStatusCode >= 400) {
        std::cout << "Response Header:" << "\r\n";
        std::cout << resp->header() << "\r\n";
        std::cout << "Failed." << "\r\n";
        delete resp;
        return;
    }

    delete resp;

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

    // {
    //   "active": true,
    //   "scope": "print",
    //   "realm": "/",
    //   "client_id": "forgerockDemoConfidentialClient",
    //   "user_id": "forgerockDemoConfidentialClient",
    //   "token_type": "Bearer",
    //   "exp": 1597324633,
    //   "sub": "forgerockDemoConfidentialClient",
    //   "iss": "http://openam.example.com:8080/openam/oauth2",
    //   "authGrantId": "5tq7oSZ62txPaK80X3Mdex4zzew",
    //   "auditTrackingId": "037f02f9-d821-4f72-8563-c5050c40fdc3-52181",
    //   "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

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.

    bool active = jResp.BoolOf("active");
    const char *scope = jResp.stringOf("scope");
    const char *realm = jResp.stringOf("realm");
    const char *client_id = jResp.stringOf("client_id");
    const char *user_id = jResp.stringOf("user_id");
    const char *token_type = jResp.stringOf("token_type");
    int exp = jResp.IntOf("exp");
    const char *v_sub = jResp.stringOf("sub");
    const char *iss = jResp.stringOf("iss");
    const char *authGrantId = jResp.stringOf("authGrantId");
    const char *auditTrackingId = jResp.stringOf("auditTrackingId");
    int expires_in = jResp.IntOf("expires_in");
    }

Curl Command

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

Postman Collection Item JSON

{
  "name": "Step 2: 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(\"postmanConfidentialClientId\"));",
          "});",
          "",
          "",
          ""
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "urlencoded",
      "urlencoded": [
        {
          "key": "token",
          "value": "{{access_token}}",
          "description": "Access token you want to introspect.",
          "type": "text"
        },
        {
          "key": "client_id",
          "value": "{{postmanConfidentialClientId}}",
          "description": "The ID of the Confidential OAuth Client.",
          "type": "text"
        },
        {
          "key": "client_secret",
          "value": "{{postmanClientSecret}}",
          "description": "The secret of the Confidential 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": "{{access_token}}",
              "description": "Access token you want to introspect.",
              "type": "text"
            },
            {
              "key": "client_id",
              "value": "{{postmanConfidentialClientId}}",
              "description": "The ID of the Confidential OAuth Client.",
              "type": "text"
            },
            {
              "key": "client_secret",
              "value": "{{postmanClientSecret}}",
              "description": "The secret of the Confidential 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": "390"
        },
        {
          "key": "Date",
          "value": "Thu, 13 Aug 2020 12:17:22 GMT"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"active\": true,\n    \"scope\": \"print\",\n    \"realm\": \"/\",\n    \"client_id\": \"forgerockDemoConfidentialClient\",\n    \"user_id\": \"forgerockDemoConfidentialClient\",\n    \"token_type\": \"Bearer\",\n    \"exp\": 1597324633,\n    \"sub\": \"forgerockDemoConfidentialClient\",\n    \"iss\": \"http://openam.example.com:8080/openam/oauth2\",\n    \"authGrantId\": \"5tq7oSZ62txPaK80X3Mdex4zzew\",\n    \"auditTrackingId\": \"037f02f9-d821-4f72-8563-c5050c40fdc3-52181\",\n    \"expires_in\": 3600\n}"
    }
  ]
}