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 support for SSO errors coming from the API #913

Merged
merged 7 commits into from
Aug 15, 2020
Merged
Show file tree
Hide file tree
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
32 changes: 32 additions & 0 deletions lib/Github/Exception/SsoRequiredException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Github\Exception;

/**
* SsoRequiredException.
*/
class SsoRequiredException extends RuntimeException
{
/** @var string */
private $url;

/**
* @param string $url
* @param int $code
* @param \Throwable|null $previous
*/
public function __construct($url, $code = 0, $previous = null)
{
$this->url = $url;

parent::__construct('Resource protected by organization SAML enforcement. You must grant your personal token access to this organization.', $code, $previous);
}

/**
* @return string
*/
public function getUrl()
{
return $this->url;
}
}
11 changes: 11 additions & 0 deletions lib/Github/HttpClient/Plugin/GithubExceptionThrower.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Github\Exception\ApiLimitExceedException;
use Github\Exception\ErrorException;
use Github\Exception\RuntimeException;
use Github\Exception\SsoRequiredException;
use Github\Exception\TwoFactorAuthenticationRequiredException;
use Github\Exception\ValidationFailedException;
use Github\HttpClient\Message\ResponseMediator;
Expand Down Expand Up @@ -103,6 +104,16 @@ public function doHandleRequest(RequestInterface $request, callable $next, calla
throw new RuntimeException(implode(', ', $errors), 502);
}

if ((403 === $response->getStatusCode()) && $response->hasHeader('X-GitHub-SSO') && 0 === strpos((string) ResponseMediator::getHeader($response, 'X-GitHub-SSO'), 'required;')) {
// The header will look something like this:
// required; url=https://github.com/orgs/octodocs-test/sso?authorization_request=AZSCKtL4U8yX1H3sCQIVnVgmjmon5fWxks5YrqhJgah0b2tlbl9pZM4EuMz4
// So we strip out the first 14 characters, leaving only the URL.
// @see https://developer.github.com/v3/auth/#authenticating-for-saml-sso
$url = substr((string) ResponseMediator::getHeader($response, 'X-GitHub-SSO'), 14);

throw new SsoRequiredException($url);
}

throw new RuntimeException(isset($content['message']) ? $content['message'] : $content, $response->getStatusCode());
});
}
Expand Down
10 changes: 10 additions & 0 deletions test/Github/Tests/HttpClient/Plugin/GithubExceptionThrowerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ public static function responseProvider()
),
'exception' => new \Github\Exception\RuntimeException('Something went wrong with executing your query', 502),
],
'Sso required Response' => [
'response' => new Response(
403,
[
'Content-Type' => 'application/json',
'X-GitHub-SSO' => 'required; url=https://github.com/orgs/octodocs-test/sso?authorization_request=AZSCKtL4U8yX1H3sCQIVnVgmjmon5fWxks5YrqhJgah0b2tlbl9pZM4EuMz4',
]
),
'exception' => new \Github\Exception\SsoRequiredException('https://github.com/orgs/octodocs-test/sso?authorization_request=AZSCKtL4U8yX1H3sCQIVnVgmjmon5fWxks5YrqhJgah0b2tlbl9pZM4EuMz4'),
],
'Default handling' => [
'response' => new Response(
555,
Expand Down