-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.aixoauth.php
More file actions
160 lines (134 loc) · 4.27 KB
/
class.aixoauth.php
File metadata and controls
160 lines (134 loc) · 4.27 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
require('aixoauth2.php');
class Aixoauth
{
private static $initiated = false;
private static $instance;
public static function init() {
if ( ! self::$initiated ) {
self::$instance = new Aixoauth();
}
}
/**
* Initializes WordPress hooks
*/
function __construct() {
self::$initiated = true;
add_filter('query_vars', array($this, 'qvar_triggers'));
add_action('template_redirect', array($this, 'qvar_handler'));
}
function qvar_triggers($vars) {
$vars[] = 'login';
$vars[] = 'code';
return $vars;
}
function getCredentials()
{
$options = get_option('addressixoauth');
$this->clientid = $options['clientid'];
$this->secret = $options['secret'];
$this->redirecturl = get_bloginfo('url');
}
function qvar_handler() {
if (get_query_var('login')) {
$this->getCredentials();
$this->callLogin();
}
else if (get_query_var('code')) {
$this->getCredentials();
$this->callback();
}
}
function getAccessToken()
{
$tokens = get_user_meta(get_current_user_id(),'addressix_access');
return $tokens[0];
}
function callLogin() {
global $wpdb;
$state = rand(1001,99999);
// TODO: $_SESSION['aixauthstate'] = $state;
if (wp_redirect('https://www.addressix.com/oauth2/v1/fdp.ch/authorize?response_type=code&redirect_uri='.urlencode($this->redirecturl).'&client_id='.$this->clientid.'&state=' . $state)) {
exit;
}
}
function getUserForAddressixId($addressixid)
{
global $wpdb;
$usermeta_table = $wpdb->usermeta;
$sql = "SELECT $usermeta_table.user_id FROM $usermeta_table WHERE $usermeta_table.meta_key='wpoa_identity' AND $usermeta_table.meta_value LIKE '%Addressix|" .$addressixid . "%'";
$rs = $wpdb->get_var($sql);
$user = get_user_by('id', $rs);
return $user;
}
function linkAccount($user_id, $addressixid)
{
add_user_meta($user_id, 'wpoa_identity', 'Addressix|' . $addressixid . '|' . time());
}
function register($person) {
$user_login = $person->username;
$user_login = preg_replace('#[<>"\'%;()&\s\\\\]|\\.\\./#', "", $user_login);
$user_login = trim (trim($user_login), '.');
$pw = wp_generate_password();
$user_id = wp_insert_user(
array(
'user_login' => $person->username,
'user_nicename' => trim($person->firstname . ' ' . $person->surname),
'user_email' => $person->primaryemail,
'display_name' => $person->firstname,
'nickname' => $person->firstname,
'first_name' => $person->firstname,
'last_name' => $person->surname,
'password' => $pw
)
);
if (is_wp_error($user_id)) {
error_log($user_id->get_error_message());
return 0;
}
$this->linkAccount($user_id, $person->addressixid);
$creds = array();
$creds['user_login'] = $person->username;
$creds['user_password'] = $pw;
$creds['remember'] = true;
$user = wp_signon($creds, false);
return $user_id;
}
function callback() {
$client = new OAuth2Client($this->clientid, $this->secret);
$params = array('code' => $_GET['code'],
'redirect_uri' => $this->redirecturl);
$response = $client->getAccessToken('authorization_code', $params);
if ($response->code==200) {
$access_token = $response->body->access_token;
$client->setAccessToken($access_token);
// error_log('access-code: ' . $access_token);
$personresponse = $client->fetch('https://www.addressix.com/api/people/v1/me');
if ($personresponse->code==200) {
$person = $personresponse->body;
// error_log('aixid: ' . $person->addressixid);
$matched_user = $this->getUserForAddressixId($person->addressixid);
if ($matched_user)
{
// a user matched, log him in
$user_id = $matched_user->ID;
$user_login = $matched_user->user_login;
wp_set_current_user($user_id, $user_login);
wp_set_auth_cookie($user_id);
do_action('wp_login', $user_login, $matched_user);
update_user_meta($user_id, 'addressix_access', $access_token);
wp_redirect($this->redirecturl);
}
else {
error_log('new user');
// user does not exist yet, create him
$user_id = $this->register($person);
add_user_meta($user_id, 'addressix_access', $access_token);
}
}
}
else {
error_log('aixlogin-callback: error-response: ' .$response->code);
}
}
}