Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
vl committed Jan 16, 2017
0 parents commit e928935
Show file tree
Hide file tree
Showing 25 changed files with 6,136 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
/vendor
27 changes: 27 additions & 0 deletions Controller/ApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Playtini\Bundle\AuthBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;

/**
* ApiController
*/
class ApiController extends Controller
{
/**
* Provides key that can be used to connect to other services with AuthBundle
*
* @return JsonResponse {api_key: string}
*/
public function keyAction()
{
$username = $this->getUser()->getUsername();
$apiKey = $this->get('playtini.auth.security.api_key_user_provider')->getHash($username);

return JsonResponse::create([
'api_key' => $username . '~' . $apiKey,
]);
}
}
64 changes: 64 additions & 0 deletions Controller/GoogleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Playtini\Bundle\AuthBundle\Controller;

use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

class GoogleController extends Controller
{
/**
* Link to this controller to start the "connect" process
*
* @param Request $request
* @return Response
*/
public function loginAction(Request $request)
{
$link = $this->get('oauth2.registry')->getClient('google')->getOAuth2Provider()->getAuthorizationUrl();

if (!$request->cookies->get('logout')) {
return RedirectResponse::create($link);
}

$response = new Response();
$response->headers->clearCookie('logout');

return $this->render('AuthBundle::login.html.twig', [
'link' => $link
], $response);
}

/**
* After going to Facebook, you're redirect back here
* because this is the "redirect_route" you configured
* in services.yml
*
* @param Request $request
* @return Response
*/
public function loginCheckAction(Request $request)
{
// ** if you want to *authenticate* the user, then
// leave this method blank and create a Guard authenticator
}

public function logoutAction()
{
// will never be executed
}

/**
* @return RedirectResponse
*/
public function forceLogoutAction()
{
$response = new RedirectResponse($this->get('router')->generate('mailer_auth_security_logout'));
$response->headers->setCookie(new Cookie('logout', 1, '+1 hour'));

return $response;
}
}
49 changes: 49 additions & 0 deletions DependencyInjection/AuthExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Playtini\Bundle\AuthBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class AuthExtension extends Extension implements PrependExtensionInterface
{
/**
* @param ContainerBuilder $container
*/
public function prepend(ContainerBuilder $container)
{
$config = [
'clients' => [
'google' => [
// must be "google" - it activates that type!
'type' => 'google',
// add and configure client_id and client_secret in parameters.yml
'client_id' => $container->getParameter('google_app_id'),
'client_secret' => $container->getParameter('google_app_secret'),
// a route name you'll create
'redirect_route' => 'playtini_auth_connect_google_check',
'redirect_params' => [],
// Optional value for sending access_type parameter. More detail: https://developers.google.com/identity/protocols/OAuth2WebServer#offline
'access_type' => 'online',
// Optional value for sending hd parameter. More detail: https://developers.google.com/accounts/docs/OAuth2Login#hd-param
//'hosted_domain' => 'yourdomain.com',
// whether to check OAuth2 "state": defaults to true
'use_state' => false
]
]
];
$container->prependExtensionConfig('knpu_oauth2_client', $config);
}

/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');
}
}
Loading

0 comments on commit e928935

Please sign in to comment.