Jelajahi Sumber

login oauth

Guillermo Espinoza 8 tahun lalu
induk
melakukan
2e743e8256

+ 93 - 0
Controller/SecurityController.php

@@ -0,0 +1,93 @@
+<?php
+
+/*
+ * This file is part of the FOSUserBundle package.
+ *
+ * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Base\OAuthBundle\Controller;
+
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Security\Core\Exception\AuthenticationException;
+use Symfony\Component\Security\Core\Security;
+use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
+
+class SecurityController extends Controller
+{
+    /**
+     * @Route("/oauth/v2/auth/login", name="oauth_login")
+     * @param Request $request
+     *
+     * @return Response
+     */
+    public function loginAction(Request $request)
+    {
+        /** @var $session \Symfony\Component\HttpFoundation\Session\Session */
+        $session = $request->getSession();
+
+        $authErrorKey = Security::AUTHENTICATION_ERROR;
+        $lastUsernameKey = Security::LAST_USERNAME;
+
+        // get the error if any (works with forward and redirect -- see below)
+        if ($request->attributes->has($authErrorKey)) {
+            $error = $request->attributes->get($authErrorKey);
+        } elseif (null !== $session && $session->has($authErrorKey)) {
+            $error = $session->get($authErrorKey);
+            $session->remove($authErrorKey);
+        } else {
+            $error = null;
+        }
+
+        if (!$error instanceof AuthenticationException) {
+            $error = null; // The value does not come from the security component.
+        }
+
+        // last username entered by the user
+        $lastUsername = (null === $session) ? '' : $session->get($lastUsernameKey);
+
+        $csrfToken = $this->has('security.csrf.token_manager')
+            ? $this->get('security.csrf.token_manager')->getToken('authenticate')->getValue()
+            : null;
+
+        return $this->renderLogin(array(
+            'last_username' => $lastUsername,
+            'error' => $error,
+            'csrf_token' => $csrfToken,
+        ));
+    }
+
+    /**
+     * Renders the login template with the given parameters. Overwrite this function in
+     * an extended controller to provide additional data for the login template.
+     *
+     * @param array $data
+     *
+     * @return Response
+     */
+    protected function renderLogin(array $data)
+    {
+        return $this->render('@BaseOAuthBundle/Security/login.html.twig', $data);
+    }
+
+    /**
+     * @Route("/oauth/v2/auth/login_check", name="oauth_login_check")
+     */
+    public function checkAction()
+    {
+        throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
+    }
+
+    /**
+     * @Route("/oauth/v2/auth/logout", name="oauth_logout")
+     */
+    public function logoutAction()
+    {
+        throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
+    }
+}

+ 5 - 1
Resources/config/fosoauthserverbundle/config.yml

@@ -7,4 +7,8 @@ fos_oauth_server:
     service:
         user_provider: fos_user.user_provider.username_email
         options:
-            supported_scopes: user
+            supported_scopes: user
+
+twig:
+    paths:
+        '%kernel.root_dir%/../vendor/base-oauth-bundle/Resources/views': BaseOAuthBundle            

+ 0 - 15
Resources/config/fosoauthserverbundle/routing.yml

@@ -1,20 +1,5 @@
-base_o_auth:
-    resource: "@BaseOAuthBundle/Controller/"
-    type:     annotation    
-    prefix:   /
-
 fos_oauth_server_token:
     resource: "@FOSOAuthServerBundle/Resources/config/routing/token.xml"
 
 fos_oauth_server_authorize:
     resource: "@FOSOAuthServerBundle/Resources/config/routing/authorize.xml"
-
-oauth_login:
-    path: /oauth/v2/auth/login
-    defaults:
-        _controller: FOSUserBundle:Security:login
-
-oauth_login_check:
-    path: /oauth/v2/auth/check
-    defaults:
-        _controller: FOSUserBundle:Security:check

+ 1 - 6
Resources/config/hwioauthbundle/routing.yml

@@ -8,9 +8,4 @@ hwi_oauth_connect:
 
 hwi_oauth_login:
     resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
-    prefix:   /login
-
-base_o_auth:
-    resource: "@BaseOAuthBundle/Controller/"
-    type:     annotation    
-    prefix:   /
+    prefix:   /login

+ 5 - 0
Resources/views/Security/login.html.twig

@@ -0,0 +1,5 @@
+{% extends "BaseOAuthBundle::layout.html.twig" %}
+
+{% block fos_user_content %}
+    {{ include("BaseOAuthBundle:Security:login_content.html.twig") }}
+{% endblock fos_user_content %}

+ 22 - 0
Resources/views/Security/login_content.html.twig

@@ -0,0 +1,22 @@
+{% trans_default_domain 'FOSUserBundle' %}
+
+{% if error %}
+    <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
+{% endif %}
+
+<form action="{{ path("oauth_login_check") }}" method="post">
+    {% if csrf_token %}
+        <input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />
+    {% endif %}
+
+    <label for="username">{{ 'security.login.username'|trans }}</label>
+    <input type="text" id="username" name="_username" value="{{ last_username }}" required="required" />
+
+    <label for="password">{{ 'security.login.password'|trans }}</label>
+    <input type="password" id="password" name="_password" required="required" />
+
+    <input type="checkbox" id="remember_me" name="_remember_me" value="on" />
+    <label for="remember_me">{{ 'security.login.remember_me'|trans }}</label>
+
+    <input type="submit" id="_submit" name="_submit" value="{{ 'security.login.submit'|trans }}" />
+</form>

+ 33 - 0
Resources/views/layout.html.twig

@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <meta charset="UTF-8" />
+    </head>
+    <body>
+        <div>
+            {% if is_granted("IS_AUTHENTICATED_REMEMBERED") %}
+                {{ 'layout.logged_in_as'|trans({'%username%': app.user.username}, 'FOSUserBundle') }} |
+                <a href="{{ path('fos_user_security_logout') }}">
+                    {{ 'layout.logout'|trans({}, 'FOSUserBundle') }}
+                </a>
+            {% else %}
+                <a href="{{ path('fos_user_security_login') }}">{{ 'layout.login'|trans({}, 'FOSUserBundle') }}</a>
+            {% endif %}
+        </div>
+
+        {% if app.request.hasPreviousSession %}
+            {% for type, messages in app.session.flashbag.all() %}
+                {% for message in messages %}
+                    <div class="flash-{{ type }}">
+                        {{ message }}
+                    </div>
+                {% endfor %}
+            {% endfor %}
+        {% endif %}
+
+        <div>
+            {% block fos_user_content %}
+            {% endblock fos_user_content %}
+        </div>
+    </body>
+</html>