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

Auth/OAuth2/class.GitHubAuthenticator.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 GitHubAuthenticator extends OAuth2Authenticator
5
{
6
    protected $app_id;
7
    protected $app_secret;
8
9
    public function __construct($params)
10
    {
11
        parent::__construct($params);
12
        $this->app_id = $params['app_id'];
13
        $this->app_secret = $params['app_secret'];
14
    }
15
16
    public function getHostName()
17
    {
18
        return 'github.com';
19
    }
20
21
    public function getAuthorizationUrl()
22
    {
23
        return 'https://github.com/login/oauth/authorize?client_id='.$this->app_id.'&redirect_uri='.urlencode($this->redirect_uri).'&scope=user';
24
    }
25
26
    public function getAccessTokenUrl()
27
    {
28
        return 'https://github.com/login/oauth/access_token?client_id='.$this->app_id.'&client_secret='.$this->app_secret.'&redirect_uri='.urlencode($this->redirect_uri);
29
    }
30
31
    public function getUserFromToken($token)
32
    {
33
        if($token === false)
34
        {
35
            $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...
36
        }
37
        $resp = \Httpful\Request::get('https://api.github.com/user')->addHeader('Authorization', 'token '.$token['access_token'])->send();
38
        $github_user = $resp->body;
39
        $user = new \Auth\PendingUser();
40
        if(isset($github_user->name))
41
        {
42
            $name = explode(' ', $github_user->name);
43
            $user->givenName = $name[0];
44
            $user->sn = $name[1];
45
        }
46
        $resp = \Httpful\Request::get('https://api.github.com/user/emails')->addHeader('Authorization', 'token '.$token['access_token'])->send();
47
        $user->mail = $resp->body[0]->email;
48
        $user->addLoginProvider($this->getHostName());
49
        return $user;
50
    }
51
}
52