-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrublon.php
93 lines (74 loc) · 3.07 KB
/
rublon.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
91
92
93
<?php
use Rublon\Rublon as RublonSdk;
use Rublon\RublonCallback;
use Rublon\Core\Exceptions\Api\UserBypassedException;
class rublon extends rcube_plugin {
private $rcmail;
private $rcubeUrl;
function init()
{
$this->rcmail = rcmail::get_instance();
$this->load_config();
$this->add_hook('send_page', array($this, 'rublon_check'));
$this->add_hook('login_after', array($this, 'login_after'));
$this->register_action('plugin.rublon-callback', array($this, 'rublon_callback'));
$this->rcubeUrl = $this->rcmail->config->get('rcubeUrl',$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST']).'/?_action=plugin.rublon-callback';
}
function login_after($args) {
$_SESSION['_Rublon_2FA'] = false;
$rublon = new RublonSdk(
$this->rcmail->config->get('client'),
$this->rcmail->config->get('secret'),
$this->rcmail->config->get('rublonApi')
);
try {
$url = $rublon->auth(
$this->rcubeUrl,
$this->rcmail->user->data['username'], // App User ID
$this->rcmail->user->data['username']// User email
);
if (!empty($url)) { // User protection is active
// Redirect the user's web browser to Rublon servers to verify the protection:
header('Location: ' . $url);
exit;
}
} catch (UserBypassedException $e) {
$_SESSION['_Rublon_2FA'] = true;
$this->rcmail->output->redirect(array('_task' => 'mail', 'action' => null));
}
}
function rublon_callback() {
$rublon = new RublonSdk(
$this->rcmail->config->get('client'),
$this->rcmail->config->get('secret'),
$this->rcmail->config->get('rublonApi')
);
try {
$callback = new RublonCallback($rublon);
$callback->call(
$successHandler = function($appUserId, RublonCallback $callback) {
$_SESSION['_Rublon_2FA'] = true;
},
$cancelHandler = function(RublonCallback $callback) {
$_SESSION['_Rublon_2FA'] = false;
$this->rcmail->output->redirect(array('_task' => 'login', 'action' => null));
}
);
$this->rcmail->output->redirect(array('_task' => 'mail', 'action' => null));
} catch (UserBypassedException $e) {
$this->rcmail->output->redirect(array('_task' => 'mail', 'action' => null));
} catch (RublonException $e) {
die($e->getMessage());
}
$this->rcmail->output->redirect(array('_task' => 'mail', 'action' => null));
}
function rublon_check($args) {
if ($_SESSION['_Rublon_2FA']) {
return $args;
} else if ($this->rcmail->action == 'plugin.rublon-callback' || $this->rcmail->task == 'login') {
return $args;
}
$this->rcmail->output->redirect(array('_task' => 'login', 'action' => null));
return $args;
}
}