Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/Resource/UserIdentityProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace WorkOS\Resource;

/**
* Class UserIdentityProvider.
*/
class UserIdentityProvider extends BaseWorkOSResource
{
public const RESOURCE_TYPE = "user_identity_provider";

public const RESOURCE_ATTRIBUTES = [
"idpId",
"type",
"provider",
];

public const RESPONSE_TO_RESOURCE_KEY = [
"idp_id" => "idpId",
"type" => "type",
"provider" => "provider",
];

public static function constructFromResponse($response)
{
$instance = parent::constructFromResponse($response);

$instance->values["type"] = (string) new UserIdentityProviderType($response["type"]);

return $instance;
}
}
39 changes: 39 additions & 0 deletions lib/Resource/UserIdentityProviderType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace WorkOS\Resource;

/**
* Class UserIdentityProviderType
*
* This class represents the type of user identity provider.
*/
class UserIdentityProviderType
{
public const OAuth = 'OAuth';

private $type;

/**
* Constructor for UserIdentityProviderType.
*
* @param string $type The type of user identity provider.
*/
public function __construct($type)
{
// Map of lowercase API response values to our standardized constants
$typeMap = [
'oauth' => self::OAuth,
// Add future types here in the format: 'lowercase_value' => self::ConstantName
];

$lowercaseType = strtolower($type);

// Use our mapped constant if available, otherwise keep the original value
$this->type = isset($typeMap[$lowercaseType]) ? $typeMap[$lowercaseType] : $type;
}

public function __toString()
{
return $this->type;
}
}
23 changes: 23 additions & 0 deletions lib/UserManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,29 @@ public function getUser($userId)
return Resource\User::constructFromResponse($response);
}

/**
* Get User's identity providers
*
* @param string $userId The unique ID of the user.
*
* @throws Exception\WorkOSException
*
* @return Resource\UserIdentityProvider[]
*/
public function getUserIdentityProviders($userId)
{
$path = "user_management/users/{$userId}/identities";

$response = Client::request(Client::METHOD_GET, $path, null, null, true);

$userIdentityProviders = [];
foreach ($response as $responseData) {
\array_push($userIdentityProviders, Resource\UserIdentityProvider::constructFromResponse($responseData));
}

return $userIdentityProviders;
}

/**
* Get a User by external ID.
*
Expand Down
48 changes: 47 additions & 1 deletion tests/WorkOS/UserManagementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace WorkOS;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use WorkOS\Resource\UserIdentityProviderType;
use PHPUnit\Framework\Attributes\DataProvider;

class UserManagementTest extends TestCase
{
use TestHelper {
setUp as traitSetUp;
}
private $userManagement;

protected function setUp(): void
{
Expand Down Expand Up @@ -1265,6 +1267,50 @@ public function testGetLogoutUrlException()
}
}

public function testGetUserIdentityProviders()
{
$userId = "user_01E4ZCR3C56J083X43JQXF3JK5";
$path = "user_management/users/{$userId}/identities";

$result = $this->userIdentityProvidersResponseFixture();

$this->mockRequest(
Client::METHOD_GET,
$path,
null,
null,
true,
$result
);

$userIdentityProviderFixture = $this->userIdentityProviderFixture();

$response = $this->userManagement->getUserIdentityProviders($userId);

$this->assertCount(1, $response);
$this->assertSame($userIdentityProviderFixture, $response[0]->toArray());
}

private function userIdentityProvidersResponseFixture()
{
return json_encode([
[
"idp_id" => "4F42ABDE-1E44-4B66-824A-5F733C037A6D",
"type" => "OAuth",
"provider" => "MicrosoftOAuth"
]
]);
}

private function userIdentityProviderFixture()
{
return [
"idpId" => "4F42ABDE-1E44-4B66-824A-5F733C037A6D",
"type" => "OAuth",
"provider" => "MicrosoftOAuth"
];
}

//Fixtures

private function invitationResponseFixture()
Expand Down