Skip to content

Commit

Permalink
Add widgets get token API support. (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgd authored Nov 15, 2024
1 parent bb05c6a commit 32cc0a1
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/Resource/WidgetScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace WorkOS\Resource;

/**
* Class WidgetScope
*
* Widget scopes supported through the WorkOS Widgets API.
*/
class WidgetScope
{
public const UsersTableManage = "widgets:users-table:manage";
}
21 changes: 21 additions & 0 deletions lib/Resource/WidgetTokenResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace WorkOS\Resource;

/**
* Class WidgetTokenResponse.
*
* @property string $token
*/
class WidgetTokenResponse extends BaseWorkOSResource
{
public const RESOURCE_TYPE = "widget_token_response";

public const RESOURCE_ATTRIBUTES = [
"token"
];

public const RESPONSE_TO_RESOURCE_KEY = [
"token" => "token"
];
}
36 changes: 36 additions & 0 deletions lib/Widgets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace WorkOS;

/**
* Class Widgets.
*
* This class facilitates the use of the WorkOS Widgets API.
*/
class Widgets
{
/**
* Generate a widget token scoped to an organization and user.
*
* @param string $organization_id An Organization identifier.
* @param string $user_id An AuthKit user identifier.
* @param WidgetScope[] $scopes The scopes to mint the widget token with. Possible values are ["widgets:users-table:manage"].
*
* @throws Exception\WorkOSException
*
* @return \WorkOS\Resource\WidgetTokenResponse
*/
public function getToken($organization_id, $user_id, $scopes)
{
$getTokenPath = "widgets/token";
$params = [
"organization_id" => $organization_id,
"user_id" => $user_id,
"scopes" => $scopes,
];

$response = Client::request(Client::METHOD_POST, $getTokenPath, null, $params, true);

return Resource\WidgetTokenResponse::constructFromResponse($response);
}
}
54 changes: 54 additions & 0 deletions tests/WorkOS/WidgetsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace WorkOS;

class WidgetsTest extends \PHPUnit\Framework\TestCase
{
use TestHelper {
setUp as protected traitSetUp;
}

protected function setUp(): void
{
$this->traitSetUp();

$this->withApiKey();
$this->ap = new Widgets();
}

public function testGenerateLinkSSO()
{
$getTokenPath = "widgets/token";

$result = $this->generateWidgetTokenFixture();

$params = [
"organization_id" => "org_01EHZNVPK3SFK441A1RGBFSHRT",
"user_id" => "user_01EHZNVPK3SFK441A1RGBFSHRT",
"scopes" => ["widgets:users-table:manage"]
];

$this->mockRequest(
Client::METHOD_POST,
$getTokenPath,
null,
$params,
true,
$result
);

$expectation = "abc123456";

$response = $this->ap->getToken("org_01EHZNVPK3SFK441A1RGBFSHRT", "user_01EHZNVPK3SFK441A1RGBFSHRT", ["widgets:users-table:manage"]);
$this->assertSame($expectation, $response->token);
}

// Fixtures

private function generateWidgetTokenFixture()
{
return json_encode([
"token" => "abc123456"
]);
}
}

0 comments on commit 32cc0a1

Please sign in to comment.