Skip to content

Commit 93a0570

Browse files
authored
feat: Add Kick OAuth2 provider (#1414)
* feat: Add Kick OAuth2 provider * security: add PKCE support to OAuth flow * feat: add new line * refactor: change key type to integer
1 parent be9167d commit 93a0570

File tree

5 files changed

+190
-0
lines changed

5 files changed

+190
-0
lines changed

monorepo-builder.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ parameters:
108108
src/Kakao: 'git@github.com:SocialiteProviders/Kakao.git'
109109
src/Kanidm: 'git@github.com:SocialiteProviders/Kanidm.git'
110110
src/Keycloak: 'git@github.com:SocialiteProviders/Keycloak.git'
111+
src/Kick: 'git@github.com:SocialiteProviders/Kick.git'
111112
src/Klarna: 'git@github.com:SocialiteProviders/Klarna.git'
112113
src/Kinde: 'git@github.com:SocialiteProviders/Kinde.git'
113114
src/Kommo: 'git@github.com:SocialiteProviders/Kommo.git'

src/Kick/KickExtendSocialite.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace SocialiteProviders\Kick;
4+
5+
use SocialiteProviders\Manager\SocialiteWasCalled;
6+
7+
class KickExtendSocialite
8+
{
9+
public function handle(SocialiteWasCalled $socialiteWasCalled): void
10+
{
11+
$socialiteWasCalled->extendSocialite('kick', Provider::class);
12+
}
13+
}

src/Kick/Provider.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace SocialiteProviders\Kick;
4+
5+
use GuzzleHttp\RequestOptions;
6+
use Illuminate\Support\Arr;
7+
use Laravel\Socialite\Two\Token;
8+
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
9+
use SocialiteProviders\Manager\OAuth2\User;
10+
11+
class Provider extends AbstractProvider
12+
{
13+
public const IDENTIFIER = 'KICK';
14+
15+
protected $scopes = ['user:read'];
16+
17+
protected $usesPKCE = true;
18+
19+
protected function getAuthUrl($state): string
20+
{
21+
return $this->buildAuthUrlFromBase('https://id.kick.com/oauth/authorize', $state);
22+
}
23+
24+
protected function getTokenUrl(): string
25+
{
26+
return 'https://id.kick.com/oauth/token';
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
protected function getUserByToken($token)
33+
{
34+
$response = $this->getHttpClient()->get(
35+
'https://api.kick.com/public/v1/users',
36+
[
37+
RequestOptions::HEADERS => [
38+
'Accept' => 'application/json',
39+
'Authorization' => 'Bearer ' . $token
40+
],
41+
]
42+
);
43+
44+
return json_decode((string) $response->getBody(), true);
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
protected function mapUserToObject(array $user)
51+
{
52+
$user = $user['data'][0];
53+
54+
return (new User)->setRaw($user)->map([
55+
'id' => $user['user_id'],
56+
'nickname' => $user['name'],
57+
'name' => $user['name'],
58+
'email' => $user['email'],
59+
'avatar' => $user['profile_picture'] ?? null,
60+
]);
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
public function refreshToken($refreshToken)
67+
{
68+
$response = $this->getRefreshTokenResponse($refreshToken);
69+
70+
return new Token(
71+
Arr::get($response, 'access_token'),
72+
Arr::get($response, 'refresh_token'),
73+
Arr::get($response, 'expires_in'),
74+
Arr::get($response, 'scope', [])
75+
);
76+
}
77+
}

src/Kick/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Kick
2+
3+
```bash
4+
composer require socialiteproviders/kick
5+
```
6+
7+
## Installation & Basic Usage
8+
9+
Please see the [Base Installation Guide](https://socialiteproviders.com/usage/), then follow the provider specific instructions below.
10+
11+
### Add configuration to `config/services.php`
12+
13+
```php
14+
'kick' => [
15+
'client_id' => env('KICK_CLIENT_ID'),
16+
'client_secret' => env('KICK_CLIENT_SECRET'),
17+
'redirect' => env('KICK_REDIRECT_URI')
18+
],
19+
```
20+
21+
### Add provider event listener
22+
23+
#### Laravel 11+
24+
25+
In Laravel 11, the default `EventServiceProvider` provider was removed. Instead, add the listener using the `listen` method on the `Event` facade, in your `AppServiceProvider` `boot` method.
26+
27+
* Note: You do not need to add anything for the built-in socialite providers unless you override them with your own providers.
28+
29+
```php
30+
Event::listen(function (\SocialiteProviders\Manager\SocialiteWasCalled $event) {
31+
$event->extendSocialite('kick', \SocialiteProviders\Kick\Provider::class);
32+
});
33+
```
34+
<details>
35+
<summary>
36+
Laravel 10 or below
37+
</summary>
38+
Configure the package's listener to listen for `SocialiteWasCalled` events.
39+
40+
Add the event to your `listen[]` array in `app/Providers/EventServiceProvider`. See the [Base Installation Guide](https://socialiteproviders.com/usage/) for detailed instructions.
41+
42+
```php
43+
protected $listen = [
44+
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
45+
// ... other providers
46+
\SocialiteProviders\Kick\KickExtendSocialite::class.'@handle',
47+
],
48+
];
49+
```
50+
</details>
51+
52+
### Usage
53+
54+
You should now be able to use the provider like you would regularly use Socialite (assuming you have the facade installed):
55+
56+
```php
57+
return Socialite::driver('kick')->redirect();
58+
```
59+
60+
### Returned User fields
61+
62+
- ``id``
63+
- ``username``
64+
- ``account_type``
65+
- ``media_count``
66+
- ``avatar``

src/Kick/composer.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "socialiteproviders/kick",
3+
"description": "Kick OAuth2 Provider for Laravel Socialite",
4+
"license": "MIT",
5+
"keywords": [
6+
"laravel",
7+
"oauth",
8+
"provider",
9+
"socialite",
10+
"kick"
11+
],
12+
"authors": [
13+
{
14+
"name": "Ali Fadaei",
15+
"email": "itsalifadaei@gmail.com"
16+
}
17+
],
18+
"support": {
19+
"issues": "https://github.com/socialiteproviders/providers/issues",
20+
"source": "https://github.com/socialiteproviders/providers",
21+
"docs": "https://socialiteproviders.com/kick"
22+
},
23+
"require": {
24+
"php": "^8.0",
25+
"ext-json": "*",
26+
"socialiteproviders/manager": "^4.4|^4.8|^4.8.1"
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"SocialiteProviders\\Kick\\": ""
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)