|
1
|
|
|
<?php |
|
2
|
|
|
namespace Auth\OAuth2; |
|
3
|
|
|
|
|
4
|
|
|
class FlipsideAuthenticator extends OAuth2Authenticator |
|
5
|
|
|
{ |
|
6
|
|
|
private $apiUrl; |
|
7
|
|
|
private $oauthUrl; |
|
8
|
|
|
|
|
9
|
|
|
public function __construct($params) |
|
10
|
|
|
{ |
|
11
|
|
|
$this->settings = \Settings::getInstance(); |
|
|
|
|
|
|
12
|
|
|
$this->profilesUrl = $this->settings->getGlobalSetting('profiles_url', 'https://profiles.burningflipside.com/'); |
|
|
|
|
|
|
13
|
|
|
$this->apiUrl = $this->profilesUrl.'api/v1'; |
|
14
|
|
|
$this->oauthUrl = $this->profilesUrl.'OAUTH2'; |
|
15
|
|
|
|
|
16
|
|
|
parent::__construct($params); |
|
17
|
|
|
if(isset($params['api_url'])) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->apiUrl = $params['api_url']; |
|
20
|
|
|
} |
|
21
|
|
|
if(isset($params['oauth_url'])) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->oauthUrl = $params['oauth_url']; |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function getHostName() |
|
28
|
|
|
{ |
|
29
|
|
|
return 'burningflipside.com'; |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function getAuthorizationUrl() |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->oauthUrl.'/authorize.php?client_id=test&redirect_uri='.urlencode($this->redirect_uri).'&scope=user'; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getAccessTokenUrl() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->oauthUrl.'/token.php?client_id=test&redirect_uri='.urlencode($this->redirect_uri); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function getUserFromToken($token) |
|
43
|
|
|
{ |
|
44
|
|
|
if($token === false) |
|
45
|
|
|
{ |
|
46
|
|
|
$token = \FlipSession::getVar('OAuthToken'); |
|
47
|
|
|
} |
|
48
|
|
|
$resp = \Httpful\Request::get($this->apiUrl.'/users/me')->addHeader('Authorization', 'token '.$token['access_token'])->send(); |
|
49
|
|
|
$data = array('extended'=>$resp->body); |
|
50
|
|
|
$user = new \Auth\FlipsideAPIUser($data); |
|
|
|
|
|
|
51
|
|
|
$user->addLoginProvider($this->getHostName()); |
|
52
|
|
|
return $user; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function login($username, $password) |
|
56
|
|
|
{ |
|
57
|
|
|
$resp = \Httpful\Request::post($this->apiUrl.'/login?username='.urlencode($username).'&password='.urlencode($password))->send(); |
|
58
|
|
|
if($resp->hasErrors()) |
|
59
|
|
|
{ |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
$this->user = $resp->body->extended; |
|
|
|
|
|
|
63
|
|
|
$this->user->userPassword = $password; |
|
64
|
|
|
return array('res'=>true, 'extended'=>$this->user); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function isLoggedIn($data) |
|
68
|
|
|
{ |
|
69
|
|
|
if(isset($this->user)) |
|
70
|
|
|
{ |
|
71
|
|
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
if(isset($data['res'])) |
|
74
|
|
|
{ |
|
75
|
|
|
return $data['res']; |
|
76
|
|
|
} |
|
77
|
|
|
return false; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getUser($data) |
|
81
|
|
|
{ |
|
82
|
|
|
return new \Auth\FlipsideAPIUser($data, $this->apiUrl); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: