This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
GitlabAuthenticator.php
136 lines (111 loc) · 4.24 KB
/
GitlabAuthenticator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace App\Security;
use Doctrine\ODM\MongoDB\DocumentManager;
use Omines\OAuth2\Client\Provider\Gitlab;
use Omines\OAuth2\Client\Provider\GitlabResourceOwner;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
class GitlabAuthenticator extends AbstractGuardAuthenticator
{
use TargetPathTrait;
/**
* @var Gitlab
*/
private $gitlab;
/**
* @var RouterInterface
*/
private $router;
/**
* @var DocumentManager
*/
private $manager;
/**
* @var string
*/
private $gitlabDefaultRole;
public function __construct(RouterInterface $router, DocumentManager $manager, $gitlabDomain, $gitlabClientId, $gitlabClientSecret, $gitlabDefaultRole)
{
$this->router = $router;
$this->manager = $manager;
$this->gitlabDefaultRole = $gitlabDefaultRole;
$this->gitlab = new Gitlab([
'clientId' => $gitlabClientId,
'clientSecret' => $gitlabClientSecret,
'redirectUri' => $this->router->generate('gitlab_login', [], RouterInterface::ABSOLUTE_URL),
'domain' => $gitlabDomain,
]);
}
public function start(Request $request, AuthenticationException $authException = null)
{
}
public function supports(Request $request)
{
return 'gitlab_login' === $request->attributes->get('_route')
&& $this->gitlab->domain;
}
public function getCredentials(Request $request)
{
$code = $request->query->get('code');
if (!$code) {
throw new AuthenticationCredentialsNotFoundException();
}
$state = $request->query->get('state');
if (!$state || $state !== $request->getSession()->get('oauth2state')) {
throw new CustomUserMessageAuthenticationException('Invalid state.');
}
return ['code' => $code];
}
public function getUser($credentials, UserProviderInterface $userProvider)
{
$token = $this->gitlab->getAccessToken('authorization_code', [
'code' => $credentials['code'],
]);
try {
/** @var GitlabResourceOwner $gitlabUser */
$gitlabUser = $this->gitlab->getResourceOwner($token);
} catch (\Exception $e) {
throw new CustomUserMessageAuthenticationException('Gitlab error.');
}
try {
$user = $userProvider->loadUserByUsername($gitlabUser->getUsername());
} catch (UsernameNotFoundException $e) {
$user = $this->manager->getRepository('App:User')->createFromGitlab($gitlabUser, $this->gitlabDefaultRole);
}
return $user;
}
public function checkCredentials($credentials, UserInterface $user)
{
return true;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
return new RedirectResponse($targetPath);
}
return new RedirectResponse($this->router->generate('home'));
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
if ($exception instanceof AuthenticationCredentialsNotFoundException) {
$url = $this->gitlab->getAuthorizationUrl();
$request->getSession()->set('oauth2state', $this->gitlab->getState());
return new RedirectResponse($url);
}
throw $exception;
}
public function supportsRememberMe()
{
return false;
}
}