Skip to content

Commit b451b74

Browse files
authored
Merge pull request #466 from zendesk/validate-zendesk-subdomain
2 parents 13f0c1f + 60e0589 commit b451b74

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/Zendesk/API/Utilities/OAuth.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Zendesk\API\Utilities;
44

5+
use InvalidArgumentException;
56
use GuzzleHttp\Client;
67
use GuzzleHttp\Exception\RequestException;
78
use GuzzleHttp\Psr7\Request;
@@ -22,6 +23,7 @@ class OAuth
2223
*/
2324
public static function getAccessToken(Client $client, $subdomain, array $params, $domain = 'zendesk.com')
2425
{
26+
static::validateSubdomain($subdomain);
2527
$authUrl = "https://$subdomain.$domain/oauth/tokens";
2628

2729
// Fetch access_token
@@ -55,6 +57,7 @@ public static function getAccessToken(Client $client, $subdomain, array $params,
5557
*/
5658
public static function getAuthUrl($subdomain, array $options, $domain = 'zendesk.com')
5759
{
60+
static::validateSubdomain($subdomain);
5861
$queryParams = [
5962
'response_type' => 'code',
6063
'client_id' => null,
@@ -71,4 +74,17 @@ public static function getAuthUrl($subdomain, array $options, $domain = 'zendesk
7174

7275
return $oAuthUrl;
7376
}
77+
78+
/**
79+
* Validate subdomain
80+
*
81+
* @param string $subdomain
82+
* @throws InvalidArgumentException
83+
*/
84+
private static function validateSubdomain($subdomain)
85+
{
86+
if (! preg_match('/^[A-Za-z0-9](?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9])?$/', $subdomain)) {
87+
throw new InvalidArgumentException('Invalid Zendesk subdomain.');
88+
}
89+
}
7490
}

0 commit comments

Comments
 (0)