private function getUserId(array $inputData)
 {
     if (f\not($inputData, 'user_id')) {
         throw new UserNotFoundException();
     }
     return f\get($inputData, 'user_id');
 }
 /**
  * @return TokenGrantTypeProcessorInterface
  */
 private function findProcessor($grantType)
 {
     if (f\contains($this->processors, $grantType)) {
         return f\get($this->processors, $grantType);
     }
     throw new UnsupportedGrantTypeOAuthErrorException();
 }
 private function findUser($username)
 {
     $isUser = function ($user) use($username) {
         return f\get($user, 'username') === $username;
     };
     return f\find($isUser, $this->users);
 }
 public function process(Client $client, array $inputData)
 {
     $userId = f\get($client, 'id');
     $scopes = $this->scopesObtainer->getScopes($inputData);
     $context = new Context($client, $userId, $scopes);
     return $this->tokenCreator->create($context);
 }
Exemplo n.º 5
0
 private function getToken($data)
 {
     $token = f\get($data, 'token');
     if (f\not(is_string($token))) {
         throw new \InvalidArgumentException('Token must be a strong.');
     }
     return $token;
 }
 public function testFind()
 {
     $accessToken = $this->createAccessToken();
     $key = f\get($accessToken, 'token');
     $value = $accessToken->getParams();
     $this->cache->shouldReceive('fetch')->once()->with($key)->andReturn($value);
     $this->assertEquals($accessToken, $this->repository->find(f\get($accessToken, 'token')));
 }
 public function testFindAllShouldReturnAllClients()
 {
     $newClient1 = new Client('foo');
     $newClient2 = new Client('bar');
     $addedClient1 = $this->repository->add($newClient1);
     $addedClient2 = $this->repository->add($newClient2);
     $this->assertEquals([f\get($addedClient1, 'id') => $addedClient1, f\get($addedClient2, 'id') => $addedClient2], $this->repository->findAll());
 }
Exemplo n.º 8
0
 public function testCreate()
 {
     $context = $this->createContextMock();
     $accessToken = $this->createAccessToken();
     $this->accessTokenCreator->shouldReceive('create')->once()->with($context)->andReturn($accessToken);
     $response = $this->tokenCreator->create($context);
     $this->assertSame(['access_token' => f\get($accessToken, 'token'), 'token_type' => f\get($accessToken, 'type'), 'expires_in' => f\get($accessToken, 'lifetime'), 'scope' => f\get($accessToken, 'scope')], $response);
 }
Exemplo n.º 9
0
/**
 * f\get_in($coll, $in)
 *
 * Returns a element of a collection in a nested structure in.
 * An InvalidArgumentException is thrown if the in does not exist.
 *
 * f\get_in(array('a' => array('a1' => 'foo'), array('a', 'a1');
 * => 'foo'
 */
