Chilkat Online Tools

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

Back to Collection Items

#include <C_CkHttp.h>
#include <C_CkHttpRequest.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    HCkHttp http;
    BOOL success;
    HCkHttpRequest req;
    HCkJsonObject jsonParam1;
    HCkJsonObject jsonParam2;
    HCkHttpResponse resp;
    HCkStringBuilder sbResponseBody;
    HCkJsonObject jResp;
    int respStatusCode;
    BOOL active;
    const char *scope;
    const char *realm;
    const char *client_id;
    const char *user_id;
    const char *token_type;
    int exp;
    const char *v_sub;
    const char *iss;
    int auth_level;
    const char *authGrantId;
    const char *auditTrackingId;
    int expires_in;

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

    http = CkHttp_Create();

    req = CkHttpRequest_Create();

    jsonParam1 = CkJsonObject_Create();
    CkHttpRequest_AddParam(req,"token",CkJsonObject_emit(jsonParam1));

    jsonParam2 = CkJsonObject_Create();
    CkHttpRequest_AddParam(req,"client_id",CkJsonObject_emit(jsonParam2));

    CkHttpRequest_AddHeader(req,"Authorization","Bearer <access_token>");

    resp = CkHttp_PostUrlEncoded(http,"https://<tenant-name>.forgeblocks.com/am/oauth2/realms/root/realms/alpha/introspect",req);
    if (CkHttp_getLastMethodSuccess(http) == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkHttpRequest_Dispose(req);
        CkJsonObject_Dispose(jsonParam1);
        CkJsonObject_Dispose(jsonParam2);
        return;
    }

    sbResponseBody = CkStringBuilder_Create();
    CkHttpResponse_GetBodySb(resp,sbResponseBody);

    jResp = CkJsonObject_Create();
    CkJsonObject_LoadSb(jResp,sbResponseBody);
    CkJsonObject_putEmitCompact(jResp,FALSE);

    printf("Response Body:\n");
    printf("%s\n",CkJsonObject_emit(jResp));

    respStatusCode = CkHttpResponse_getStatusCode(resp);
    printf("Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        printf("Response Header:\n");
        printf("%s\n",CkHttpResponse_header(resp));
        printf("Failed.\n");
        CkHttpResponse_Dispose(resp);
        CkHttp_Dispose(http);
        CkHttpRequest_Dispose(req);
        CkJsonObject_Dispose(jsonParam1);
        CkJsonObject_Dispose(jsonParam2);
        CkStringBuilder_Dispose(sbResponseBody);
        CkJsonObject_Dispose(jResp);
        return;
    }

    CkHttpResponse_Dispose(resp);

    // 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

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

    active = CkJsonObject_BoolOf(jResp,"active");
    scope = CkJsonObject_stringOf(jResp,"scope");
    realm = CkJsonObject_stringOf(jResp,"realm");
    client_id = CkJsonObject_stringOf(jResp,"client_id");
    user_id = CkJsonObject_stringOf(jResp,"user_id");
    token_type = CkJsonObject_stringOf(jResp,"token_type");
    exp = CkJsonObject_IntOf(jResp,"exp");
    v_sub = CkJsonObject_stringOf(jResp,"sub");
    iss = CkJsonObject_stringOf(jResp,"iss");
    auth_level = CkJsonObject_IntOf(jResp,"auth_level");
    authGrantId = CkJsonObject_stringOf(jResp,"authGrantId");
    auditTrackingId = CkJsonObject_stringOf(jResp,"auditTrackingId");
    expires_in = CkJsonObject_IntOf(jResp,"expires_in");


    CkHttp_Dispose(http);
    CkHttpRequest_Dispose(req);
    CkJsonObject_Dispose(jsonParam1);
    CkJsonObject_Dispose(jsonParam2);
    CkStringBuilder_Dispose(sbResponseBody);
    CkJsonObject_Dispose(jResp);

    }

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}"
    }
  ]
}