datatable.directory codebase https://datatable.directory/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

60 lines
1.4 KiB

<?php namespace SocialNorm;
use SocialNorm\Exceptions\InvalidAuthorizationCodeException;
class SocialNorm
{
/** @var ProviderRegistry */
protected $providers;
/** @var Session */
protected $session;
/** @var Request */
protected $request;
/** @var StateGenerator */
protected $stateGenerator;
public function __construct(
ProviderRegistry $providers,
Session $session,
Request $request,
StateGenerator $stateGenerator
)
{
$this->providers = $providers;
$this->session = $session;
$this->request = $request;
$this->stateGenerator = $stateGenerator;
}
public function registerProvider(string $alias, Provider $provider)
{
$this->providers->registerProvider($alias, $provider);
}
public function authorize(string $providerAlias)
{
$state = $this->stateGenerator->generate();
$this->session->put('oauth.state', $state);
return $this->getProvider($providerAlias)->authorizeUrl($state);
}
public function getUser(string $providerAlias)
{
$this->verifyState();
return $this->getProvider($providerAlias)->getUser();
}
protected function getProvider(string $providerAlias)
{
return $this->providers->getProvider($providerAlias);
}
protected function verifyState()
{
if ($this->session->get('oauth.state') !== $this->request->state()) {
throw new InvalidAuthorizationCodeException("State failed to verify");
}
}
}