Skip to content

Commit 1478bb2

Browse files
committed
Add GitProvider to LaravelOAuthServiceProvider
1 parent d3075c8 commit 1478bb2

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

src/LaravelOAuthServiceProvider.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Mckenziearts\LaravelOAuth;
44

55
use Illuminate\Support\ServiceProvider;
6+
use Mckenziearts\LaravelOAuth\Providers\GitlabProvider;
67
use Mckenziearts\LaravelOAuth\Providers\YoutubeProvider;
78
use Mckenziearts\LaravelOAuth\Providers\DribbbleProvider;
89
use Mckenziearts\LaravelOAuth\Providers\InstagramProvider;
@@ -37,6 +38,7 @@ public function boot()
3738
$this->bootDribbbleSocialite();
3839
$this->bootPinterestSocialite();
3940
$this->bootYoutubeSocialite();
41+
$this->bootGitlabSocialite();
4042
}
4143

4244
/**
@@ -131,4 +133,17 @@ function ($app) use ($socialite) {
131133
}
132134
);
133135
}
136+
137+
private function bootGitlabSocialite()
138+
{
139+
$socialite = $this->app->make(\Laravel\Socialite\Contracts\Factory::class);
140+
$socialite->extend(
141+
'gitlab',
142+
function ($app) use ($socialite) {
143+
$config = $app['config']['services.gitlab'];
144+
145+
return $socialite->buildProvider(GitlabProvider::class, $config);
146+
}
147+
);
148+
}
134149
}

src/Providers/GitlabProvider.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Mckenziearts\LaravelOAuth\Providers;
4+
5+
use Laravel\Socialite\Two\User;
6+
use Laravel\Socialite\Two\AbstractProvider;
7+
use Laravel\Socialite\Two\ProviderInterface;
8+
9+
class GitlabProvider extends AbstractProvider implements ProviderInterface
10+
{
11+
/**
12+
* {@inheritdoc}
13+
*/
14+
protected $scopes = ['read_user'];
15+
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
protected $scopeSeparator = ' ';
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
protected function getAuthUrl($state)
25+
{
26+
return $this->buildAuthUrlFromBase('https://gitlab.com/oauth/authorize', $state);
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
protected function getTokenUrl()
33+
{
34+
return 'https://gitlab.com/oauth/token';
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
protected function getUserByToken($token)
41+
{
42+
$response = $this->getHttpClient()->get('https://gitlab.com/api/v4/user', [
43+
'headers' => [
44+
'Authorization' => 'Bearer '.$token,
45+
],
46+
]);
47+
48+
return json_decode($response->getBody(), true);
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
protected function mapUserToObject(array $user)
55+
{
56+
return (new User())->setRaw($user)->map([
57+
'id' => $user['id'],
58+
'nickname' => $user['username'],
59+
'name' => $user['name'],
60+
'email' => $user['email'],
61+
'avatar' => $user['avatar_url'],
62+
]);
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
protected function getTokenFields($code)
69+
{
70+
return array_merge(parent::getTokenFields($code), [
71+
'grant_type' => 'authorization_code',
72+
]);
73+
}
74+
}

0 commit comments

Comments
 (0)