function get_in($coll, $in)
{
    $arrayIn = f\_coll_in($coll, $in);
    if ($arrayIn === false) {
        return $default;
    }
    return f\get($arrayIn, f\last($in));
}
 /**
  * @expectedException \Akamon\OAuth2\Server\Domain\Exception\OAuthError\InvalidRefreshTokenOAuthErrorException
  */
 public function testItThrowsAnExceptionIfThereAccessTokenDoesNotExist()
 {
     $refreshToken = $this->createRefreshToken();
     $this->refreshTokenRepository->shouldReceive('find')->with(f\get($refreshToken, 'token'))->once()->andReturn($refreshToken)->ordered();
     $this->accessTokenRepository->shouldReceive('find')->with(f\get($refreshToken, 'accessTokenToken'))->andReturnNull()->ordered();
     $this->refreshTokenRepository->shouldReceive('remove')->with($refreshToken)->once()->ordered();
     $this->processor->process($this->createClient(), ['refresh_token' => f\get($refreshToken, 'token')]);
 }
 public function resolve(Context $context)
 {
     if (f\not($context->getScopes()->isEmpty())) {
         return $context;
     }
     $defaultScopes = ScopeCollection::createFromString(f\get($context->getClient(), 'defaultScope'));
     return new Context($context->getClient(), $context->getUserId(), $defaultScopes);
 }
 private function getUserCredentialsFromInputData($inputData)
 {
     if ($this->hasNoUserCredentialsInInputData($inputData)) {
         throw new UserCredentialsNotFoundException();
     }
     $username = f\get($inputData, 'username');
     $password = f\get($inputData, 'password');
     return new UserCredentials($username, $password);
 }
 public function testRemoveShouldRemoveAToken()
 {
     $token1 = $this->createAccessToken();
     $token2 = $this->createAccessToken();
     $this->repository->add($token1);
     $this->repository->add($token2);
     $this->repository->remove($token2);
     $this->assertEquals($token1, $this->repository->find(f\get($token1->getParams(), 'token')));
     $this->assertNull($this->repository->find(f\get($token2->getParams(), 'token')));
 }
 public function testRemoveShouldRemoveAToken($value = '')
 {
     $token1 = $this->createRefreshToken();
     $token2 = $this->createRefreshToken();
     $this->repository->add($token1);
     $this->repository->add($token2);
     $this->repository->remove($token2);
     $this->assertEquals($token1, $this->repository->find(f\get($token1, 'token')));
     $this->assertNull($this->repository->find(f\get($token2, 'token')));
 }
 public function testOk()
 {
     $refreshToken = $this->createRefreshToken();
     $params = ['access_token' => 'foo', 'token_type' => 'bar', 'lifetime' => 20];
     $context = $this->createContextMock();
     $this->delegate->shouldReceive('create')->with($context)->once()->andReturn($params)->ordered();
     $this->refreshTokenCreator->shouldReceive('create')->with($params['access_token'])->once()->andReturn($refreshToken);
     $expected = array_merge($params, ['refresh_token' => f\get($refreshToken, 'token')]);
     $this->assertSame($expected, $this->creator->create($context));
 }
Exemplo n.º 16
0
 private function findUser($username)
 {
     $getUser = function ($user) use($username) {
         return f\get($user, 'username') === $username;
     };
     $user = f\find($getUser, $this->users);
     if (f\not($user)) {
         throw new UserNotFoundException();
     }
     return $user;
 }
Exemplo n.º 17
0
 public function testCreateShouldCreateAnAccessToken()
 {
     $token = sha1('foo');
     $this->tokenGenerator->shouldReceive('generate')->once()->andReturn($token);
     $client = new Client(['id' => 'ups', 'name' => 'pablodip']);
     $userId = 'bar';
     $scopes = new ScopeCollection([new Scope('foo'), new Scope('bar')]);
     $context = new Context($client, $userId, $scopes);
     $accessToken = $this->creator->create($context);
     $this->assertInstanceOf('Akamon\\OAuth2\\Server\\Domain\\Model\\AccessToken\\AccessToken', $accessToken);
     $this->assertSame(array('token' => $token, 'type' => 'bearer', 'clientId' => f\get($client, 'id'), 'userId' => $userId, 'createdAt' => time(), 'lifetime' => $this->lifetime, 'scope' => 'foo bar'), $accessToken->getParams());
 }
 public function testShouldCheckUniqueness()
 {
     $accessTokenToken = 'ups';
     $refreshToken1 = $this->createRefreshToken();
     $refreshToken2 = $this->createRefreshToken();
     $this->delegate->shouldReceive('create')->with($accessTokenToken)->once()->andReturn($refreshToken1)->globally()->ordered();
     $this->repository->shouldReceive('find')->with(f\get($refreshToken1, 'token'))->once()->andReturn($refreshToken1)->globally()->ordered();
     $this->delegate->shouldReceive('create')->with($accessTokenToken)->once()->andReturn($refreshToken2)->globally()->ordered();
     $this->repository->shouldReceive('find')->with(f\get($refreshToken2, 'token'))->once()->andReturnNull()->globally()->ordered();
     $this->repository->shouldReceive('add')->once()->with($refreshToken2)->globally()->ordered();
     $this->assertSame($refreshToken2, $this->creator->create($accessTokenToken));
 }
 public function testCreateShouldCheckUniqueness()
 {
     $context = $this->createContextMock();
     $accessToken1 = $this->createAccessToken();
     $accessToken2 = $this->createAccessToken();
     $this->creator->shouldReceive('create')->with($context)->once()->andReturn($accessToken1)->globally()->ordered();
     $this->repository->shouldReceive('find')->with(f\get($accessToken1, 'token'))->once()->andReturn($accessToken1)->globally()->ordered();
     $this->creator->shouldReceive('create')->with($context)->once()->andReturn($accessToken2)->globally()->ordered();
     $this->repository->shouldReceive('find')->with(f\get($accessToken2, 'token'))->once()->andReturnNull()->globally()->ordered();
     $this->repository->shouldReceive('add')->with($accessToken2)->once();
     $this->assertSame($accessToken2, $this->persistentCreator->create($context));
 }
 public function testOk()
 {
     $client = $this->createClient();
     $userId = f\get($client, 'id');
     $scope = 'foo';
     $scopes = new ScopeCollection([new Scope($scope)]);
     $context = new Context($client, $userId, $scopes);
     $inputData = ['scope' => $scope];
     $returnData = new \stdClass();
     $this->scopesObtainer->shouldReceive('getScopes')->with($inputData)->once()->andReturn($scopes);
     $this->tokenCreator->shouldReceive('create')->with(\Mockery::mustBe($context))->once()->andReturn($returnData);
     $this->assertSame($returnData, $this->processor->process($client, $inputData));
 }
