Skip to content

Commit

Permalink
Merge pull request #466 from zendesk/validate-zendesk-subdomain
Browse files Browse the repository at this point in the history
  • Loading branch information
thekindofme authored Apr 28, 2021
2 parents 13f0c1f + 60e0589 commit b451b74
Showing 1 changed file with 16 additions and 0 deletions.
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.');
}
}
}

0 comments on commit b451b74

Please sign in to comment.