Skip to content

Instantly share code, notes, and snippets.

@gsouf
Last active April 9, 2021 00:25
Show Gist options
  • Save gsouf/6909234 to your computer and use it in GitHub Desktop.
Save gsouf/6909234 to your computer and use it in GitHub Desktop.
oAuth with Phalcon and PHPoAuthLib
<?php
namespace Controllers;
use OAuth\Common\Service\AbstractService;
use OAuth\Common\Storage\Session as OAuthSession;
class AuthController extends ControllerBase {
// everytime we enter the controller, then we check for login, if yes, then we dont have to access here (except logout)
public function beforeExecuteRoute($dispatcher){
if ($dispatcher->getActionName() == 'logout') {
return true;
}
$isLogged = ...; // specific to your application
if($isLogged){
return $this->response->redirect();
}
return true;
}
/**
* @param $serviceName string name of the service
* @return bool|\OAuth\ServiceFactory the service ready to be requested or false if something went wrong
*/
protected function getOAuthService($serviceName,$storage){
$oAuthCredentials = $this->getDI()->get("config")["oAuth"];
// the auth service must be registered in config
if(!isset($oAuthCredentials[$serviceName])){
return false;
}
$uriFactory = new \OAuth\Common\Http\Uri\UriFactory();
$currentUri = $uriFactory->createFromSuperGlobalArray($_SERVER);
$currentUri->setQuery('');
$credentials = new OAuthCredentials(
$oAuthCredentials[$serviceName]['key'],
$oAuthCredentials[$serviceName]['secret'],
$currentUri->getAbsoluteUri()
);
$serviceFactory = new \OAuth\ServiceFactory();
$service = $serviceFactory->createService($serviceName, $credentials, $storage);
return $service;
}
protected function proceedOAuth($serviceName){
$authService = $this->di->get("auth");
$storage = new OAuthSession();
$storage->clearAllTokens();
$service = $this->getOAuthService($serviceName,$storage);
// make sure service is ok
if(!$service){
return $this->dispatcher->forward("error","notFound"); //specific to your application
}
if ( $this->request->hasQuery("code") ) {
$code = $this->request->getQuery("code");
if( empty($code) ){
$this->flash->error("A problem occurred with $serviceName");
return $this->dispatcher->forward("error","fatal");//specific to your application
}
try{
$service->requestAccessToken($code);
}catch (\OAuth\Common\Http\Exception\TokenResponseException $e){
return null;
}
return $service;
}else{
$url = $service->getAuthorizationUri();
$this->response->redirect($url,true)->send();
return null;
}
}
public function githubAction(){
// connect with github oAuth
$service = $this->proceedOAuth("GitHub");
// when connected, then we access this point
// and we can query github api
try{
$result = json_decode($service->request('user'), true);
}catch(\Exception $e){
//...
}
}
public function googleAction(){
// connect with google oAuth
$service = $this->proceedOAuth("google");
// when connected, then we access this point
// and we can query google api
try{
$result = json_decode($googleService->request('https://www.googleapis.com/oauth2/v1/userinfo'), true);
}catch(\Exception $e){
//...
}
}
}
{
"require" : {
"lusitanian/oauth": "~0.2"
}
}
/*** phalcon config array ***/
'oAuth' => array(
"GitHub" => array(
'key' => 'somekey',
'secret' => 'somesecrete',
),
"google" => array(
'key' => 'somekey',
'secret' => 'somesecrete',
)
)
/*** phalcon config array ***/
// add the route to match the auth
$router->add("/auth/:action",
array(
"controller" => "auth",
"action" => 1,
)
);
// you may also simply use the auto defaut routing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment