Chilkat Online Tools

Android / ForgeRock Identity Cloud Collection / Step 7: Validate Users' Session

Back to Collection Items

// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

    CkHttp http = new CkHttp();
    boolean success;

    http.SetRequestHeader("Content-Type","application/json");
    http.SetRequestHeader("Accept-API-Version","resource=1.0");
    http.SetRequestHeader("{{cookieName}}","{{demoSSOToken}}");

    CkHttpResponse resp = http.QuickRequest("POST","https://<tenant-name>.forgeblocks.com/am/json/realms/root/realms/alpha/sessions?_prettyPrint=true&_action=validate");
    if (http.get_LastMethodSuccess() == false) {
        Log.i(TAG, http.lastErrorText());
        return;
        }

    Log.i(TAG, String.valueOf(resp.get_StatusCode()));
    Log.i(TAG, resp.bodyStr());


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}

Curl Command

curl -X POST
	-H "Accept-API-Version: resource=1.0"
	-H "Content-Type: application/json"
	-H "{{cookieName}}: {{demoSSOToken}}"
https://<tenant-name>.forgeblocks.com/am/json/realms/root/realms/alpha/sessions?_prettyPrint=true&_action=validate

Postman Collection Item JSON

{
  "name": "Step 7: Validate Users' Session",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "const jsonData = JSON.parse(responseBody);",
          "",
          "pm.test(\"Status code is 200\", () => {",
          "  pm.expect(pm.response.code).to.eql(200);",
          "});",
          "",
          "pm.test(\"Session is valid\", function () {",
          "    pm.expect(jsonData.valid).to.eql(true);",
          "});",
          "",
          "pm.test(\"Response has expected data\", function () {",
          "    pm.expect(jsonData).to.have.all.keys('valid','sessionUid','uid','realm');",
          "});"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "description": "(Required) ",
        "key": "Accept-API-Version",
        "value": "resource=1.0"
      },
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "{{cookieName}}",
        "value": "{{demoSSOToken}}",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": ""
    },
    "url": {
      "raw": "{{amUrl}}/json{{realm}}/sessions?_prettyPrint=true&_action=validate",
      "host": [
        "{{amUrl}}"
      ],
      "path": [
        "json{{realm}}",
        "sessions"
      ],
      "query": [
        {
          "key": "_fields",
          "value": "",
          "description": "Optional parameter containing a comma separated list of field references specifying which fields of the targeted JSON resource should be returned.",
          "disabled": true
        },
        {
          "key": "_prettyPrint",
          "value": "true",
          "description": "Optional parameter requesting that the returned JSON resource content should be formatted to be more human readable."
        },
        {
          "key": "_action",
          "value": "validate"
        }
      ]
    },
    "description": "It checks that the specified SSO Token Id is valid or not. If there is any problem getting or validating the token which causes an exception the json response will be false. In addition if the token is expired then the json response will be set to false. Otherwise it will be set to true."
  },
  "response": [
    {
      "name": "Success",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "description": "(Required) ",
            "key": "Accept-API-Version",
            "value": "resource=1.0"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"tokenId\": \"<string>\"\n}"
        },
        "url": {
          "raw": "{{amUrl}}/json{{realm}}/sessions#4.0_action_validate?&_prettyPrint=true&_action=<string>",
          "host": [
            "{{amUrl}}"
          ],
          "path": [
            "json{{realm}}",
            "sessions"
          ],
          "hash": "4.0_action_validate?&_prettyPrint=true&_action=<string>"
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "text",
      "header": [
        {
          "key": "Content-Type",
          "value": "*/*"
        }
      ],
      "cookie": [
      ],
      "body": ""
    },
    {
      "name": "It happens when when the SSO header is missing in the request or user token is not valid.",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "description": "(Required) ",
            "key": "Accept-API-Version",
            "value": "resource=1.0"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"tokenId\": \"<string>\"\n}"
        },
        "url": {
          "raw": "{{amUrl}}/json{{realm}}/sessions#4.0_action_validate?&_prettyPrint=true&_action=<string>",
          "host": [
            "{{amUrl}}"
          ],
          "path": [
            "json{{realm}}",
            "sessions"
          ],
          "hash": "4.0_action_validate?&_prettyPrint=true&_action=<string>"
        }
      },
      "status": "Unauthorized",
      "code": 401,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"code\": 93541721,\n \"message\": \"minim\",\n \"reason\": \"sint Excepteur\",\n \"detail\": \"veniam id qui\"\n}"
    }
  ]
}