This repository was archived by the owner on Sep 23, 2020. It is now read-only.
forked from georgiclient/opauth-probook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProbookStrategy.php
154 lines (132 loc) · 4.33 KB
/
ProbookStrategy.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
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
<?php
/**
* Probook strategy for Opauth
*
* More information on Opauth: http://opauth.org
*
* @copyright Copyright © 2014 Georgi Georgiev [email protected]
* @link http://opauth.org
* @package Opauth.ProbookStrategy
* @license MIT License
*/
class ProbookStrategy extends OpauthStrategy{
/**
* Compulsory config keys, listed as unassociative arrays
* eg. array('client_id', 'client_secret');
*/
public $expects = array('client_id', 'client_secret');
/**
* Optional config keys with respective default values, listed as associative arrays
* eg. array('scope' => 'email');
*/
public $defaults = array(
'redirect_uri' => '{complete_url_to_strategy}int_callback',
'response_type' => 'code',
'scope' => 'basic',
);
/**
* Auth request
*/
public function request(){
$url = 'https://probook.bg/auth/authorize';
$params = array(
'client_id' => $this->strategy['client_id'],
'redirect_uri' => $this->strategy['redirect_uri'],
'tmpl' => 4
);
if (!empty($this->strategy['scope'])) $params['scope'] = $this->strategy['scope'];
if (!empty($this->strategy['state'])) $params['state'] = $this->strategy['state'];
if (!empty($this->strategy['response_type'])) $params['response_type'] = $this->strategy['response_type'];
if (!empty($this->strategy['display'])) $params['display'] = $this->strategy['display'];
if (!empty($this->strategy['auth_type'])) $params['auth_type'] = $this->strategy['auth_type'];
$this->clientGet($url, $params);
}
/**
* Internal callback, after Probook's OAuth
*/
public function int_callback(){
if (array_key_exists('code', $_GET) && !empty($_GET['code'])){
$url = 'http://probook.bg/auth/token';
$params = array(
'grant_type' => 'authorization_code',
'client_id' => $this->strategy['client_id'],
'client_secret' => $this->strategy['client_secret'],
'code' => trim($_GET['code']),
'tmpl' => 4,
'redirect_uri' => $this->strategy['redirect_uri'],
);
$response = $this->serverPost($url, $params, null);
$results = json_decode($response, true);
if (!empty($results) && !empty($results['access_token'])){
$me = $this->me($results['access_token']);
$this->auth = array(
'provider' => 'Probook',
'uid' => $me->user_id,
'info' => array(
'name' => $me->user_first_name." ".$me->user_last_name,
'image' => $me->user_image,
),
'credentials' => array(
'token' => $results['access_token'],
'expires' => date('c', time() + $results['expires_in'])
),
'raw' => $me
);
if (!empty($me->user_email)) $this->auth['info']['email'] = $me->user_email;
if (!empty($me->username)) $this->auth['info']['username'] = $me->username;
if (!empty($me->user_first_name)) $this->auth['info']['first_name'] = $me->user_first_name;
if (!empty($me->user_last_name)) $this->auth['info']['last_name'] = $me->user_last_name;
if (!empty($me->city_name)) $this->auth['info']['location'] = $me->city_name;
if (!empty($me->user_id)) $this->auth['info']['urls']['probook'] = "http://probook.bg/profile/".$me->user_id;
if (!empty($me->website)) $this->auth['info']['urls']['website'] = $me->website;
/**
* Missing optional info values
* - description
* - phone: not accessible via Probook Graph API
*/
$this->callback();
}
else{
$error = array(
'provider' => 'Probook',
'code' => 'access_token_error',
'message' => 'Failed when attempting to obtain access token',
'raw' => $headers
);
$this->errorCallback($error);
}
}
else{
$error = array(
'provider' => 'Probook',
'code' => $_GET['error'],
'message' => $_GET['error_description'],
'raw' => $_GET
);
$this->errorCallback($error);
}
}
/**
* Queries Probook Graph API for user info
*
* @param string $access_token
* @return array Parsed JSON results
*/
private function me($access_token){
$me = $this->serverGet('http://probook.bg/auth/me', array('oauth_token' => $access_token), null);
if (!empty($me)){
return json_decode($me);
} else{
$error = array(
'provider' => 'Probook',
'code' => 'me_error',
'message' => 'Failed when attempting to query for user information',
'raw' => array(
'response' => $me,
'headers' => null
)
);
$this->errorCallback($error);
}
}
}