SSO material

pull/42/head
Ali Gajani 2017-09-16 03:40:15 +01:00
parent 7d20722326
commit 6d28713ff6
5 changed files with 171 additions and 36 deletions

View File

@ -1,42 +1,11 @@
#### How to launch with one command?
![](https://dt9ph4xofvj87.cloudfront.net/user/sites/shawacademy.com/themes/mytheme/images/logo/logo-284-50/png/regular.png)
## How to launch with one command?
* Step 1: `composer install`
* Step 2: `php -S localhost:9999 launch.php`
`launch.php` is a custom script that gives you a reproducable development environment.
##### By Ali Gajani
---
___
Last revision on 16/09/2017
@aligajani
![Flarum](http://flarum.org/img/logo.png)
**[Flarum](http://flarum.org) is free, open-source forum software** built with PHP and [Mithril.js](http://mithril.js.org). It is:
* **Simple**, with a responsive UI that is optimized for touch devices
* **Fast**, with a total JS payload size of ~130 KB gzipped
* **Extensible**, so you can tailor it to your use-case
![screenshot](http://flarum.org/img/screenshot.png)
## Installation
> **Flarum is currently in beta and should not be used in production.** It is being developed openly on GitHub. Check out the [Roadmap](http://flarum.org/roadmap) to follow along with our progress.
You must have SSH access to a server with **PHP 5.5+** and **MySQL 5.5+**, and install [Composer](https://getcomposer.org).
```
composer create-project flarum/flarum . --stability=beta
```
Read the [Installation Guide](http://flarum.org/docs/installation) for more information.
## Support
Refer to the [FAQ](http://flarum.org/docs/faq), [Documentation](http://flarum.org/docs), and ask questions on the [Community Forum](http://discuss.flarum.org) or [Gitter Chat](https://gitter.im/flarum/flarum).
## Contributing
Flarum is open-source and we would love your help building it! Please read the [Contributing Guide](https://github.com/flarum/flarum/blob/master/CONTRIBUTING.md) to learn how you can help.
## License
Copyright (c) 2015 Toby Zerner. Code released under the [MIT License](https://github.com/flarum/flarum/blob/master/LICENSE).

126
sso/Forum.php Normal file
View File

@ -0,0 +1,126 @@
<?php
class Forum
{
const REMEMBER_ME_KEY = 'flarum_remember';
private $config;
public function __construct()
{
$this->config = require __DIR__ . '/config.php';
}
/**
* Call this method after your user is successfully authenticated.
*
* @param $username
* @param $email
*/
public function login($username, $email)
{
$password = $this->createPassword($username);
$token = $this->getToken($username, $password);
if (empty($token)) {
$this->signup($username, $password, $email);
$token = $this->getToken($username, $password);
}
$this->setRememberMeCookie($token);
}
/**
* Call this method after you logged out your user.
*/
public function logout()
{
$this->removeRememberMeCookie();
}
/**
* Redirects a user back to the forum.
*/
public function redirectToForum()
{
header('Location: ' . $this->config['flarum_url']);
die();
}
private function createPassword($username)
{
return hash('sha256', $username . $this->config['password_token']);
}
private function getToken($username, $password)
{
$data = [
'identification' => $username,
'password' => $password,
'lifetime' => $this->getLifetimeInSeconds(),
];
$response = $this->sendPostRequest('/api/token', $data);
return isset($response['token']) ? $response['token'] : '';
}
private function signup($username, $password, $email)
{
$data = [
"data" => [
"type" => "users",
"attributes" => [
"username" => $username,
"password" => $password,
"email" => $email,
"avatarUrl" => "https://cyantificdsgn.files.wordpress.com/2009/11/spidey.png"
]
]
];
$response = $this->sendPostRequest('/api/users', $data);
return isset($response['data']['id']);
}
private function sendPostRequest($path, $data)
{
$data_string = json_encode($data);
$ch = curl_init($this->config['flarum_url'] . $path);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'Authorization: Token ' . $this->config['flarum_api_key'] . '; userId=1',
]
);
$result = curl_exec($ch);
return json_decode($result, true);
}
private function setRememberMeCookie($token)
{
$this->setCookie(self::REMEMBER_ME_KEY, $token, time() + $this->getLifetimeInSeconds());
}
private function removeRememberMeCookie()
{
unset($_COOKIE[self::REMEMBER_ME_KEY]);
$this->setCookie(self::REMEMBER_ME_KEY, '', time() - 10);
}
private function setCookie($key, $token, $time)
{
setcookie($key, $token, $time, '/', $this->config['root_domain']);
}
private function getLifetimeInSeconds()
{
return $this->config['lifetime_in_days'] * 60 * 60 * 24;
}
}

22
sso/auth.php Normal file
View File

@ -0,0 +1,22 @@
<?php
require_once __DIR__ . '/Forum.php';
$users = [
'user' => [
'password' => 'password',
'email' => 'test@mailinator.com',
],
];
$username = empty($_POST['username']) ? '' : $_POST['username'];
$password = empty($_POST['password']) ? '' : $_POST['password'];
if (isset($users[$username]) && $users[$username]['password'] === $password) {
$email = $users[$username]['email'];
$forum = new Forum();
$forum->login($username, $email);
$forum->redirectToForum();
} elseif (!empty($username) || !empty($password)) {
echo 'Login failed';
}

7
sso/index.php Normal file
View File

@ -0,0 +1,7 @@
<h1>Login</h1>
<form method="post" action="auth.php">
<input type="text" name="username" placeholder="Username">
<input type="password" name="password" placeholder="Password">
<button type="submit">Login</button>
</form>

11
sso/logout.php Normal file
View File

@ -0,0 +1,11 @@
<?php
require_once __DIR__ . '/Forum.php';
$forum = new Forum();
$forum->logout();
if ($_GET['forum']) {
$forum->redirectToForum();
}