Oauthost module: Passing auth_token and auth_role in cookies. Front-end:...

Oauthost module: Passing auth_token and auth_role in cookies. Front-end: Retrieve them from cookies and then remove them
parent 08a487f5
......@@ -15,6 +15,8 @@ use Drupal\Core\Database\Connection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Symfony\Component\HttpFoundation\Cookie;
require ('RedirectResponseWithCookie.php');
class CBController extends ControllerBase
{
......@@ -104,7 +106,12 @@ class CBController extends ControllerBase
$epalToken = $this->authenticatePhase2($request, $authToken, $authVerifier);
if ($epalToken) {
return new RedirectResponse($this->redirect_url . $epalToken.'&auth_role=student', 302, []);
$cookie = new Cookie('auth_token', $epalToken, 0, '/', null, false, false);
$cookie2 = new Cookie('auth_role', 'student', 0, '/', null, false, false);
return new RedirectResponseWithCookie($this->redirect_url, 302, array ($cookie, $cookie2));
// return new RedirectResponse($this->redirect_url . $epalToken.'&auth_role=student', 302, []);
} else {
$response = new Response();
$response->setContent('forbidden');
......
<?php
/*
* A RedirectResponse object with cookie sending
*/
namespace Drupal\oauthost\Controller;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* RedirectResponseWithCookie represents an HTTP response doing a redirect and sending cookies.
*/
class RedirectResponseWithCookie extends RedirectResponse
{
/**
* Creates a redirect response so that it conforms to the rules defined for a redirect status code.
*
* @param string $url The URL to redirect to
* @param integer $status The status code (302 by default)
* @param Symfony\Component\HttpFoundation\Cookie[] $cookies An array of Cookie objects
*/
public function __construct($url, $status = 302, $cookies = array ())
{
parent::__construct($url, $status);
foreach ($cookies as $cookie)
{
if (!$cookie instanceof Cookie)
{
throw new \InvalidArgumentException(sprintf('Third parameter is not a valid Cookie object.'));
}
$this->headers->setCookie($cookie);
}
}
}
......@@ -6,6 +6,7 @@ import { NgRedux, select } from 'ng2-redux';
import { Observable } from 'rxjs/Rx';
import { IAppState } from '../store/store';
import { HelperDataService } from '../services/helper-data-service';
import { CookieService } from 'ngx-cookie';
import {
FormBuilder,
FormGroup,
......@@ -49,7 +50,8 @@ export default class Home implements OnInit {
private _ngRedux: NgRedux<IAppState>,
private activatedRoute: ActivatedRoute,
private _hds: HelperDataService,
private router: Router
private router: Router,
private _cookieService:CookieService
) {
this.authToken = '';
this.authRole = '';
......@@ -59,6 +61,14 @@ export default class Home implements OnInit {
};
ngOnInit() {
this.authToken = this.getCookie('auth_token');
this.authRole = this.getCookie('auth_role');
if (this.authToken && this.authRole) {
this._ata.getloginInfo({ auth_token: this.authToken, auth_role: this.authRole });
this.removeCookie('auth_token');
this.removeCookie('auth_role');
}
this.loginInfo$ = this._ngRedux.select(state => {
if (state.loginInfo.size > 0) {
state.loginInfo.reduce(({}, loginInfoToken) => {
......@@ -74,7 +84,7 @@ export default class Home implements OnInit {
});
// subscribe to router event
this.activatedRoute.queryParams.subscribe((params: Params) => {
/* this.activatedRoute.queryParams.subscribe((params: Params) => {
if (params) {
this.authToken = params['auth_token'];
this.authRole = params['auth_role'];
......@@ -83,8 +93,17 @@ export default class Home implements OnInit {
if (this.authToken && this.authRole)
this._ata.getloginInfo({ auth_token: this.authToken, auth_role: this.authRole });
});
}); */
}
getCookie(key: string){
return this._cookieService.get(key);
}
removeCookie(key: string){
return this._cookieService.remove(key);
}
checkvalidation() {
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment