Chilkat Online Tools

DataFlex / ForgeRock Identity Cloud Collection / Step 3: Get Session Info

Back to Collection Items

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoHttp
    Boolean iSuccess
    Variant vResp
    Handle hoResp
    Variant vSbResponseBody
    Handle hoSbResponseBody
    Handle hoJResp
    Integer iRespStatusCode
    String sUsername
    String sUniversalId
    String sRealm
    String sLatestAccessTime
    String sMaxIdleExpirationTime
    String sMaxSessionExpirationTime
    String sAMCtxId
    String sTemp1
    Boolean bTemp1

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

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    Send ComSetRequestHeader To hoHttp "{{cookieName}}" "{{demoSSOToken}}"
    Send ComSetRequestHeader To hoHttp "Accept-API-Version" "resource=2.0, protocol=1.0"

    Get ComQuickRequest Of hoHttp "POST" "https://<tenant-name>.forgeblocks.com/am/json/realms/root/realms/alpha/sessions?_action=getSessionInfo" To vResp
    If (IsComObject(vResp)) Begin
        Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
        Set pvComObject Of hoResp To vResp
    End
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
    If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
        Send CreateComObject of hoSbResponseBody
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComGetBodySb Of hoResp vSbResponseBody To iSuccess

    Get Create (RefClass(cComChilkatJsonObject)) To hoJResp
    If (Not(IsComObjectCreated(hoJResp))) Begin
        Send CreateComObject of hoJResp
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComLoadSb Of hoJResp vSbResponseBody To iSuccess
    Set ComEmitCompact Of hoJResp To False

    Showln "Response Body:"
    Get ComEmit Of hoJResp To sTemp1
    Showln sTemp1

    Get ComStatusCode Of hoResp To iRespStatusCode
    Showln "Response Status Code = " iRespStatusCode
    If (iRespStatusCode >= 400) Begin
        Showln "Response Header:"
        Get ComHeader Of hoResp To sTemp1
        Showln sTemp1
        Showln "Failed."
        Send Destroy of hoResp
        Procedure_Return
    End

    Send Destroy of hoResp

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

    // {
    //   "username": "demo",
    //   "universalId": "id=demo,ou=user,dc=openam,dc=forgerock,dc=org",
    //   "realm": "/",
    //   "latestAccessTime": "2020-08-12T14:23:23Z",
    //   "maxIdleExpirationTime": "2020-08-12T14:53:23Z",
    //   "maxSessionExpirationTime": "2020-08-12T16:23:22Z",
    //   "properties": {
    //     "AMCtxId": "e2fe9cef-dce1-41e4-ab15-0c85a88bf8cd-66167"
    //   }
    // }

    // Sample code for parsing the JSON response...
    // Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    Get ComStringOf Of hoJResp "username" To sUsername
    Get ComStringOf Of hoJResp "universalId" To sUniversalId
    Get ComStringOf Of hoJResp "realm" To sRealm
    Get ComStringOf Of hoJResp "latestAccessTime" To sLatestAccessTime
    Get ComStringOf Of hoJResp "maxIdleExpirationTime" To sMaxIdleExpirationTime
    Get ComStringOf Of hoJResp "maxSessionExpirationTime" To sMaxSessionExpirationTime
    Get ComStringOf Of hoJResp "properties.AMCtxId" To sAMCtxId


End_Procedure

Curl Command

curl -X POST
	-H "{{cookieName}}: {{demoSSOToken}}"
	-H "Accept-API-Version: resource=2.0, protocol=1.0"
https://<tenant-name>.forgeblocks.com/am/json/realms/root/realms/alpha/sessions?_action=getSessionInfo

Postman Collection Item JSON

{
  "name": "Step 3: Get Session Info",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "// Tests",
          "",
          "pm.test(\"Status code is 200\", () => {",
          "  pm.expect(pm.response.code).to.eql(200);",
          "});",
          ""
        ],
        "type": "text/javascript"
      }
    }
  ],
  "protocolProfileBehavior": {
    "disableCookies": true
  },
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "{{cookieName}}",
        "value": "{{demoSSOToken}}",
        "description": "Specifies the session token to query. To obtain information about a different token, specify it in the `tokenId` parameter of the POST body."
      },
      {
        "description": "Specifies the version of the `/json/sessions` endpoint to use.",
        "key": "Accept-API-Version",
        "type": "text",
        "value": "resource=2.0, protocol=1.0"
      }
    ],
    "url": {
      "raw": "{{amUrl}}/json{{realm}}/sessions?_action=getSessionInfo",
      "host": [
        "{{amUrl}}"
      ],
      "path": [
        "json{{realm}}",
        "sessions"
      ],
      "query": [
        {
          "key": "_action",
          "value": "getSessionInfo",
          "description": "Specifies a request to get information about a session, such as idle times."
        }
      ]
    },
    "description": "Once the user has completed all the required callbacks, they are issued their session token. You can use the token to obtain information about the session.\n\nTo obtain information about a session, perform an HTTP POST to the `/json/sessions/` endpoint, using the `getSessionInfo` action. \n\nThe endpoint returns information about the provided session token by default. \n\n> Tip: \n>\n> To get information about a different session token, include it in the POST body, as the value of the `tokenId` parameter."
  },
  "response": [
    {
      "name": "Success",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "iPlanetDirectoryPro",
            "value": "{{demoSSOToken}}",
            "description": "Specifies the session token to query. To obtain information about a different token, specify it in the `tokenId` parameter of the POST body."
          },
          {
            "description": "Specifies the version of the `/json/sessions` endpoint to use.",
            "key": "Accept-API-Version",
            "type": "text",
            "value": "resource=2.0, protocol=1.0"
          }
        ],
        "url": {
          "raw": "{{amUrl}}/json{{realm}}/sessions?_action=getSessionInfo",
          "host": [
            "{{amUrl}}"
          ],
          "path": [
            "json{{realm}}",
            "sessions"
          ],
          "query": [
            {
              "key": "_action",
              "value": "getSessionInfo",
              "description": "Specifies a request to get information about a session, such as idle times."
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "X-Frame-Options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "Cache-Control",
          "value": "private"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache, no-store, must-revalidate"
        },
        {
          "key": "Content-API-Version",
          "value": "resource=2.2"
        },
        {
          "key": "Expires",
          "value": "0"
        },
        {
          "key": "Pragma",
          "value": "no-cache"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=UTF-8"
        },
        {
          "key": "Content-Length",
          "value": "302"
        },
        {
          "key": "Date",
          "value": "Wed, 12 Aug 2020 14:23:25 GMT"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"username\": \"demo\",\n    \"universalId\": \"id=demo,ou=user,dc=openam,dc=forgerock,dc=org\",\n    \"realm\": \"/\",\n    \"latestAccessTime\": \"2020-08-12T14:23:23Z\",\n    \"maxIdleExpirationTime\": \"2020-08-12T14:53:23Z\",\n    \"maxSessionExpirationTime\": \"2020-08-12T16:23:22Z\",\n    \"properties\": {\n        \"AMCtxId\": \"e2fe9cef-dce1-41e4-ab15-0c85a88bf8cd-66167\"\n    }\n}"
    }
  ]
}