Skip to content

Commit

Permalink
Feature: login with session cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
devdot committed Jun 5, 2024
1 parent 8cb4b81 commit 11cff31
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/CTConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use CTApi\Models\Groups\Person\PersonRequest;
use CTApi\Utils\CTUtil;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Cookie\SetCookie;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\TransferStats;
use Kevinrob\GuzzleCache\CacheMiddleware;
Expand Down Expand Up @@ -133,6 +134,11 @@ public static function authWithLoginToken(string $loginToken): Auth
return AuthRequest::authWithLoginToken($loginToken);
}

public static function authWithSessionCookie(string $cookieString): Auth
{
return AuthRequest::authWithSessionCookie($cookieString);
}

/**
* Auth via undocumented ajax-API. Use <code>authWithLoginToken()</code> instead.
* @param string $userId
Expand All @@ -156,6 +162,12 @@ public static function getSessionCookie(): ?array
return end($cookieData);
}

public static function getSessionCookieString(): ?string
{
$cookieData = self::getSessionCookie();
return $cookieData ? (string) (new SetCookie($cookieData)) : null;
}

/**
* @see CTConfig::authWithLoginToken()
* @deprecated Will be removed in further version. Use <code>CTConfig::authWithLoginToken()</code> instead.
Expand Down
7 changes: 7 additions & 0 deletions src/Models/Common/Auth/AuthRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CTApi\CTLog;


class AuthRequest
{
public static function authWithEmailAndPassword(string $email, string $password): Auth
Expand All @@ -18,6 +19,12 @@ public static function authWithLoginToken(string $loginToken): Auth
return (new AuthRequestBuilder())->authWithLoginToken($loginToken);
}

public static function authWithSessionCookie(string $sessionCookie): Auth
{
CTLog::getLog()->info('AuthRequest: Authenticate CTConfig with Session');
return (new AuthRequestBuilder())->authWithSessionCookie($sessionCookie);
}

public static function authWithUserIdAndLoginToken(string $userId, string $loginToken): bool
{
CTLog::getLog()->info('AuthRequest: Authenticate CTConfig with UserId and Token.');
Expand Down
22 changes: 22 additions & 0 deletions src/Models/Common/Auth/AuthRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ public function authWithLoginToken(string $loginToken): Auth
throw new CTAuthException("Authentication was not successfull.");
}

public function authWithSessionCookie(string $cookieString): Auth
{
$client = CTClient::getClient();

try {
$response = $client->get('/api/whoami', [
'headers' => [
'cookie' => $cookieString,
]
]);
$data = CTResponseUtil::dataAsArray($response);
$person = Person::createModelFromData($data);
} catch (CTRequestException $exception) {
throw new CTAuthException("Authentication was not successfull: " . $exception->getMessage(), $exception->getCode(), $exception);
}

if ($person->getId() != null && $person->getId() != -1 && $person->getId() != "-1") {
return new Auth($person->getId(), false);
}
throw new CTAuthException("Authentication was not successfull.");
}

public function authWithUserIdAndLoginToken(string $userId, string $loginToken): bool
{
$client = CTClient::getClient();
Expand Down

0 comments on commit 11cff31

Please sign in to comment.