Skip to content

Commit

Permalink
* Cleanup legacy variable naming style.
Browse files Browse the repository at this point in the history
* Prepare for authorize scope page.
  • Loading branch information
hswong3i committed Jul 4, 2014
1 parent 7bffb7b commit dfc98d3
Show file tree
Hide file tree
Showing 43 changed files with 525 additions and 250 deletions.
18 changes: 11 additions & 7 deletions src/AuthBucket/OAuth2/Controller/AuthorizeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,45 +30,49 @@ class AuthorizeController
protected $modelManagerFactory;
protected $responseTypeHandlerFactory;
protected $tokenTypeHandlerFactory;
protected $authorizeScopeUri;

public function __construct(
SecurityContextInterface $securityContext,
ModelManagerFactoryInterface $modelManagerFactory,
ResponseTypeHandlerFactoryInterface $responseTypeHandlerFactory,
TokenTypeHandlerFactoryInterface $tokenTypeHandlerFactory
TokenTypeHandlerFactoryInterface $tokenTypeHandlerFactory,
$authorizeScopeUri = null
)
{
$this->securityContext = $securityContext;
$this->modelManagerFactory = $modelManagerFactory;
$this->responseTypeHandlerFactory = $responseTypeHandlerFactory;
$this->tokenTypeHandlerFactory = $tokenTypeHandlerFactory;
$this->authorizeScopeUri = $authorizeScopeUri;
}

public function authorizeAction(Request $request)
{
// Fetch response_type from GET.
$response_type = $this->getResponseType($request);
$responseType = $this->getResponseType($request);

// Handle authorize endpoint response.
return $this->responseTypeHandlerFactory->getResponseTypeHandler($response_type)->handle(
return $this->responseTypeHandlerFactory->getResponseTypeHandler($responseType)->handle(
$this->securityContext,
$request,
$this->modelManagerFactory,
$this->tokenTypeHandlerFactory
$this->tokenTypeHandlerFactory,
$this->authorizeScopeUri
);
}

private function getResponseType(Request $request)
{
// Validate and set response_type.
$response_type = $request->query->get('response_type');
$responseType = $request->query->get('response_type');
$query = array(
'response_type' => $response_type
'response_type' => $responseType
);
if (!Filter::filter($query)) {
throw new InvalidRequestException();
}

return $response_type;
return $responseType;
}
}
32 changes: 16 additions & 16 deletions src/AuthBucket/OAuth2/Controller/DebugController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,45 +42,45 @@ public function debugAction(Request $request)
$accessTokenManager = $this->modelManagerFactory->getModelManager('access_token');

// Fetch access_token from GET.
$debug = $this->getDebug($request);
$access_token = $accessTokenManager->findAccessTokenByAccessToken($debug);
if (null === $access_token) {
$debugToken = $this->getDebugToken($request);
$accessToken = $accessTokenManager->findAccessTokenByAccessToken($debugToken);
if (null === $accessToken) {
throw new InvalidRequestException();
} elseif ($access_token->getExpires() < new \DateTime()) {
} elseif ($accessToken->getExpires() < new \DateTime()) {
throw new InvalidRequestException();
}

// Handle debug endpoint response.
$parameters = array(
'access_token' => $access_token->getAccessToken(),
'token_type' => $access_token->getTokenType(),
'client_id' => $access_token->getClientId(),
'username' => $access_token->getUsername(),
'expires' => $access_token->getExpires()->getTimestamp(),
'scope' => $access_token->getScope(),
'access_token' => $accessToken->getAccessToken(),
'token_type' => $accessToken->getTokenType(),
'client_id' => $accessToken->getClientId(),
'username' => $accessToken->getUsername(),
'expires' => $accessToken->getExpires()->getTimestamp(),
'scope' => $accessToken->getScope(),
);

return JsonResponse::create($parameters);
}

