Chilkat Online Tools

SQL Server / ForgeRock Identity Cloud Collection / Step 2a: Authenticate as ID Cloud Admin using Username / Password Callback

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
    -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    -- Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    -- The following JSON is sent in the request body.

    -- {
    --   "authId": "{{authId}}",
    --   "callbacks": [
    --     {
    --       "type": "NameCallback",
    --       "output": [
    --         {
    --           "name": "prompt",
    --           "value": "User Name"
    --         }
    --       ],
    --       "input": [
    --         {
    --           "name": "IDToken1",
    --           "value": "{{IDCloudAdminUsername}}"
    --         }
    --       ],
    --       "_id": 0
    --     },
    --     {
    --       "type": "PasswordCallback",
    --       "output": [
    --         {
    --           "name": "prompt",
    --           "value": "Password"
    --         }
    --       ],
    --       "input": [
    --         {
    --           "name": "IDToken2",
    --           "value": "{{IDCloudAdminPassword}}"
    --         }
    --       ],
    --       "_id": 1
    --     }
    --   ]
    -- }

    DECLARE @json int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'authId', '{{authId}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'callbacks[0].type', 'NameCallback'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'callbacks[0].output[0].name', 'prompt'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'callbacks[0].output[0].value', 'User Name'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'callbacks[0].input[0].name', 'IDToken1'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'callbacks[0].input[0].value', '{{IDCloudAdminUsername}}'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'callbacks[0]._id', 0
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'callbacks[1].type', 'PasswordCallback'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'callbacks[1].output[0].name', 'prompt'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'callbacks[1].output[0].value', 'Password'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'callbacks[1].input[0].name', 'IDToken2'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'callbacks[1].input[0].value', '{{IDCloudAdminPassword}}'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'callbacks[1]._id', 1

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Accept-API-Version', 'resource=2.1, protocol=1.0'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://<tenant-name>.forgeblocks.com/am/json/authenticate', 'application/json', @json
    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 @json
        RETURN
      END

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @resp


    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json


END
GO

Curl Command

curl -X POST
	-H "Content-Type: application/json"
	-H "Accept-API-Version: resource=2.1, protocol=1.0"
	-d '{
    "authId": "{{authId}}",
    "callbacks": [
        {
            "type": "NameCallback",
            "output": [
                {
                    "name": "prompt",
                    "value": "User Name"
                }
            ],
            "input": [
                {
                    "name": "IDToken1",
                    "value": "{{IDCloudAdminUsername}}"
                }
            ],
            "_id": 0
        },
        {
            "type": "PasswordCallback",
            "output": [
                {
                    "name": "prompt",
                    "value": "Password"
                }
            ],
            "input": [
                {
                    "name": "IDToken2",
                    "value": "{{IDCloudAdminPassword}}"
                }
            ],
            "_id": 1
        }
    ]
}'
https://<tenant-name>.forgeblocks.com/am/json/authenticate

Postman Collection Item JSON

{
  "name": "Step 2a: Authenticate as ID Cloud Admin using Username / Password Callback",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "const JSONResponse = JSON.parse(responseBody);",
          "",
          "// Did request have a callback?",
          "if(JSONResponse.authId && JSONResponse.authId != \"\")",
          "{",
          "  // Set `authId` variable",
          "  pm.globals.set(\"authId\", JSONResponse.authId);",
          "}",
          "",
          "// Tests",
          "pm.test(\"Status code is 200.\", () => {",
          "  pm.expect(pm.response.code).to.eql(200);",
          "});",
          "",
          "pm.test(\"Response contains authId\", function () {",
          "    pm.expect(JSONResponse).to.have.any.keys('authId');",
          "});",
          ""
        ],
        "type": "text/javascript"
      }
    }
  ],
  "protocolProfileBehavior": {
    "disableCookies": true
  },
  "request": {
    "method": "POST",
    "header": [
      {
        "description": "Specifies that the `/json/authenticate` endpoint uses JSON format for requests.",
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "description": "Specifies the version of the `/json/authenticate` endpoint to use.",
        "key": "Accept-API-Version",
        "value": "resource=2.1, protocol=1.0"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"authId\": \"{{authId}}\",\n    \"callbacks\": [\n        {\n            \"type\": \"NameCallback\",\n            \"output\": [\n                {\n                    \"name\": \"prompt\",\n                    \"value\": \"User Name\"\n                }\n            ],\n            \"input\": [\n                {\n                    \"name\": \"IDToken1\",\n                    \"value\": \"{{IDCloudAdminUsername}}\"\n                }\n            ],\n            \"_id\": 0\n        },\n        {\n            \"type\": \"PasswordCallback\",\n            \"output\": [\n                {\n                    \"name\": \"prompt\",\n                    \"value\": \"Password\"\n                }\n            ],\n            \"input\": [\n                {\n                    \"name\": \"IDToken2\",\n                    \"value\": \"{{IDCloudAdminPassword}}\"\n                }\n            ],\n            \"_id\": 1\n        }\n    ]\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{amUrl}}/json/authenticate",
      "host": [
        "{{amUrl}}"
      ],
      "path": [
        "json",
        "authenticate"
      ]
    },
    "description": "Return the Username / Password callbacks received from the previous step, specifying the credentials of an administrative user.\n"
  },
  "response": [
  ]
}