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 code challenge #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ public function upgradeStorage($storageId)
if ($oldStorage->hasAuthorizationState($service)) {
$newStorage->storeAuthorizationState($service, $oldStorage->retrieveAuthorizationState($service));
}
if ($oldStorage->hasCodeVerifier($service)) {
$newStorage->storeCodeVerifier($service, $oldStorage->retrieveCodeVerifier($service));
}

// fixme invalidate current oauth object? reinitialize it?
}
Expand Down
47 changes: 47 additions & 0 deletions Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,51 @@ public function clearAllAuthorizationStates()

return $this;
}

/** @inheritDoc */
public function storeCodeVerifier($service, $verifier)
{
$data = $this->loadServiceFile($service);
$data['verifier'] = $verifier;
$this->saveServiceFile($service, $data);
return $this;
}

/** @inheritDoc */
public function hasCodeVerifier($service)
{
$data = $this->loadServiceFile($service);
return isset($data['verifier']);
}

/**
* @inheritDoc
* @throws TokenNotFoundException
*/
public function retrieveCodeVerifier($service)
{
$data = $this->loadServiceFile($service);
if (!isset($data['verifier'])) {
throw new TokenNotFoundException('No code verifier found in storage');
}
return $data['verifier'];
}

/** @inheritDoc */
public function clearCodeVerifier($service)
{
$data = $this->loadServiceFile($service);
if (isset($data['verifier'])) unset($data['verifier']);
$this->saveServiceFile($service, $data);

return $this;
}

/** @inheritDoc */
public function clearAllCodeVerifiers()
{
// TODO: Implement clearAllCodeVerifiers() method.

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace OAuth\Common\Storage\Exception;

/**
* Exception thrown when a code verifier is not found in storage.
*/
class CodeVerifierNotFoundException extends StorageException
{
}
62 changes: 62 additions & 0 deletions vendor/lusitanian/oauth/src/OAuth/Common/Storage/Memory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use OAuth\Common\Token\TokenInterface;
use OAuth\Common\Storage\Exception\TokenNotFoundException;
use OAuth\Common\Storage\Exception\AuthorizationStateNotFoundException;
use OAuth\Common\Storage\Exception\CodeVerifierNotFoundException;

/*
* Stores a token in-memory only (destroyed at end of script execution).
Expand All @@ -21,10 +22,16 @@ class Memory implements TokenStorageInterface
*/
protected $states;

/**
* @var array
*/
protected $verifiers;

public function __construct()
{
$this->tokens = array();
$this->states = array();
$this->verifiers = array();
}

/**
Expand Down Expand Up @@ -136,4 +143,59 @@ public function clearAllAuthorizationStates()
// allow chaining
return $this;
}

/**
* {@inheritDoc}
*/
public function retrieveCodeVerifier($service)
{
if ($this->hasCodeVerifier($service)) {
return $this->verifiers[$service];
}

throw new CodeVerifierNotFoundException('code verifier not stored');
}

/**
* {@inheritDoc}
*/
public function storeCodeVerifier($service, $verifier)
{
$this->verifiers[$service] = $verifier;

// allow chaining
return $this;
}

/**
* {@inheritDoc}
*/
public function hasCodeVerifier($service)
{
return isset($this->verifiers[$service]) && null !== $this->verifiers[$service];
}

/**
* {@inheritDoc}
*/
public function clearCodeVerifier($service)
{
if (array_key_exists($service, $this->verifiers)) {
unset($this->verifiers[$service]);
}

// allow chaining
return $this;
}

/**
* {@inheritDoc}
*/
public function clearAllCodeVerifiers()
{
$this->verifiers = array();

// allow chaining
return $this;
}
}
98 changes: 97 additions & 1 deletion vendor/lusitanian/oauth/src/OAuth/Common/Storage/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use OAuth\Common\Token\TokenInterface;
use OAuth\Common\Storage\Exception\TokenNotFoundException;
use OAuth\Common\Storage\Exception\AuthorizationStateNotFoundException;
use OAuth\Common\Storage\Exception\CodeVerifierNotFoundException;
use Predis\Client as Predis;

/*
Expand All @@ -19,6 +20,11 @@ class Redis implements TokenStorageInterface

protected $stateKey;

/**
* @var string
*/
protected $verifierKey;

/**
* @var object|\Redis
*/
Expand All @@ -34,18 +40,26 @@ class Redis implements TokenStorageInterface
*/
protected $cachedStates;

/**
* @var object
*/
protected $cachedVerifiers;

/**
* @param Predis $redis An instantiated and connected redis client
* @param string $key The key to store the token under in redis
* @param string $stateKey The key to store the state under in redis.
* @param string $verifierKey The key to store the verifier under in redis.
*/
public function __construct(Predis $redis, $key, $stateKey)
public function __construct(Predis $redis, $key, $stateKey, $verifierKey)
{
$this->redis = $redis;
$this->key = $key;
$this->stateKey = $stateKey;
$this->verifierKey = $verifierKey;
$this->cachedTokens = array();
$this->cachedStates = array();
$this->cachedVerifiers = array();
}

/**
Expand Down Expand Up @@ -212,6 +226,88 @@ function ($pipe) use ($keys, $me) {
return $this;
}

/**
* {@inheritDoc}
*/
public function retrieveCodeVerifier($service)
{
if (!$this->hasCodeVerifier($service)) {
throw new CodeVerifierNotFoundException('CodeVerifier not found in redis');
}

if (isset($this->cachedVerifiers[$service])) {
return $this->cachedVerifiers[$service];
}

$val = $this->redis->hget($this->verifierKey, $service);

return $this->cachedVerifiers[$service] = $val;
}

/**
* {@inheritDoc}
*/
public function storeCodeVerifier($service, $verifier)
{
// (over)write the token
$this->redis->hset($this->verifierKey, $service, $verifier);
$this->cachedVerifiers[$service] = $verifier;

// allow chaining
return $this;
}

/**
* {@inheritDoc}
*/
public function hasCodeVerifier($service)
{
if (isset($this->cachedVerifiers[$service])
&& null !== $this->cachedVerifiers[$service]
) {
return true;
}

return $this->redis->hexists($this->verifierKey, $service);
}

/**
* {@inheritDoc}
*/
public function clearCodeVerifier($service)
{
$this->redis->hdel($this->verifierKey, $service);
unset($this->cachedVerifiers[$service]);

// allow chaining
return $this;
}

/**
* {@inheritDoc}
*/
public function clearAllCodeVerifiers()
{
// memory
$this->cachedVerifiers = array();

// redis
$keys = $this->redis->hkeys($this->verifierKey);
$me = $this; // 5.3 compat

// pipeline for performance
$this->redis->pipeline(
function ($pipe) use ($keys, $me) {
foreach ($keys as $k) {
$pipe->hdel($me->getKey(), $k);
}
}
);

// allow chaining
return $this;
}

/**
* @return Predis $redis
*/
Expand Down
Loading