Chilkat Online Tools

SQL Server / ForgeRock Identity Cloud Collection / Step 3: Grant Consent Using the User Code

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

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

    EXEC sp_OAMethod @req, 'AddParam', NULL, 'decision', 'allow'

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

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

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

    EXEC sp_OAMethod @jsonParam3, 'Emit', @sTmp0 OUT
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'user_code', @sTmp0

    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Authorization', 'Bearer <access_token>'
    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Cookie', '{{demoSSOToken}}'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostUrlEncoded', @resp OUT, 'https://<tenant-name>.forgeblocks.com/am/oauth2/realms/root/realms/alpha/device/user', @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 @jsonParam2
        EXEC @hr = sp_OADestroy @jsonParam3
        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 @req
    EXEC @hr = sp_OADestroy @jsonParam2
    EXEC @hr = sp_OADestroy @jsonParam3


END
GO

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Cookie: {{demoSSOToken}}"
	--data-urlencode 'decision=allow'
	--data-urlencode 'csrf={{demoSSOToken}}'
	--data-urlencode 'user_code={{user_code}}'
https://<tenant-name>.forgeblocks.com/am/oauth2/realms/root/realms/alpha/device/user

Postman Collection Item JSON

{
  "name": "Step 3: Grant Consent Using the User Code",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "pm.test(\"Status code is 200\", () => {",
          "  pm.expect(pm.response.code).to.eql(200);",
          "});",
          "",
          "pm.test(\"Response contains 'done: true'.\",() => {",
          "  pm.expect(pm.response.text()).to.include(\"done: true\");",
          "});"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Cookie",
        "value": "{{demoSSOToken}}",
        "description": "SSO token to protect against cross-site request forgery.",
        "type": "text"
      }
    ],
    "body": {
      "mode": "urlencoded",
      "urlencoded": [
        {
          "key": "decision",
          "value": "allow",
          "description": "Decision that grants access to the scopes in the request (to the client).",
          "type": "text"
        },
        {
          "key": "csrf",
          "value": "{{demoSSOToken}}",
          "description": "SSO token to protect against cross-site request forgery.",
          "type": "text"
        },
        {
          "key": "user_code",
          "value": "{{user_code}}",
          "description": "The user code for the device.",
          "type": "text"
        }
      ]
    },
    "url": {
      "raw": "{{amUrl}}/oauth2{{realm}}/device/user",
      "host": [
        "{{amUrl}}"
      ],
      "path": [
        "oauth2{{realm}}",
        "device",
        "user"
      ]
    },
    "description": "Grants consent using the user code recieved in the previous call to allow the client device to access resources."
  },
  "response": [
    {
      "name": "Example - page contains \"done: true\"",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Cookie",
            "value": "{{demoSSOToken}}",
            "description": "SSO token to protect against cross-site request forgery.",
            "type": "text"
          }
        ],
        "body": {
          "mode": "urlencoded",
          "urlencoded": [
            {
              "key": "decision",
              "value": "allow",
              "description": "Decision that grants access to the scopes in the request (to the client).",
              "type": "text"
            },
            {
              "key": "csrf",
              "value": "{{demoSSOToken}}",
              "description": "SSO token to protect against cross-site request forgery.",
              "type": "text"
            },
            {
              "key": "user_code",
              "value": "{{user_code}}",
              "description": "The user code for the device.",
              "type": "text"
            }
          ]
        },
        "url": {
          "raw": "{{amUrl}}/oauth2{{realm}}/device/user",
          "host": [
            "{{amUrl}}"
          ],
          "path": [
            "oauth2{{realm}}",
            "device",
            "user"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "html",
      "header": [
        {
          "key": "X-Frame-Options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "Content-Type",
          "value": "text/html;charset=UTF-8"
        },
        {
          "key": "Content-Length",
          "value": "969"
        },
        {
          "key": "Date",
          "value": "Thu, 13 Aug 2020 12:19:21 GMT"
        }
      ],
      "cookie": [
      ],
      "body": "<!DOCTYPE html>\n<!--\n  Copyright 2015-2018 ForgeRock AS. All Rights Reserved\n \n  Use of this code requires a commercial software license with ForgeRock AS.\n  or with one of its affiliates. All use shall be exclusively subject\n  to such license between the licensee and ForgeRock AS.\n-->\n<html lang=\"en\">\n    <head>\n        <meta charset=\"utf-8\">\n        <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n        <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n        <meta name=\"description\" content=\"OAuth2 Authorization\">\n        <title>OAuth2 Authorization Server</title>\n    </head>\n    <body style=\"display:none\">\n        <div id=\"wrapper\">Loading...</div>\n        <footer id=\"footer\" class=\"footer\"></footer>\n        <script type=\"text/javascript\">\n  pageData = {\n      locale: \"en_GB\",\n      baseUrl : \"http://openam.example.com:8080/openam/XUI/\",\n      realm : \"\\//XUI\",\n      done: true\n  };\n</script>\n        <script src=\"http://openam.example.com:8080/openam/XUI/main-device.js\"></script>\n    </body>\n</html>"
    }
  ]
}