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

Auth/OAuth2/class.GitLabAuthenticator.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 GitLabAuthenticator 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 'gitlab.com';
19
    }
20
21
    public function getAuthorizationUrl()
22
    {
23
        return 'https://gitlab.com/oauth/authorize?client_id='.$this->app_id.'&redirect_uri='.urlencode($this->redirect_uri).'&response_type=code';
24
    }
25
26
    public function getAccessTokenUrl()
27
    {
28
        return 'https://gitlab.com/oauth/token?client_id='.$this->app_id.'&client_secret='.$this->app_secret.'&grant_type=authorization_code&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://gitlab.com/api/v3/user')->addHeader('Authorization', 'Bearer '.$token->access_token)->send();
38
        $gitlab_user = $resp->body;
39
        $user = new \Auth\PendingUser();
40
        $user->mail = $gitlab_user->email;
41
        $name = explode(' ', $gitlab_user->name);
42
        $user->givenName = $name[0];
43
        $user->sn = $name[1];
44
        $user->addLoginProvider($this->getHostName());
45
        return $user;
46
    }
47
}
48