Chilkat Online Tools

TCL / ForgeRock Identity Cloud Collection / Step 2: Get Authorization Code

Back to Collection Items

load ./chilkat.dll

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

set http [new_CkHttp]

set req [new_CkHttpRequest]

CkHttpRequest_AddParam $req "scope" "edit"
CkHttpRequest_AddParam $req "response_type" "code"

set jsonParam3 [new_CkJsonObject]

CkHttpRequest_AddParam $req "client_id" [CkJsonObject_emit $jsonParam3]

set jsonParam4 [new_CkJsonObject]

CkHttpRequest_AddParam $req "csrf" [CkJsonObject_emit $jsonParam4]

set jsonParam5 [new_CkJsonObject]

CkHttpRequest_AddParam $req "redirect_uri" [CkJsonObject_emit $jsonParam5]
CkHttpRequest_AddParam $req "decision" "allow"

CkHttpRequest_AddHeader $req "Authorization" "Bearer <access_token>"
CkHttpRequest_AddHeader $req "Cookie" "{{cookieName}}={{demoSSOToken}}"

# resp is a CkHttpResponse
set resp [CkHttp_PostUrlEncoded $http "https://<tenant-name>.forgeblocks.com/am/oauth2/realms/root/realms/alpha/authorize" $req]
if {[CkHttp_get_LastMethodSuccess $http] == 0} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    delete_CkHttpRequest $req
    delete_CkJsonObject $jsonParam3
    delete_CkJsonObject $jsonParam4
    delete_CkJsonObject $jsonParam5
    exit
}

set sbResponseBody [new_CkStringBuilder]

CkHttpResponse_GetBodySb $resp $sbResponseBody

set jResp [new_CkJsonObject]

CkJsonObject_LoadSb $jResp $sbResponseBody
CkJsonObject_put_EmitCompact $jResp 0

puts "Response Body:"
puts [CkJsonObject_emit $jResp]

set respStatusCode [CkHttpResponse_get_StatusCode $resp]
puts "Response Status Code = $respStatusCode"
if {$respStatusCode >= 400} then {
    puts "Response Header:"
    puts [CkHttpResponse_header $resp]
    puts "Failed."
    delete_CkHttpResponse $resp

    delete_CkHttp $http
    delete_CkHttpRequest $req
    delete_CkJsonObject $jsonParam3
    delete_CkJsonObject $jsonParam4
    delete_CkJsonObject $jsonParam5
    delete_CkStringBuilder $sbResponseBody
    delete_CkJsonObject $jResp
    exit
}

delete_CkHttpResponse $resp

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

# {
#   "args": {
#     "client_id": "forgerockDemoConfidentialClient",
#     "code": "TGBSt2LHHruGUU0lAOrrjr08jtA",
#     "iss": "http://openam.example.com:8080/openam/oauth2"
#   },
#   "data": "",
#   "files": {},
#   "form": {},
#   "headers": {
#     "Accept": "*/*",
#     "Accept-Encoding": "gzip, deflate, br",
#     "Cookie": "amlbcookie=01; iPlanetDirectoryPro=sOpI1RvbCgvlQk.*AAJTSQACMDEA.*",
#     "Host": "httpbin.org",
#     "Referer": "http://openam.example.com:8080/openam/oauth2/realms/root/authorize",
#     "User-Agent": "PostmanRuntime/7.26.3",
#     "X-Amzn-Trace-Id": "Root=1-5f35302f-6babefe013e7ad28bf1a82f0"
#   },
#   "json": null,
#   "method": "GET",
#   "origin": "5.65.200.229",
#   "url": "https://httpbin.org/anything?code=TGBSt2LHHruGUU0lAOrrjr08jtA&iss=http:%2F%2Fopenam.example.com:8080%2Fopenam%2Foauth2&client_id=forgerockDemoConfidentialClient"
# }

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

set Client_id [CkJsonObject_stringOf $jResp "args.client_id"]
set Code [CkJsonObject_stringOf $jResp "args.code"]
set Iss [CkJsonObject_stringOf $jResp "args.iss"]
set data [CkJsonObject_stringOf $jResp "data"]
set Accept [CkJsonObject_stringOf $jResp "headers.Accept"]
set Accept_Encoding [CkJsonObject_stringOf $jResp "headers.Accept-Encoding"]
set Cookie [CkJsonObject_stringOf $jResp "headers.Cookie"]
set Host [CkJsonObject_stringOf $jResp "headers.Host"]
set Referer [CkJsonObject_stringOf $jResp "headers.Referer"]
set User_Agent [CkJsonObject_stringOf $jResp "headers.User-Agent"]
set X_Amzn_Trace_Id [CkJsonObject_stringOf $jResp "headers.X-Amzn-Trace-Id"]
set json [CkJsonObject_stringOf $jResp "json"]
set method [CkJsonObject_stringOf $jResp "method"]
set origin [CkJsonObject_stringOf $jResp "origin"]
set url [CkJsonObject_stringOf $jResp "url"]

delete_CkHttp $http
delete_CkHttpRequest $req
delete_CkJsonObject $jsonParam3
delete_CkJsonObject $jsonParam4
delete_CkJsonObject $jsonParam5
delete_CkStringBuilder $sbResponseBody
delete_CkJsonObject $jResp

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Cookie: {{cookieName}}={{demoSSOToken}}"
	--data-urlencode 'scope=edit'
	--data-urlencode 'response_type=code'
	--data-urlencode 'client_id={{postmanConfidentialClientId}}'
	--data-urlencode 'csrf={{demoSSOToken}}'
	--data-urlencode 'redirect_uri={{redirect_uri}}'
	--data-urlencode 'decision=allow'