Exemplo n.º 21
0
function _coll_depth($array, $depth)
{
    $first = f\first($depth);
    if (f\contains($array, $first)) {
        $arrayIn = f\to_array(f\get($array, $first));
        $inRest = f\rest($depth);
        if (count($inRest)) {
            return f\_coll_depth($arrayIn, $inRest);
        }
        return $arrayIn;
    }
    return false;
}
Exemplo n.º 22
0
/**
 * f\normalize_coll($coll, $normalizers)
 *
 * Returns a new collection normalizing the elements indicating in normalizers.
 * It accepts param rules as normalizers.
 * It's similar to map_indexed.
 *
 * f\normalize_coll(array('a' => 1.0), array('a' => 'intval'));
 * => array('a' => 1)
 *
 * // elements without normalizers are returned without modification
 * f\normalize_coll(array('a' => 1.0, 'b' => 2.0), array('a' => 'intval'));
 * => array('a' => 1, 'b' => 2.0)
 *
 * // with param rules
 * f\normalize_coll(array('a' => 1.0), array('a' => f\required('normalizer' => 'intval')));
 * => array('a' => 1)
 */
function normalize_coll($collection, $normalizers)
{
    $result = f\to_array($collection);
    foreach ($normalizers as $name => $normalizer) {
        if ($normalizer instanceof param_rule) {
            $normalizer = $normalizer->getNormalizer();
            if (f\not($normalizer)) {
                continue;
            }
        }
        $result[$name] = call_user_func($normalizer, f\get($collection, $name));
    }
    return $result;
}
Exemplo n.º 23
0
/**
 * f\fill($coll, $paramRules)
 *
 * Returns a new collection filled with param rules.
 * If a param is optional and it does not exist and there is a default value, it's filled.
 * If a param exists and there is no param rule for that param, it's not filled.
 *
 * // filling with empty coll
 * f\fill(array(), array('a' => f\optional(array('d' => 1)));
 * => array('a' => 1)
 *
 * // filling with existing coll
 * f\fill(array('a' => 1), array('a' => f\required(), 'b' => f\optional(array('d' => 2)));
 * => array('a' => 1, 'b' => 2)
 *
 * // without param rule
 * f\fill(array('a' => 1, 'b' => 2), array('a' => f\required()));
 * => array()
 */
