created new oauthost module

parent 91b3df4d
......@@ -6,6 +6,7 @@ drupal/*
drupal/modules/*
!drupal/modules/epal/
!drupal/modules/epalreadydata/
!drupal/modules/oauthost/
# Logs
*.log
......
{
"name": "drupal/oauthost",
"type": "drupal-module",
"description": "Authentication Provider OST",
"keywords": ["Drupal"],
"license": "GPL-2.0+",
"homepage": "https://www.drupal.org/project/oauthost",
"minimum-stability": "dev",
"support": {
"issues": "https://www.drupal.org/project/issues/oauthost",
"source": "http://cgit.drupalcode.org/oauthost"
},
"require": { }
}
name: oauthost
type: module
description: Authentication Provider OST
core: 8.x
package: oauthost
<?php
/**
* @file
* Contains oauthost.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function oauthost_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the oauthost module.
case 'help.page.oauthost':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Authentication Provider OST') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function oauthost_theme() {
return [
'oauthost' => [
'template' => 'oauthost',
'render element' => 'children',
],
];
}
services:
authentication.oauthost:
class: Drupal\oauthost\Authentication\Provider\OSTAuthenticationProvider
arguments: ['@config.factory', '@entity_type.manager']
tags:
- { name: authentication_provider, provider_id: ostauthentication_provider, priority: 100 }
<?php
namespace Drupal\oauthost\Authentication\Provider;
use Drupal\Core\Authentication\AuthenticationProviderInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use \OAuthProvider;
use \OAuthException;
/**
* Class OSTAuthenticationProvider.
*
* @package Drupal\oauthost\Authentication\Provider
*/
class OSTAuthenticationProvider implements AuthenticationProviderInterface {
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The user data service.
*
* @var \Drupal\user\UserDataInterface
*/
protected $user_data;
/**
* The logger service for OAuth.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* An authenticated user object.
*
* @var \Drupal\user\UserBCDecorator
*/
protected $user;
/**
* Constructor.
*
* @param \Drupal\user\UserDataInterface
* The user data service.
* @param \Psr\Log\LoggerInterface $logger
* The logger service for OAuth.
*/
/* public function __construct(UserDataInterface $user_data, LoggerInterface $logger) {
$this->user_data = $user_data;
$this->logger = $logger;
} */
/**
* Constructs a HTTP basic authentication provider object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
*/
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
$this->configFactory = $config_factory;
$this->entityTypeManager = $entity_type_manager;
}
/**
* Checks whether suitable authentication credentials are on the request.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
*
* @return bool
* TRUE if authentication credentials suitable for this provider are on the
* request, FALSE otherwise.
*/
public function applies(Request $request) {
// If you return TRUE and the method Authentication logic fails,
// you will get out from Drupal navigation if you are logged in.
// Only check requests with the 'authorization' header starting with OAuth.
drupal_set_message("sdfsddgdg");
return getHeader($request, 'OAuthEnabled');
// return preg_match('/^OAuth/', $request->headers->get('authorization'));
// return $this->checkAuthToken($this->getAuthToken($request));
}
private function checkAuthToken($authToken) {
if (!$authToken) {
return TRUE;
}
else if ($authToken === 'bourboutsala') {
return FALSE;
}
else {
return TRUE;
}
}
private function getLoginToken($request) {
$loginToken = $request->headers->get('X-Login-Token');
if (isset($loginToken) && $loginToken !== "") {
return TRUE;
}
else {
return FALSE;
}
}
private function getHeader($request, $headerName) {
$headerValue = $request->headers->get($headerName);
if (isset($headerValue) && $headerValue !== "") {
return $headerValue;
}
else {
return FALSE;
}
}
/**
* {@inheritdoc}
*/
/* public function authenticate(Request $request) {
$consumer_ip = $request->getClientIp();
$ips = ['192.168.0.59:80'];
// if (in_array($consumer_ip, $ips)) {
if ($request->query->get('name') === 'haris') {
// Return Anonymous user.
print_r($request->query->get('name'));
return true;
// return $this->entityTypeManager->getStorage('user')->load(1);
}
else {
throw new AccessDeniedHttpException();
}
} */
public function authenticate(Request $request) {
/* $code = filter_input(INPUT_GET, 'code');
if (empty($code) || !$this->client) {
return new RedirectResponse('/');
}
try {
$this->client->authenticate($code);
}
catch (\Exception $e) {
return new RedirectResponse('/');
}
$plus = new Google_Service_Oauth2($this->client);
$userinfo = $plus->userinfo->get();
$user_email = $userinfo['email']; */
drupal_set_message("hello");
$user_email = 'haris.rnd@gmail.com';
$user = user_load_by_mail($user_email);
/* if (!$user) {
$user_name = $userinfo['name'];
$user_picture = $userinfo['picture'];
try {
$user = User::create([
'name' => $user_name,
'mail' => $user_email,
'status' => 1,
'picture' => $user_picture,
]);
// hook_google_oauth_create_user_alter($user, $userinfo);
\Drupal::moduleHandler()->alter('google_oauth_create_user', $user, $userinfo);
$user->save();
}
catch (\Exception $e) {
return new RedirectResponse('/');
}
} */
user_login_finalize($user);
// return new RedirectResponse('http://example.com');
return($user);
// return $this->redirect('<front>');
}
/**
* {@inheritdoc}
*/
public function cleanup(Request $request) {}
/**
* {@inheritdoc}
*/
/* public function handleException(GetResponseForExceptionEvent $event) {
$exception = $event->getException();
if ($exception instanceof AccessDeniedHttpException) {
$event->setException(
new UnauthorizedHttpException('Invalid consumer origin.', $exception)
);
return TRUE;
}
return FALSE;
} */
}
<?php
namespace Drupal\oauthost\Tests;
use Drupal\Core\Url;
use Drupal\simpletest\WebTestBase;
/**
* Simple test to ensure that main page loads with module enabled.
*
* @group oauthost
*/
class LoadTest extends WebTestBase{
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['oauthost'];
/**
* A user with permission to administer site configuration.
*
* @var \Drupal\user\UserInterface
*/
protected $user;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->user = $this->drupalCreateUser(['administer site configuration']);
$this->drupalLogin($this->user);
}
/**
* Tests that the home page loads with a 200 response.
*/
public function testLoad() {
$this->drupalGet(Url::fromRoute('<front>'));
$this->assertResponse(200);
}
}
<!-- Add you custom twig html here -->
\ No newline at end of file
import {Http, Headers} from '@angular/http';
import {Http, Headers, RequestOptions} from '@angular/http';
import {Injectable} from '@angular/core';
import {Observable} from "rxjs/Observable";
import 'rxjs/add/operator/map';
......@@ -15,8 +15,19 @@ export class HelperDataService {
constructor(private http: Http) {
};
getCourseFields() {
let headers = new Headers({
//"Authorization": "Basic cmVzdHVzZXI6czNjckV0MFAwdWwwJA==", // encoded user:pass
// "Authorization": "Basic bmthdHNhb3Vub3M6emVtcmFpbWU=",
"Content-Type": "application/json",
// "Content-Type": "text/plain", // try to skip preflight
//"X-CSRF-Token": "hVtACDJjFRSyE4bgGJENHbXY0B9yNhF71Fw-cYHSDNY"
//"X-CSRF-Token": "fj1QtF_Z_p6kE19EdCnN08zoSjVfcT4Up-ciW6I0IG8"
"X-CSRF-Token": "LU92FaWYfImfZxfldkF5eVnssdHoV7Aa9fg8K1bWYUc",
"X-Auth-Token": "bourboutsala"
});
let options = new RequestOptions({ headers: headers });
return new Promise((resolve, reject) => {
this.http.get(`${AppSettings.API_ENDPOINT}/coursefields/list`)
this.http.get(`${AppSettings.API_ENDPOINT}/coursefields/list`, options)
.map(response => <ICourseField[]>response.json())
.subscribe(data => {
resolve(data);
......@@ -45,8 +56,19 @@ export class HelperDataService {
};
getRegionsWithSchools() {
let headers = new Headers({
//"Authorization": "Basic cmVzdHVzZXI6czNjckV0MFAwdWwwJA==", // encoded user:pass
// "Authorization": "Basic bmthdHNhb3Vub3M6emVtcmFpbWU=",
"Content-Type": "application/json",
// "Content-Type": "text/plain", // try to skip preflight
//"X-CSRF-Token": "hVtACDJjFRSyE4bgGJENHbXY0B9yNhF71Fw-cYHSDNY"
//"X-CSRF-Token": "fj1QtF_Z_p6kE19EdCnN08zoSjVfcT4Up-ciW6I0IG8"
"X-CSRF-Token": "LU92FaWYfImfZxfldkF5eVnssdHoV7Aa9fg8K1bWYUc",
"X-Auth-Token": "bourboutsal"
});
let options = new RequestOptions({ headers: headers });
return new Promise((resolve, reject) => {
this.http.get(`${AppSettings.API_ENDPOINT}/regions/list`)
this.http.get(`${AppSettings.API_ENDPOINT}/regions/list`, options)
.map(response => response.json())
.subscribe(data => {
// console.log(data);
......
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