private function getDebug(Request $request)
private function getDebugToken(Request $request)
{
// Fetch debug token from GET/POST/access_token.
$debug = $request->query->get('debug')
?: $request->request->get('debug')
$debugToken = $request->query->get('debug_token')
?: $request->request->get('debug_token')
?: $this->securityContext->getToken()->getAccessToken()->getAccessToken();
if (null === $debug) {
if (null === $debugToken) {
throw new InvalidRequestException();
}

// Validate debug token.
$query = array(
'access_token' => $debug,
'access_token' => $debugToken,
);
if (!Filter::filter($query)) {
throw new InvalidRequestException();
}

return $debug;
return $debugToken;
}
}
10 changes: 5 additions & 5 deletions src/AuthBucket/OAuth2/Controller/TokenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public function __construct(
public function tokenAction(Request $request)
{
// Fetch grant_type from POST.
$grant_type = $this->getGrantType($request);
$grantType = $this->getGrantType($request);

// Handle token endpoint response.
return $this->grantTypeHandlerFactory->getGrantTypeHandler($grant_type)->handle(
return $this->grantTypeHandlerFactory->getGrantTypeHandler($grantType)->handle(
$this->securityContext,
$this->userChecker,
$this->encoderFactory,
Expand All @@ -73,14 +73,14 @@ public function tokenAction(Request $request)
private function getGrantType(Request $request)
{
// grant_type must set and in valid format.
$grant_type = $request->request->get('grant_type');
$grantType = $request->request->get('grant_type');
$query = array(
'grant_type' => $grant_type
'grant_type' => $grantType
);
if (!Filter::filter($query)) {
throw new InvalidRequestException();
}

return $grant_type;
return $grantType;
}
}
4 changes: 2 additions & 2 deletions src/AuthBucket/OAuth2/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ private function handleException(
$message = unserialize($exception->getMessage());

if (isset($message['redirect_uri'])) {
$redirect_uri = $message['redirect_uri'];
$redirectUri = $message['redirect_uri'];
unset($message['redirect_uri']);
$response = RedirectResponse::create($redirect_uri, $message);
$response = RedirectResponse::create($redirectUri, $message);
} else {
$code = $exception->getCode();
$response = JsonResponse::create($message, $code);
Expand Down
18 changes: 9 additions & 9 deletions src/AuthBucket/OAuth2/GrantType/AbstractGrantTypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ protected function checkClientId(
SecurityContextInterface $securityContext
)
{
$client_id = $securityContext->getToken()->getClientId();
$clientId = $securityContext->getToken()->getClientId();

return $client_id;
return $clientId;
}

/**
Expand All @@ -58,7 +58,7 @@ protected function checkClientId(
protected function checkScope(
Request $request,
ModelManagerFactoryInterface $modelManagerFactory,
$client_id,
$clientId,
$username
)
{
Expand All @@ -75,24 +75,24 @@ protected function checkScope(
}

// Compare if given scope within all available authorized scopes.
$authorized_scope = array();
$scopeAuthorized = array();
$authorizeManager = $modelManagerFactory->getModelManager('authorize');
$result = $authorizeManager->findAuthorizeByClientIdAndUsername($client_id, $username);
$result = $authorizeManager->findAuthorizeByClientIdAndUsername($clientId, $username);
if ($result !== null) {
$authorized_scope = $result->getScope();
$scopeAuthorized = $result->getScope();
}

$supported_scope = array();
$scopeSupported = array();
$scopeManager = $modelManagerFactory->getModelManager('scope');
$result = $scopeManager->findScopes();
if ($result !== null) {
foreach ($result as $row) {
$supported_scope[] = $row->getScope();
$scopeSupported[] = $row->getScope();
}
}

$scope = preg_split('/\s+/', $scope);
if (array_intersect($scope, $authorized_scope, $supported_scope) != $scope) {
if (array_intersect($scope, $scopeAuthorized, $scopeSupported) != $scope) {
throw new InvalidScopeException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ public function handle(
)
{
// Fetch client_id from authenticated token.
$client_id = $this->checkClientId($securityContext);
$clientId = $this->checkClientId($securityContext);

// Fetch username and scope from stored code.
list($username, $scope) = $this->checkCode($request, $modelManagerFactory, $client_id);
list($username, $scope) = $this->checkCode($request, $modelManagerFactory, $clientId);

// Check and set redirect_uri.
$redirect_uri = $this->checkRedirectUri($request, $modelManagerFactory, $client_id);
$redirectUri = $this->checkRedirectUri($request, $modelManagerFactory, $clientId);

// Generate access_token, store to backend and set token response.
$parameters = $tokenTypeHandlerFactory->getTokenTypeHandler()->createAccessToken(
$modelManagerFactory,
$client_id,
$clientId,
$username,
$scope
);
Expand All @@ -65,7 +65,7 @@ public function handle(
*
* @param Request $request Incoming request object.
* @param ModelManagerFactoryInterface $modelManagerFactory Model manager factory for compare with database record.
* @param string $client_id Corresponding client_id that code should belongs to.
* @param string $clientId Corresponding client_id that code should belongs to.
*
* @return array A list with stored username and scope, originally grant in authorize endpoint.
*
Expand All @@ -75,7 +75,7 @@ public function handle(
private function checkCode(
Request $request,
ModelManagerFactoryInterface $modelManagerFactory,
$client_id
$clientId
)
{
$code = $request->request->get('code');
Expand All @@ -91,7 +91,7 @@ private function checkCode(
// Check code with database record.
$codeManager = $modelManagerFactory->getModelManager('code');
$result = $codeManager->findCodeByCode($code);
if ($result === null || $result->getClientId() !== $client_id) {
if ($result === null || $result->getClientId() !== $clientId) {
throw new InvalidGrantException();
} elseif ($result->getExpires() < new \DateTime()) {
throw new InvalidGrantException();
Expand All @@ -105,7 +105,7 @@ private function checkCode(
*
* @param Request $request Incoming request object.
* @param ModelManagerFactoryInterface $modelManagerFactory Model manager factory for compare with database record.
* @param string $client_id Corresponding client_id that code should belongs to.
* @param string $clientId Corresponding client_id that code should belongs to.
*
* @return string The supplied redirect_uri from incoming request, or from stored record.
*
Expand All @@ -114,34 +114,34 @@ private function checkCode(
private function checkRedirectUri(
Request $request,
ModelManagerFactoryInterface $modelManagerFactory,
$client_id
$clientId
)
{
$redirect_uri = $request->request->get('redirect_uri');
$redirectUri = $request->request->get('redirect_uri');

// redirect_uri is not required if already established via other channels,
// check an existing redirect URI against the one supplied.
$stored = null;
$clientManager = $modelManagerFactory->getModelManager('client');
$result = $clientManager->findClientByClientId($client_id);
$result = $clientManager->findClientByClientId($clientId);
if ($result !== null && $result->getRedirectUri()) {
$stored = $result->getRedirectUri();
}

// At least one of: existing redirect URI or input redirect URI must be
// specified.
if (!$stored && !$redirect_uri) {
if (!$stored && !$redirectUri) {
throw new InvalidRequestException();
}

// If there's an existing uri and one from input, verify that they match.
if ($stored && $redirect_uri) {
if ($stored && $redirectUri) {
// Ensure that the input uri starts with the stored uri.
if (strcasecmp(substr($redirect_uri, 0, strlen($stored)), $stored) !== 0) {
if (strcasecmp(substr($redirectUri, 0, strlen($stored)), $stored) !== 0) {
throw new InvalidRequestException();
}
}

return $redirect_uri ?: $stored;
return $redirectUri ?: $stored;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ public function handle(
)
{
// Fetch client_id from authenticated token.
$client_id = $this->checkClientId($securityContext);
$clientId = $this->checkClientId($securityContext);

// No (and not possible to have) username, set as empty string.
$username = '';

// Check and set scope.
$scope = $this->checkScope($request, $modelManagerFactory, $client_id, $username);
$scope = $this->checkScope($request, $modelManagerFactory, $clientId, $username);

// Generate access_token, store to backend and set token response.
$parameters = $tokenTypeHandlerFactory->getTokenTypeHandler()->createAccessToken(
$modelManagerFactory,
$client_id,
$clientId,
$username,
$scope
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ interface GrantTypeHandlerFactoryInterface
/**
* Gets a stored grant type handler.
*
* @param string $type
* Type of grant type handler, as refer to RFC6749.
* @param string $type Type of grant type handler, as refer to RFC6749.
*
* @return GrantTypeHandlerInterface
* The stored grant type handler.
* @return GrantTypeHandlerInterface The stored grant type handler.
*
* @throw UnsupportedGrantTypeException
* If supplied grant type not found.
* @throw UnsupportedGrantTypeException If supplied grant type not found.
*/
public function getGrantTypeHandler($type);
}
6 changes: 3 additions & 3 deletions src/AuthBucket/OAuth2/GrantType/PasswordGrantTypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function handle(
}

// Fetch client_id from authenticated token.
$client_id = $this->checkClientId($securityContext);
$clientId = $this->checkClientId($securityContext);

// Check resource owner credentials
$username = $this->checkUsername(
Expand All @@ -64,12 +64,12 @@ public function handle(
);

// Check and set scope.
$scope = $this->checkScope($request, $modelManagerFactory, $client_id, $username);
$scope = $this->checkScope($request, $modelManagerFactory, $clientId, $username);

// Generate access_token, store to backend and set token response.
$parameters = $tokenTypeHandlerFactory->getTokenTypeHandler()->createAccessToken(
$modelManagerFactory,
$client_id,
$clientId,
$username,
$scope
);
Expand Down
Loading

0 comments on commit dfc98d3

Please sign in to comment.