Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add public method to get authorization bearer token #14784

Merged
merged 5 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
56 changes: 26 additions & 30 deletions src/controllers/GraphqlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,37 +247,14 @@ private function _schema(GqlService $gqlService): GqlSchema
return $schema;
}

// Was a specific token passed?
$authHeaders = $requestHeaders->get('X-Craft-Authorization', null, false) ?? $requestHeaders->get('Authorization', null, false) ?? [];
foreach ($authHeaders as $authHeader) {
$authValues = array_map('trim', explode(',', $authHeader));
foreach ($authValues as $authValue) {
if (preg_match('/^Bearer\s+(.+)$/i', $authValue, $matches)) {
try {
$token = $gqlService->getTokenByAccessToken($matches[1]);
} catch (InvalidArgumentException) {
}

if (!isset($token) || !$token->getIsValid()) {
throw new BadRequestHttpException('Invalid Authorization header');
}
$token = $this->_token($gqlService);

break 2;
}
}
}

if (!isset($token)) {
// Get the public schema, if it exists & is valid
$token = $this->_publicToken($gqlService);

// If we couldn't find a token, then return the active schema if there is one, otherwise bail
if (!$token) {
try {
return $gqlService->getActiveSchema();
} catch (GqlException) {
throw new BadRequestHttpException('Missing Authorization header');
}
// If we couldn't find a token, then return the active schema if there is one, otherwise bail
if (!$token) {
try {
return $gqlService->getActiveSchema();
} catch (GqlException) {
throw new BadRequestHttpException('Missing Authorization header');
}
}

Expand All @@ -288,6 +265,25 @@ private function _schema(GqlService $gqlService): GqlSchema
return $token->getSchema();
}

private function _token(GqlService $gqlService): ?GqlToken
{
$bearerToken = $this->request->getBearerToken();

if ($bearerToken) {
try {
$token = $gqlService->getTokenByAccessToken($bearerToken);

if ($token->getIsValid()) {
return $token;
}
brandonkelly marked this conversation as resolved.
Show resolved Hide resolved
} catch (InvalidArgumentException) {
}
}

// Get the public schema, if it exists & is valid
return $this->_publicToken($gqlService);
}

/**
* Returns the public token, if it exists and is valid.
*
Expand Down
17 changes: 17 additions & 0 deletions src/web/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,23 @@ public function getRawCookies(): CookieCollection
return $this->_rawCookies;
}

public function getBearerToken(): ?string
{
$authHeaders = $this->getHeaders()->get('X-Craft-Authorization', null, false) ?? $this->getHeaders()->get('Authorization', null, false) ?? [];

foreach ($authHeaders as $authHeader) {
$authValues = array_map('trim', explode(',', $authHeader));

foreach ($authValues as $authValue) {
if (preg_match('/^Bearer\s+(.+)$/i', $authValue, $matches)) {
return $matches[1];
}
}
}

return null;
}

/**
* Converts any invalid cookies in `$_COOKIE` into an array of [[Cookie]] objects.
*
Expand Down
Loading