https://<tenant-name>.forgeblocks.com/am/oauth2/realms/root/realms/alpha/authorize

Postman Collection Item JSON

{
  "name": "Step 2: Get Authorization Code",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "const jsonData = pm.response.json();",
          "",
          "if(pm.response.code == 200)",
          "{",
          "    if(jsonData.args.code && jsonData.args.code != \"\") {",
          "        pm.globals.set(\"authorization_code\", jsonData.args.code);",
          "    }",
          "}",
          "",
          "// TESTS",
          "",
          "pm.test(\"Follow redirects is enabled in Postman (Status code not 302)\", () => {",
          "  // If response was 302, ensure Postman is following redirects.  ",
          "  pm.response.to.not.have.status(302);",
          "});",
          "",
          "pm.test(\"Response from httpbin contained `code` argument\", () => {",
          "    pm.expect(jsonData.args.code).to.be.a(\"string\");",
          "});"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Cookie",
        "value": "{{cookieName}}={{demoSSOToken}}",
        "type": "text"
      }
    ],
    "body": {
      "mode": "urlencoded",
      "urlencoded": [
        {
          "key": "scope",
          "value": "edit",
          "description": "Strings that are presented to the user for approval and included in tokens so that the protected resource may make decisions about what to give access to.",
          "type": "text"
        },
        {
          "key": "response_type",
          "value": "code",
          "description": "Response types the client will support and use.",
          "type": "text"
        },
        {
          "key": "client_id",
          "value": "{{postmanConfidentialClientId}}",
          "description": "The ID of the Confidential OAuth Client.",
          "type": "text"
        },
        {
          "key": "csrf",
          "value": "{{demoSSOToken}}",
          "description": "SSO token of a ForgeRock user, to protect against cross-site request forgery.",
          "type": "text"
        },
        {
          "key": "redirect_uri",
          "value": "{{redirect_uri}}",
          "description": "The complete URI to which client redirects the user if the request is successful.",
          "type": "text"
        },
        {
          "key": "decision",
          "value": "allow",
          "description": "Decision that grants access to the authentication code. When using a browser, the user would consent that the client can access their information. This flow can be used machine-to-machine, by assuming consent, for example between two services provided by the same organization.",
          "type": "text"
        }
      ]
    },
    "url": {
      "raw": "{{amUrl}}/oauth2{{realm}}/authorize",
      "host": [
        "{{amUrl}}"
      ],
      "path": [
        "oauth2{{realm}}",
        "authorize"
      ]
    },
    "description": "Make a call to the authorization server's authorization endpoint, specifying the SSO token of the user in a cookie."
  },
  "response": [
    {
      "name": "Example - parameters captured by HttpBin",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "urlencoded",
          "urlencoded": [
            {
              "key": "scope",
              "value": "edit",
              "description": "Strings that are presented to the user for approval and included in tokens so that the protected resource may make decisions about what to give access to.",
              "type": "text"
            },
            {
              "key": "response_type",
              "value": "code",
              "description": "Response types the client will support and use.",
              "type": "text"
            },
            {
              "key": "client_id",
              "value": "{{postmanConfidentialClientId}}",
              "description": "The ID of the Confidential OAuth Client.",
              "type": "text"
            },
            {
              "key": "csrf",
              "value": "{{demoSSOToken}}",
              "description": "SSO token of a ForgeRock user, to protect against cross-site request forgery.",
              "type": "text"
            },
            {
              "key": "redirect_uri",
              "value": "{{redirect_uri}}",
              "description": "The complete URI to which client redirects the user if the request is successful.",
              "type": "text"
            },
            {
              "key": "decision",
              "value": "allow",
              "description": "Decision that grants access to the authentication code. When using a browser, the user would consent that the client can access their information. This flow can be used machine-to-machine, by assuming consent, for example between two services provided by the same organization.",
              "type": "text"
            }
          ]
        },
        "url": {
          "raw": "{{amUrl}}/oauth2{{realm}}/authorize",
          "host": [
            "{{amUrl}}"
          ],
          "path": [
            "oauth2{{realm}}",
            "authorize"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Thu, 13 Aug 2020 12:21:03 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Content-Length",
          "value": "937"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "Server",
          "value": "gunicorn/19.9.0"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Access-Control-Allow-Credentials",
          "value": "true"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"args\": {\n        \"client_id\": \"forgerockDemoConfidentialClient\",\n        \"code\": \"TGBSt2LHHruGUU0lAOrrjr08jtA\",\n        \"iss\": \"http://openam.example.com:8080/openam/oauth2\"\n    },\n    \"data\": \"\",\n    \"files\": {},\n    \"form\": {},\n    \"headers\": {\n        \"Accept\": \"*/*\",\n        \"Accept-Encoding\": \"gzip, deflate, br\",\n        \"Cookie\": \"amlbcookie=01; iPlanetDirectoryPro=sOpI1RvbCgvlQk.*AAJTSQACMDEA.*\",\n        \"Host\": \"httpbin.org\",\n        \"Referer\": \"http://openam.example.com:8080/openam/oauth2/realms/root/authorize\",\n        \"User-Agent\": \"PostmanRuntime/7.26.3\",\n        \"X-Amzn-Trace-Id\": \"Root=1-5f35302f-6babefe013e7ad28bf1a82f0\"\n    },\n    \"json\": null,\n    \"method\": \"GET\",\n    \"origin\": \"5.65.200.229\",\n    \"url\": \"https://httpbin.org/anything?code=TGBSt2LHHruGUU0lAOrrjr08jtA&iss=http:%2F%2Fopenam.example.com:8080%2Fopenam%2Foauth2&client_id=forgerockDemoConfidentialClient\"\n}"
    }
  ]
}