Chilkat Online Tools

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

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    DECLARE @req int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.HttpRequest', @req OUT

    DECLARE @jsonParam1 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jsonParam1 OUT

    EXEC sp_OAMethod @jsonParam1, 'Emit', @sTmp0 OUT
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'token', @sTmp0

    DECLARE @jsonParam2 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jsonParam2 OUT

    EXEC sp_OAMethod @jsonParam2, 'Emit', @sTmp0 OUT
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'client_id', @sTmp0

    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Authorization', 'Bearer <access_token>'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostUrlEncoded', @resp OUT, 'https://<tenant-name>.forgeblocks.com/am/oauth2/realms/root/realms/alpha/introspect', @req
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @jsonParam1
        EXEC @hr = sp_OADestroy @jsonParam2
        RETURN
      END

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @jsonParam1
        EXEC @hr = sp_OADestroy @jsonParam2
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @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": 1597324710,
    --   "sub": "demo",
    --   "iss": "http://openam.example.com:8080/openam/oauth2",
    --   "auth_level": 0,
    --   "authGrantId": "NqnPcBkWNYRwRfnYPDKCGvSWLoo",
    --   "auditTrackingId": "037f02f9-d821-4f72-8563-c5050c40fdc3-52703",
    --   "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

    DECLARE @active int
    EXEC sp_OAMethod @jResp, 'BoolOf', @active OUT, 'active'
    DECLARE @scope nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @scope OUT, 'scope'
    DECLARE @realm nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @realm OUT, 'realm'
    DECLARE @client_id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @client_id OUT, 'client_id'
    DECLARE @user_id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @user_id OUT, 'user_id'
    DECLARE @token_type nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @token_type OUT, 'token_type'
    DECLARE @exp int
    EXEC sp_OAMethod @jResp, 'IntOf', @exp OUT, 'exp'
    DECLARE @v_sub nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @v_sub OUT, 'sub'
    DECLARE @iss nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @iss OUT, 'iss'
    DECLARE @auth_level int
    EXEC sp_OAMethod @jResp, 'IntOf', @auth_level OUT, 'auth_level'
    DECLARE @authGrantId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @authGrantId OUT, 'authGrantId'
    DECLARE @auditTrackingId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @auditTrackingId OUT, 'auditTrackingId'
    DECLARE @expires_in int
    EXEC sp_OAMethod @jResp, 'IntOf', @expires_in OUT, 'expires_in'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @req
    EXEC @hr = sp_OADestroy @jsonParam1
    EXEC @hr = sp_OADestroy @jsonParam2
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

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 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": "{{access_token}}",
              "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:18:39 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\": 1597324710,\n    \"sub\": \"demo\",\n    \"iss\": \"http://openam.example.com:8080/openam/oauth2\",\n    \"auth_level\": 0,\n    \"authGrantId\": \"NqnPcBkWNYRwRfnYPDKCGvSWLoo\",\n    \"auditTrackingId\": \"037f02f9-d821-4f72-8563-c5050c40fdc3-52703\",\n    \"expires_in\": 3600\n}"
    }
  ]
}