From 1a16b288193d1a09739a94079c905ca3f4832169 Mon Sep 17 00:00:00 2001 From: Sander Verkuil Date: Thu, 10 Jul 2025 12:39:22 +0200 Subject: [PATCH 1/2] fix(Profile): Fixed the fetching of a workos profile. The `getProfile` method was not properly implemented and tested, wrote a test scenario for that to ensure that it works properly now. --- lib/SSO.php | 33 +++++++++++---------------------- tests/WorkOS/SSOTest.php | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/lib/SSO.php b/lib/SSO.php index 6eed387..37ef14e 100644 --- a/lib/SSO.php +++ b/lib/SSO.php @@ -125,32 +125,21 @@ public function getProfileAndToken($code) * * @return Resource\Profile */ - public function getProfile($accessToken) + public function getProfile() { - $getProfilePath = "sso/profile"; - - $params = [ - "access_token" => $accessToken - ]; - - $method = Client::METHOD_GET; - - $url = "https://api.workos.com/sso/profile"; - - $requestHeaders = ["Authorization: Bearer " . $accessToken]; - - list($result) = Client::requestClient()->request( - $method, - $url, - $requestHeaders, - null + $response = Client::request( + Client::METHOD_GET, + 'sso/profile', + null, + null, + true ); - $decodedResponse = json_decode($result, true); - - $profile = Resource\Profile::constructFromResponse($decodedResponse); + if (!array_key_exists('profile', $response)) { + throw new Exception\GenericException('The profile was not found in the response.'); + } - return $profile->json(); + return Resource\Profile::constructFromResponse($response['profile']); } /** diff --git a/tests/WorkOS/SSOTest.php b/tests/WorkOS/SSOTest.php index c2d4cda..7e529dd 100644 --- a/tests/WorkOS/SSOTest.php +++ b/tests/WorkOS/SSOTest.php @@ -115,6 +115,27 @@ public function testGetProfileAndTokenReturnsProfileWithExpectedValues() $this->assertEquals($profileFixture, $profileAndToken->profile->toArray()); } + public function testGetProfileReturnsProfileWithExpectedValues() + { + $path = "sso/profile"; + + $result = $this->profileAndTokenResponseFixture(); + + $this->mockRequest( + Client::METHOD_GET, + $path, + null, + null, + true, + $result + ); + + $profile = $this->sso->getProfile(); + $profileFixture = $this->profileFixture(); + + $this->assertEquals($profileFixture, $profile->toArray()); + } + public function testGetConnection() { $connection = "connection_id"; From 8764bce535071761ed69464d8b471f194829a025 Mon Sep 17 00:00:00 2001 From: Sander Verkuil Date: Thu, 10 Jul 2025 14:25:39 +0200 Subject: [PATCH 2/2] Work with the proper response structure --- lib/SSO.php | 11 +++-------- tests/WorkOS/SSOTest.php | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/lib/SSO.php b/lib/SSO.php index 37ef14e..db3649a 100644 --- a/lib/SSO.php +++ b/lib/SSO.php @@ -125,21 +125,16 @@ public function getProfileAndToken($code) * * @return Resource\Profile */ - public function getProfile() + public function getProfile($accessToken) { $response = Client::request( Client::METHOD_GET, 'sso/profile', + ["Authorization: Bearer " . $accessToken], null, - null, - true ); - if (!array_key_exists('profile', $response)) { - throw new Exception\GenericException('The profile was not found in the response.'); - } - - return Resource\Profile::constructFromResponse($response['profile']); + return Resource\Profile::constructFromResponse($response); } /** diff --git a/tests/WorkOS/SSOTest.php b/tests/WorkOS/SSOTest.php index 7e529dd..071d2e5 100644 --- a/tests/WorkOS/SSOTest.php +++ b/tests/WorkOS/SSOTest.php @@ -118,19 +118,20 @@ public function testGetProfileAndTokenReturnsProfileWithExpectedValues() public function testGetProfileReturnsProfileWithExpectedValues() { $path = "sso/profile"; + $token = 'token'; - $result = $this->profileAndTokenResponseFixture(); + $result = $this->profileResponseFixture(); $this->mockRequest( Client::METHOD_GET, $path, + ['Authorization: Bearer ' . $token], null, - null, - true, + false, $result ); - $profile = $this->sso->getProfile(); + $profile = $this->sso->getProfile($token); $profileFixture = $this->profileFixture(); $this->assertEquals($profileFixture, $profile->toArray()); @@ -280,6 +281,31 @@ private function profileFixture() ]; } + private function profileResponseFixture() + { + return json_encode([ + "id" => "prof_hen", + "email" => "hen@papagenos.com", + "first_name" => "hen", + "last_name" => "cha", + "organization_id" => "org_01FG7HGMY2CZZR2FWHTEE94VF0", + "connection_id" => "conn_01EMH8WAK20T42N2NBMNBCYHAG", + "connection_type" => "GoogleOAuth", + "idp_id" => "randomalphanum", + "role" => new RoleResponse("admin"), + "groups" => array("Admins", "Developers"), + "custom_attributes" => array("license" => "professional"), + "raw_attributes" => array( + "email" => "hen@papagenos.com", + "first_name" => "hen", + "last_name" => "cha", + "ipd_id" => "randomalphanum", + "groups" => array("Admins", "Developers"), + "license" => "professional" + ), + ]); + } + private function connectionFixture() { return [