Chilkat Online Tools

unicodeC / ForgeRock Identity Cloud Collection / Step 5: Introspect the Access Token

Back to Collection Items

#include <C_CkHttpW.h>
#include <C_CkHttpRequestW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    HCkHttpW http;
    BOOL success;
    HCkHttpRequestW req;
    HCkJsonObjectW jsonParam1;
    HCkJsonObjectW jsonParam2;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    BOOL active;
    const wchar_t *scope;
    const wchar_t *realm;
    const wchar_t *client_id;
    const wchar_t *user_id;
    const wchar_t *token_type;
    int exp;
    const wchar_t *v_sub;
    const wchar_t *iss;
    int auth_level;
    const wchar_t *authGrantId;
    const wchar_t *auditTrackingId;
    int expires_in;

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

    http = CkHttpW_Create();

    req = CkHttpRequestW_Create();

    jsonParam1 = CkJsonObjectW_Create();
    CkHttpRequestW_AddParam(req,L"token",CkJsonObjectW_emit(jsonParam1));

    jsonParam2 = CkJsonObjectW_Create();
    CkHttpRequestW_AddParam(req,L"client_id",CkJsonObjectW_emit(jsonParam2));

    CkHttpRequestW_AddHeader(req,L"Authorization",L"Bearer <access_token>");

    resp = CkHttpW_PostUrlEncoded(http,L"https://<tenant-name>.forgeblocks.com/am/oauth2/realms/root/realms/alpha/introspect",req);
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkHttpRequestW_Dispose(req);
        CkJsonObjectW_Dispose(jsonParam1);
        CkJsonObjectW_Dispose(jsonParam2);
        return;
    }

    sbResponseBody = CkStringBuilderW_Create();
    CkHttpResponseW_GetBodySb(resp,sbResponseBody);

    jResp = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jResp,sbResponseBody);
    CkJsonObjectW_putEmitCompact(jResp,FALSE);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",CkJsonObjectW_emit(jResp));

    respStatusCode = CkHttpResponseW_getStatusCode(resp);
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkHttpResponseW_header(resp));
        wprintf(L"Failed.\n");
        CkHttpResponseW_Dispose(resp);
        CkHttpW_Dispose(http);
        CkHttpRequestW_Dispose(req);
        CkJsonObjectW_Dispose(jsonParam1);
        CkJsonObjectW_Dispose(jsonParam2);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    CkHttpResponseW_Dispose(resp);

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

    // {
    //   "active": true,
    //   "scope": "manage",
    //   "realm": "/",
    //   "client_id": "forgerockDemoPublicClient",
    //   "user_id": "demo",
    //   "token_type": "Bearer",
    //   "exp": 1597324784,
    //   "sub": "demo",
    //   "iss": "http://openam.example.com:8080/openam/oauth2",
    //   "auth_level": 0,
    //   "authGrantId": "bdtxHp1kka6iin4Q2KpNgCfgcVs",
    //   "auditTrackingId": "037f02f9-d821-4f72-8563-c5050c40fdc3-53256",
    //   "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.

    active = CkJsonObjectW_BoolOf(jResp,L"active");
    scope = CkJsonObjectW_stringOf(jResp,L"scope");
    realm = CkJsonObjectW_stringOf(jResp,L"realm");
    client_id = CkJsonObjectW_stringOf(jResp,L"client_id");
    user_id = CkJsonObjectW_stringOf(jResp,L"user_id");
    token_type = CkJsonObjectW_stringOf(jResp,L"token_type");
    exp = CkJsonObjectW_IntOf(jResp,L"exp");
    v_sub = CkJsonObjectW_stringOf(jResp,L"sub");
    iss = CkJsonObjectW_stringOf(jResp,L"iss");
    auth_level = CkJsonObjectW_IntOf(jResp,L"auth_level");
    authGrantId = CkJsonObjectW_stringOf(jResp,L"authGrantId");
    auditTrackingId = CkJsonObjectW_stringOf(jResp,L"auditTrackingId");
    expires_in = CkJsonObjectW_IntOf(jResp,L"expires_in");


    CkHttpW_Dispose(http);
    CkHttpRequestW_Dispose(req);
    CkJsonObjectW_Dispose(jsonParam1);
    CkJsonObjectW_Dispose(jsonParam2);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }

Curl Command

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

Postman Collection Item JSON

{
  "name": "Step 5: 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": "{{access_token}}",
          "description": "Access token you want to introspect.",
          "type": "text"
        },
        {
          "key": "client_id",
          "value": "{{postmanPublicClientId}}",
          "description": "The ID 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": "{{postmanPublicClientId}}",
              "description": "The ID 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": "346"
        },
        {
          "key": "Date",
          "value": "Thu, 13 Aug 2020 12:20:17 GMT"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"active\": true,\n    \"scope\": \"manage\",\n    \"realm\": \"/\",\n    \"client_id\": \"forgerockDemoPublicClient\",\n    \"user_id\": \"demo\",\n    \"token_type\": \"Bearer\",\n    \"exp\": 1597324784,\n    \"sub\": \"demo\",\n    \"iss\": \"http://openam.example.com:8080/openam/oauth2\",\n    \"auth_level\": 0,\n    \"authGrantId\": \"bdtxHp1kka6iin4Q2KpNgCfgcVs\",\n    \"auditTrackingId\": \"037f02f9-d821-4f72-8563-c5050c40fdc3-53256\",\n    \"expires_in\": 3600\n}"
    }
  ]
}