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

Validate the provided Zendesk subdomain #466

Merged
merged 3 commits into from
Apr 28, 2021
Merged
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
16 changes: 16 additions & 0 deletions src/Zendesk/API/Utilities/OAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Zendesk\API\Utilities;

use InvalidArgumentException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
Expand All @@ -22,6 +23,7 @@ class OAuth
*/
public static function getAccessToken(Client $client, $subdomain, array $params, $domain = 'zendesk.com')
{
static::validateSubdomain($subdomain);
$authUrl = "https://$subdomain.$domain/oauth/tokens";

// Fetch access_token
Expand Down Expand Up @@ -55,6 +57,7 @@ public static function getAccessToken(Client $client, $subdomain, array $params,
*/
public static function getAuthUrl($subdomain, array $options, $domain = 'zendesk.com')
{
static::validateSubdomain($subdomain);
$queryParams = [
'response_type' => 'code',
'client_id' => null,
Expand All @@ -71,4 +74,17 @@ public static function getAuthUrl($subdomain, array $options, $domain = 'zendesk

return $oAuthUrl;
}

/**
* Validate subdomain
*
* @param string $subdomain
* @throws InvalidArgumentException
*/
private static function validateSubdomain($subdomain)
{
if (! preg_match('/^[A-Za-z0-9](?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9])?$/', $subdomain)) {
throw new InvalidArgumentException('Invalid Zendesk subdomain.');
}
}
}