function fill($coll, $paramRules)
{
    $fill = function ($rule, $key) use($coll) {
        if (f\not($rule instanceof param_rule)) {
            throw new \InvalidArgumentException('Fill rules must be created with felpado\\required and felpado\\optional.');
        }
        if ($rule instanceof optional) {
            $value = f\get_or($coll, $key, $rule->getDefaultValue());
        } else {
            $value = f\get($coll, $key);
        }
        return $value;
    };
    return f\map_indexed($fill, $paramRules);
}
 private function getContext(Client $client, array $inputData)
 {
     if (f\not(f\contains($inputData, 'refresh_token'))) {
         throw new RefreshTokenNotFoundOAuthErrorException();
     }
     $refreshToken = $this->refreshTokenRepository->find(f\get($inputData, 'refresh_token'));
     if (f\not($refreshToken)) {
         throw new InvalidRefreshTokenOAuthErrorException();
     }
     if ($refreshToken->isExpired()) {
         throw new ExpiredRefreshTokenOAuthErrorException();
     }
     $accessToken = $this->accessTokenRepository->find(f\get($refreshToken, 'accessTokenToken'));
     if (f\not($accessToken)) {
         $this->refreshTokenRepository->remove($refreshToken);
         throw new InvalidRefreshTokenOAuthErrorException();
     }
     $this->refreshTokenRepository->remove($refreshToken);
     $this->accessTokenRepository->remove($accessToken);
     return new Context($client, f\get($accessToken, 'userId'), ScopeCollection::createFromString(f\get($accessToken, 'scope')));
 }
Exemplo n.º 25
0
/**
 * f\validate_coll($coll, $paramRules)
 *
 * Returns the validation errors when validating coll with param rules.
 *
 * // validating existence
 * f\validate_coll(array(), array('a' => f\required(), 'b' => f\optional()));
 * => array('a' => 'required')
 *
 * // multiple errors
 * f\validate_coll(array(), array('a' => f\required(), 'b' => f\required()));
 * => array('a' => 'required', 'b' => 'required')
 *
 * // validator fn
 * f\validate_coll(array('a' => 1.0), array('a' => f\required('v' => 'is_int')));
 * => array('a' => 'invalid')
 *
 * // without errors
 * f\validate_coll(array('a' => 1), array('a' => f\required('v' => 'is_int')));
 * => array()
 */
function validate_coll($coll, $paramRules)
{
    $validate = function ($paramRule, $key) use($coll) {
        if (f\not($paramRule instanceof param_rule)) {
            throw new \InvalidArgumentException('Param rules must be created with felpado\\required and felpado\\optional.');
        }
        if (f\contains($coll, $key)) {
            if ($paramRule->getValidator()) {
                $value = f\get($coll, $key);
                if (f\not(is_null($value) && $paramRule instanceof optional)) {
                    $isValid = f\validate($value, $paramRule->getValidator());
                    if (f\not($isValid)) {
                        return 'invalid';
                    }
                }
            }
        } elseif ($paramRule instanceof required) {
            return 'required';
        }
        return null;
    };
    return f\filter_indexed('felpado\\identity', f\map($validate, $paramRules));
}
 public function testFind()
 {
     $refreshToken = $this->createRefreshToken();
     $this->cache->shouldReceive('fetch')->once()->with(f\get($refreshToken, 'token'))->andReturn($refreshToken->getParams());
     $this->assertEquals($refreshToken, $this->repository->find(f\get($refreshToken, 'token')));
 }
Exemplo n.º 27
0
 /**
  * @return array An array of parameters.
  */
 public function create(Context $context)
 {
     $params = $this->delegate->create($context);
     $refreshToken = $this->refreshTokenCreator->create(f\get($params, 'access_token'));
     return f\assoc($params, 'refresh_token', f\get($refreshToken, 'token'));
 }
Exemplo n.º 28
0
 public function hasAllowedScope($scope)
 {
     return array_search($scope, f\get($this, 'allowedScopes')) !== false;
 }
 private function filterParamsFromDomain($params)
 {
     return f\assoc($params, 'oauth2Id', f\get($params, 'id'));
 }
Exemplo n.º 30
0
 public function create(Context $context)
 {
     $accessToken = $this->accessTokenCreator->create($context);
     return ['access_token' => f\get($accessToken, 'token'), 'token_type' => f\get($accessToken, 'type'), 'expires_in' => f\get($accessToken, 'lifetime'), 'scope' => f\get($accessToken, 'scope')];
 }