|
1
|
|
|
<?php |
|
2
|
|
|
namespace Flipside\Auth\OAuth2; |
|
3
|
|
|
|
|
4
|
|
|
class FlipsideAuthenticator extends OAuth2Authenticator |
|
5
|
|
|
{ |
|
6
|
|
|
private $apiUrl; |
|
7
|
|
|
private $oauthUrl; |
|
8
|
|
|
|
|
9
|
|
|
public function __construct($params) |
|
10
|
|
|
{ |
|
11
|
|
|
parent::__construct($params); |
|
12
|
|
|
if(isset($params['api_url'])) |
|
13
|
|
|
{ |
|
14
|
|
|
$this->apiUrl = $params['api_url']; |
|
15
|
|
|
} |
|
16
|
|
|
else |
|
17
|
|
|
{ |
|
18
|
|
|
throw new \Exception('Incorrectly configured! Missing api_url parameter.'); |
|
19
|
|
|
} |
|
20
|
|
|
if(isset($params['oauth_url'])) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->oauthUrl = $params['oauth_url']; |
|
23
|
|
|
} |
|
24
|
|
|
else |
|
25
|
|
|
{ |
|
26
|
|
|
throw new \Exception('Incorrectly configured! Missing oauth_url parameter.'); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function getHostName() |
|
31
|
|
|
{ |
|
32
|
|
|
return 'burningflipside.com'; |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function getAuthorizationUrl() |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->oauthUrl.'/authorize.php?client_id=test&redirect_uri='.urlencode($this->redirect_uri).'&scope=user'; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getAccessTokenUrl() |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->oauthUrl.'/token.php?client_id=test&redirect_uri='.urlencode($this->redirect_uri); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getUserFromToken($token) |
|
46
|
|
|
{ |
|
47
|
|
|
if($token === false) |
|
48
|
|
|
{ |
|
49
|
|
|
$token = \FlipSession::getVar('OAuthToken'); |
|
50
|
|
|
} |
|
51
|
|
|
$resp = \Httpful\Request::get($this->apiUrl.'/users/me')->addHeader('Authorization', 'token '.$token['access_token'])->send(); |
|
52
|
|
|
$data = array('extended'=>$resp->body); |
|
53
|
|
|
$user = new \Auth\FlipsideAPIUser($data); |
|
54
|
|
|
$user->addLoginProvider($this->getHostName()); |
|
55
|
|
|
return $user; |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function login($username, $password) |
|
59
|
|
|
{ |
|
60
|
|
|
$resp = \Httpful\Request::post($this->apiUrl.'/login?username='.urlencode($username).'&password='.urlencode($password))->send(); |
|
61
|
|
|
if($resp->hasErrors()) |
|
62
|
|
|
{ |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
$this->user = $resp->body->extended; |
|
|
|
|
|
|
66
|
|
|
$this->user->userPassword = $password; |
|
67
|
|
|
return array('res'=>true, 'extended'=>$this->user); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function isLoggedIn($data) |
|
71
|
|
|
{ |
|
72
|
|
|
if(isset($this->user)) |
|
73
|
|
|
{ |
|
74
|
|
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
if(isset($data['res'])) |
|
77
|
|
|
{ |
|
78
|
|
|
return $data['res']; |
|
79
|
|
|
} |
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function getUser($data) |
|
84
|
|
|
{ |
|
85
|
|
|
return new \Auth\FlipsideAPIUser($data, $this->apiUrl); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.