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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
<?php
namespace Controller;
/**
* OAuth controller
*
* @package controller
* @author Frederic Guillot
*/
class Oauth extends Base
{
/**
* Link or authenticate a Google account
*
* @access public
*/
public function google()
{
$this->step1('google');
}
/**
* Link or authenticate a Github account
*
* @access public
*/
public function github()
{
$this->step1('github');
}
/**
* Unlink external account
*
* @access public
*/
public function unlink($backend = '')
{
$backend = $this->request->getStringParam('backend', $backend);
$this->checkCSRFParam();
if ($this->authentication->backend($backend)->unlink($this->userSession->getId())) {
$this->session->flash(t('Your external account is not linked anymore to your profile.'));
}
else {
$this->session->flashError(t('Unable to unlink your external account.'));
}
$this->response->redirect($this->helper->url->to('user', 'external', array('user_id' => $this->userSession->getId())));
}
/**
* Redirect to the provider if no code received
*
* @access private
*/
private function step1($backend)
{
$code = $this->request->getStringParam('code');
if (! empty($code)) {
$this->step2($backend, $code);
}
else {
$this->response->redirect($this->authentication->backend($backend)->getService()->getAuthorizationUrl());
}
}
/**
* Link or authenticate the user
*
* @access private
*/
private function step2($backend, $code)
{
$profile = $this->authentication->backend($backend)->getProfile($code);
if ($this->userSession->isLogged()) {
$this->link($backend, $profile);
}
$this->authenticate($backend, $profile);
}
/**
* Link the account
*
* @access private
*/
private function link($backend, $profile)
{
if (empty($profile)) {
$this->session->flashError(t('External authentication failed'));
}
else {
$this->session->flash(t('Your external account is linked to your profile successfully.'));
$this->authentication->backend($backend)->updateUser($this->userSession->getId(), $profile);
}
$this->response->redirect($this->helper->url->to('user', 'external', array('user_id' => $this->userSession->getId())));
}
/**
* Authenticate the account
*
* @access private
*/
private function authenticate($backend, $profile)
{
if (! empty($profile) && $this->authentication->backend($backend)->authenticate($profile['id'])) {
$this->response->redirect($this->helper->url->to('app', 'index'));
}
else {
$this->response->html($this->template->layout('auth/index', array(
'errors' => array('login' => t('External authentication failed')),
'values' => array(),
'no_layout' => true,
'redirect_query' => '',
'title' => t('Login')
)));
}
}
}
|