-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase.php
90 lines (81 loc) · 2.24 KB
/
base.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
* @package GoogleAuth
* @author Alan Hardman <[email protected]>
* @version 1.0.0
*/
namespace Plugin\GoogleAuth;
class Base extends \Plugin {
/**
* Initialize the plugin, adding authentication hooks and routes
*/
public function _load() {
$f3 = \Base::instance();
$f3->route("POST /googleauth", "Plugin\GoogleAuth\Base->auth");
$this->_hook("render.login.after_login", array($this, "loginButton"));
$this->_hook("render.login.after_footer", array($this, "loginFooter"));
}
/**
* Check if plugin is installed
* @return bool
*/
public function _installed() {
return !!\Base::instance()->get("site.plugins.googleauth.client_id");
}
/**
* Generate page for admin panel
*/
public function _admin() {
$f3 = \Base::instance();
if($f3->get("POST.client_id")) {
\Model\Config::setVal("site.plugins.googleauth.client_id", $f3->get("POST.client_id"));
}
echo \Helper\View::instance()->render("googleauth/view/admin.html");
}
/**
* Display Google login button on login page
* @return
*/
public function loginButton() {
if($this->_installed()) {
echo \Template::instance()->render("googleauth/view/loginbutton.html");
}
}
/**
* Output authentication JS on login page
* @return
*/
public function loginFooter() {
if($this->_installed()) {
echo \Template::instance()->render("googleauth/view/loginfooter.html");
}
}
/**
* POST /googleauth
* Authenticate user with Google OpenID token
*
* @param \Base $f3
*/
public function auth($f3) {
$token = $f3->get("POST.token");
$url = "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=";
$result = \Web::instance()->request($url . urlencode($token));
$obj = json_decode($result['body']);
if(!headers_sent()) {
header("Content-type: application/json");
}
if($obj->email) {
$user = new \Model\User;
$user->load(array("email = ?", $obj->email));
if($user->id) {
$session = new \Model\Session($user->id);
$session->setCurrent();
echo json_encode(array('error' => null));
} else {
echo json_encode(array('error' => 'The email address "' . $obj->email . '" is not registered on this site.'));
}
} else {
echo json_encode(array('error' => 'An unknown error occurred logging in.'));
}
}
}