private function createRefreshingTokenCreator($accessingCreator) { if (f\not($this->storage->hasRefreshTokenRepository())) { return $accessingCreator; } return new RefreshingTokenCreator($accessingCreator, $this->createRefreshTokenCreator()); }
private function getUserId(array $inputData) { if (f\not($inputData, 'user_id')) { throw new UserNotFoundException(); } return f\get($inputData, 'user_id'); }
public function check(UserCredentials $userCredentials) { $user = $this->findUser($userCredentials->getUsername()); if (f\not($user)) { return false; } return f\get($user, 'password') === $userCredentials->getPassword(); }
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 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); }
/** * f\rename_keys($coll, $keysMap) * * Returns a new coll with the keys from keysMap renamed. * * f\rename_keys(array('a' => 1, 'b' => 2), array('a' => 'c', 'b' => 'd')) * => array('c' => 1, 'd' => 2) */ function rename_keys($coll, $keysMap) { if (f\not($keysMap)) { return $coll; } $from = f\first(f\keys($keysMap)); $to = f\first($keysMap); return f\rename_keys(f\rename_key($coll, $from, $to), f\dissoc($keysMap, $from)); }
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; }
/** * @return array */ public function obtain(Request $request) { if (f\not($request->headers->has('authorization'))) { throw new AccessTokenDataNotFoundOAuthErrorException(); } $authorization = $request->headers->get('authorization'); $regex = '/^Bearer (.+)$/'; if (f\not(preg_match($regex, $authorization))) { throw new AccessTokenDataNotFoundOAuthErrorException(); } $token = preg_replace($regex, '$1', $authorization); return array('token' => $token); }
/** * 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; }
/** * 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); }
/** * f\assoc_in($coll, $in, $value) * * Returns an array based on coll with value associated in the nested structure of in. * * f\assoc_in(array('a' => 1), array('b', 'b1'), 2); * => array('a' => 1, 'b' => array('b1' => 2)) * * // does nothing without in * f\assoc_in(array('a' => 1), array(), 2); * => array('a' => 1) * * // supports infinite nesting * f\assoc_in(array(), array('a', 'a1', 'a1I', 'a1IA'), 1); * => array('a' => array('a1' => array('a1I' => array('a1IA' => 1)))) */ function assoc_in($coll, $in, $value) { if (empty($in)) { return $coll; } $array = f\to_array($coll); $current =& $array; foreach ($in as $k) { if (f\not(f\get_or($current, $k, null))) { $current = f\assoc($current, $k, array()); } $current =& $current[$k]; } $current = $value; return $array; }
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'))); }
/** * 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)); }
/** * @dataProvider provideNotFalse */ public function testNotFalse($value) { $this->assertFalse(f\not($value)); }
/** * @Then /^the response content should match "([^"]*)"$/ */ public function theResponseContentShouldNotMatch($regex) { $content = $this->getResponse()->getContent(); if (f\not(preg_match($regex, $content))) { throw new Exception(sprintf('The response content is "%s" and it should match "%s", but it does not.', $content, $regex)); } }
/** * @When /^I try to grant a token with the client "([^"]*)" and the user id "([^"]*)" and the scope "([^"]*)"$/ */ public function iTryToGrantATokenWithTheClientAndTheUserIdAndTheScope($clientName, $userId, $scope) { $client = $this->findClientByName($clientName); $this->apiContext->addHttpBasicAuthentication(f\get($client, 'id'), f\get($client, 'secret')); $inputData = ['grant_type' => 'direct', 'user_id' => $userId]; if (f\not(is_null($scope))) { $inputData = f\assoc($inputData, 'scope', $scope); } f\each(function ($v, $k) { $this->getApiContext()->addRequestParameter($k, $v); }, $inputData); $this->iMakeAOauthTokenRequest(); }
/** * @Given /^the container should has the service "([^"]*)"$/ */ public function theContainerShouldHasTheService($id) { if (f\not($this->getContainer()->has($id))) { throw new \Exception(sprintf('The container should have the service "%s but it does not.', $id)); } }
private function hasNoUserCredentialsInInputData($inputData) { return f\not(f\contains($inputData, 'username')) || f\not(f\contains($inputData, 'password')); }