Completed
Branch master (9efafe)
by Patrick
03:38
created

Auth/OAuth2/class.FlipsideAuthenticator.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Auth\OAuth2;
3
4
class FlipsideAuthenticator extends OAuth2Authenticator
5
{
6
    public function __construct($params)
7
    {
8
        parent::__construct($params);
9
    }
10
11
    public function getHostName()
12
    {
13
        return 'burningflipside.com';
14
    }
15
16
    public function getAuthorizationUrl()
17
    {
18
        return 'https://profiles.burningflipside.com/OAUTH2/authorize.php?client_id=test&redirect_uri='.urlencode($this->redirect_uri).'&scope=user';
19
    }
20
21
    public function getAccessTokenUrl()
22
    {
23
        return 'https://profiles.burningflipside.com/OAUTH2/token.php?client_id=test&redirect_uri='.urlencode($this->redirect_uri);
24
    }
25
26
    public function getUserFromToken($token)
27
    {
28
        if($token === false)
29
        {
30
            $token = \FlipSession::getVar('OAuthToken');
0 ignored issues
show
Consider using a different name than the parameter $token. This often makes code more readable.
Loading history...
31
        }
32
        $resp = \Httpful\Request::get('https://profiles.burningflipside.com/api/v1/users/me')->addHeader('Authorization', 'token '.$token['access_token'])->send();
33
        $data = array('extended'=>$resp->body);
34
        $user = new \Auth\FlipsideAPIUser($data);
35
        $user->addLoginProvider($this->getHostName());
36
        return $user;
37
    }
38
39
    public function login($username, $password)
40
    {
41
        $resp = \Httpful\Request::post('https://profiles.test.burningflipside.com/api/v1/login?username='.urlencode($username).'&password='.urlencode($password))->send();
42
        if($resp->hasErrors())
43
        {
44
            return false;
45
        }
46
        $this->user = $resp->body->extended;
47
        $this->user->userPassword = $password;
48
        return array('res'=>true, 'extended'=>$this->user);
49
    }
50
51
    public function isLoggedIn($data)
52
    {
53
        if(isset($this->user))
54
        {
55
            return true;
56
        }
57
        if(isset($data['res']))
58
        {
59
            return $data['res'];
60
        }
61
        return false;
62
    }
63
64
    public function getUser($data)
65
    {
66
        return new \Auth\FlipsideAPIUser($data);
67
    }
68
}
69