Chilkat Online Tools

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

Back to Collection Items

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttpRequest.pb"

Procedure ChilkatExample()

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    req.i = CkHttpRequest::ckCreate()
    If req.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    jsonParam1.i = CkJsonObject::ckCreate()
    If jsonParam1.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpRequest::ckAddParam(req,"token",CkJsonObject::ckEmit(jsonParam1))

    jsonParam2.i = CkJsonObject::ckCreate()
    If jsonParam2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpRequest::ckAddParam(req,"client_id",CkJsonObject::ckEmit(jsonParam2))

    CkHttpRequest::ckAddHeader(req,"Authorization","Bearer <access_token>")

    resp.i = CkHttp::ckPostUrlEncoded(http,"https://<tenant-name>.forgeblocks.com/am/oauth2/realms/root/realms/alpha/introspect",req)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkHttpRequest::ckDispose(req)
        CkJsonObject::ckDispose(jsonParam1)
        CkJsonObject::ckDispose(jsonParam2)
        ProcedureReturn
    EndIf

    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpResponse::ckGetBodySb(resp,sbResponseBody)

    jResp.i = CkJsonObject::ckCreate()
    If jResp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(jResp,sbResponseBody)
    CkJsonObject::setCkEmitCompact(jResp, 0)

    Debug "Response Body:"
    Debug CkJsonObject::ckEmit(jResp)

    respStatusCode.i = CkHttpResponse::ckStatusCode(resp)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttpResponse::ckHeader(resp)
        Debug "Failed."
        CkHttpResponse::ckDispose(resp)

        CkHttp::ckDispose(http)
        CkHttpRequest::ckDispose(req)
        CkJsonObject::ckDispose(jsonParam1)
        CkJsonObject::ckDispose(jsonParam2)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

    CkHttpResponse::ckDispose(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

    active.i = CkJsonObject::ckBoolOf(jResp,"active")
    scope.s = CkJsonObject::ckStringOf(jResp,"scope")
    realm.s = CkJsonObject::ckStringOf(jResp,"realm")
    client_id.s = CkJsonObject::ckStringOf(jResp,"client_id")
    user_id.s = CkJsonObject::ckStringOf(jResp,"user_id")
    token_type.s = CkJsonObject::ckStringOf(jResp,"token_type")
    exp.i = CkJsonObject::ckIntOf(jResp,"exp")
    v_sub.s = CkJsonObject::ckStringOf(jResp,"sub")
    iss.s = CkJsonObject::ckStringOf(jResp,"iss")
    auth_level.i = CkJsonObject::ckIntOf(jResp,"auth_level")
    authGrantId.s = CkJsonObject::ckStringOf(jResp,"authGrantId")
    auditTrackingId.s = CkJsonObject::ckStringOf(jResp,"auditTrackingId")
    expires_in.i = CkJsonObject::ckIntOf(jResp,"expires_in")


    CkHttp::ckDispose(http)
    CkHttpRequest::ckDispose(req)
    CkJsonObject::ckDispose(jsonParam1)
    CkJsonObject::ckDispose(jsonParam2)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure

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