Completed
Pull Request — master (#29)
by
unknown
03:32
created

FlipsideAuthenticator::getUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Auth\OAuth2;
3
4
class FlipsideAuthenticator extends OAuth2Authenticator
5
{
6
    /** An instance of the Settings class */
7
    protected $settings;
8
    /** The prfiles app URL */
9
    protected $profilesUrl
10
11
    public function __construct($params)
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_PUBLIC, expecting ',' or ';'
Loading history...
12
    {
13
        parent::__construct($params);
14
        $this->settings = \Settings::getInstance();
15
        $this->profilesUrl = $this->settings->getGlobalSetting('profiles_url', 'https://profiles.burningflipside.com/');
16
    }
17
18
    public function getHostName()
19
    {
20
        return 'burningflipside.com';
21
    }
22
23
    public function getAuthorizationUrl()
24
    {
25
        return $this->profilesUrl.'/OAUTH2/authorize.php?client_id=test&redirect_uri='.urlencode($this->redirect_uri).'&scope=user';
26
    }
27
28
    public function getAccessTokenUrl()
29
    {
30
        return $this->profilesUrl.'/OAUTH2/token.php?client_id=test&redirect_uri='.urlencode($this->redirect_uri);
31
    }
32
33
    public function getUserFromToken($token)
34
    {
35
        if($token === false)
36
        {
37
            $token = \FlipSession::getVar('OAuthToken');
38
        }
39
        $resp = \Httpful\Request::get($this->profilesUrl.'/api/v1/users/me')->addHeader('Authorization', 'token '.$token['access_token'])->send();
40
        $data = array('extended'=>$resp->body);
41
        $user = new \Auth\FlipsideAPIUser($data);
42
        $user->addLoginProvider($this->getHostName());
43
        return $user;
44
    }
45
46
    public function login($username, $password)
47
    {
48
        $resp = \Httpful\Request::post($this->profilesUrl.'/api/v1/login?username='.urlencode($username).'&password='.urlencode($password))->send();
49
        if($resp->hasErrors())
50
        {
51
            return false;
52
        }
53
        $this->user = $resp->body->extended;
54
        $this->user->userPassword = $password;
55
        return array('res'=>true, 'extended'=>$this->user);
56
    }
57
58
    public function isLoggedIn($data)
59
    {
60
        if(isset($this->user))
61
        {
62
            return true;
63
        }
64
        if(isset($data['res']))
65
        {
66
            return $data['res'];
67
        }
68
        return false;
69
    }
70
71
    public function getUser($data)
72
    {
73
        return new \Auth\FlipsideAPIUser($data);
74
    }
75
}
76