-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLINEStrategy.php
More file actions
174 lines (149 loc) · 4.23 KB
/
LINEStrategy.php
File metadata and controls
174 lines (149 loc) · 4.23 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* LINE strategy for Opauth
* based on https://developers.line.me/web-api/integrating-web-login-v2
*
* More information on Opauth: http://opauth.org
*
* @copyright Copyright © 2017 Yuzuru Suzuki (https://github.com/YuzuruS/)
* @link http://opauth.org
* @package Opauth.LINEStrategy
* @license MIT License
*/
/**
* LINE strategy for Opauth
* based on https://developers.line.me/web-api/integrating-web-login-v2
*
* @package Opauth.LINE
*/
class LINEStrategy extends OpauthStrategy{
/**
* Compulsory config keys, listed as unassociative arrays
*/
public $expects = array('channel_id', 'channel_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}oauth2callback',
);
/**
* Provider Name
*/
private $_provider = 'LINE';
/**
* Auth request
*/
public function request(){
$url = 'https://access.line.me/dialog/oauth/weblogin';
if (!session_id()) {
session_start();
}
$_SESSION['_opauth_line_state'] = sha1(time());
$params = array(
'client_id' => $this->strategy['channel_id'],
'redirect_uri' => $this->strategy['redirect_uri'],
'response_type' => 'code',
'state' => $_SESSION['_opauth_line_state']
);
$this->clientGet($url, $params);
}
/**
* Internal callback, after LINE's OAuth
*/
public function oauth2callback(){
if (empty($_GET['code']) || empty($_GET['state'])) {
$error = array(
'provider' => $this->_provider,
'code' => 'oauth2callback_error',
'message' => $_GET['errorMessage'],
'raw' => $_GET
);
$this->errorCallback($error);
return;
}
$state = $_GET['state'];
if (!session_id()) {
session_start();
}
$session = $_SESSION['_opauth_line_state'];
unset($_SESSION['_opauth_line_state']);
if ($session !== $state) {
$error = array(
'provider' => $this->_provider,
'code' => 'csrf_token_error',
'message' => 'Can\'t verify CSRF token authenticity',
'raw' => $_GET
);
$this->errorCallback($error);
return;
}
$code = $_GET['code'];
$url = 'https://api.line.me/v2/oauth/accessToken';
$params = array(
'code' => $code,
'client_id' => $this->strategy['channel_id'],
'client_secret' => $this->strategy['channel_secret'],
'redirect_uri' => $this->strategy['redirect_uri'],
'grant_type' => 'authorization_code'
);
$response = $this->serverPost($url, $params, null, $headers);
$results = json_decode($response);
if (empty($results) || empty($results->access_token)) {
$error = array(
'provider' => $this->_provider,
'code' => 'access_token_error',
'message' => 'Failed when attempting to obtain access token',
'raw' => array(
'response' => $response,
'headers' => $headers
)
);
$this->errorCallback($error);
return;
}
$userinfo = $this->_userinfo($results->access_token);
$this->auth = array(
'uid' => $userinfo['userId'],
'info' => array(),
'credentials' => array(
'token' => $results->access_token,
'expires' => date('c', time() + $results->expires_in)
),
'raw' => $userinfo
);
if (!empty($results->refresh_token))
{
$this->auth['credentials']['refresh_token'] = $results->refresh_token;
}
$this->mapProfile($userinfo, 'displayName', 'info.name');
$this->mapProfile($userinfo, 'pictureUrl', 'info.image');
$this->mapProfile($userinfo, 'statusMessage', 'info.message');
$this->callback();
}
/**
* Queries LINE API for user info
*
* @param string $access_token
* @return array Parsed JSON results
*/
private function _userinfo($access_token){
$option = array('http' => array('header' => 'Authorization: Bearer '.$access_token));
$userinfo = $this->serverGet('https://api.line.me/v2/profile', array(), $option, $headers);
if (empty($userinfo)) {
$error = array(
'provider' => $this->_provider,
'code' => 'userinfo_error',
'message' => "Failed when attempting to query {$this->_provider} API for user information",
'raw' => array(
'response' => $userinfo,
'headers' => $headers
)
);
$this->errorCallback($error);
return;
}
return $this->recursiveGetObjectVars(json_decode($userinfo));
}
}