This repository has been archived by the owner on May 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Auth.php
69 lines (53 loc) · 2.02 KB
/
Auth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php namespace ProcessWire;
require_once __DIR__ . "/vendor/autoload.php";
require_once __DIR__ . "/RestApiHelper.php";
use \Firebase\JWT\JWT;
class Auth
{
public static function preflight() {
return;
}
public static function createJwt()
{
if(wire('user')->isGuest()) {
throw new \Exception('user is not logged in', 401);
}
if(!isset(wire('modules')->RestApi->jwtSecret)) {
throw new \Exception('No JWT secret defined. Please adjust settings in Module RestApi', 500);
}
$issuedAt = time();
$notBefore = $issuedAt;
$expire = $notBefore + wire('config')->sessionExpireSeconds;
$serverName = wire('config')->httpHost;
$token = array(
"iss" => $serverName, // issuer
"aud" => $serverName, // audience
"iat" => $issuedAt, // issued at
"nbf" => $notBefore, // valid not before
"exp" => $expire, // token expire time
"userId" => wire('user')->id
);
$jwt = JWT::encode($token, wire('modules')->RestApi->jwtSecret);
$response = new \StdClass();
$response->jwt = $jwt;
return $response;
}
public static function login($data) {
RestApiHelper::checkAndSanitizeRequiredParameters($data, ['username|selectorValue', 'password|string']);
$user = wire('users')->get($data->username);
// if(!$user->id) throw new \Exception("User with username: $data->username not found", 404);
// prevent username sniffing by just throwing a general exception:
if(!$user->id) throw new \Exception("Login not successful", 401);
$loggedIn = wire('session')->login($data->username, $data->password);
if($loggedIn) {
if(wire('modules')->RestApi->authMethod === 'session') return 'logged in: ' . wire('user')->name;
if(wire('modules')->RestApi->authMethod === 'jwt') return self::createJWT();
}
else throw new \Exception("Login not successful", 401);
}
public static function logout() {
$username = wire('user')->name;
wire('session')->logout(wire('user'));
return "logged out: $username";
}
}