Skip to content

Commit 4a13cbf

Browse files
authored
feat: Add Trovo provider (#1427)
1 parent 5e31d6c commit 4a13cbf

5 files changed

Lines changed: 194 additions & 0 deletions

File tree

monorepo-builder.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ parameters:
226226
src/TwentyThreeAndMe: 'git@github.com:SocialiteProviders/23andme.git'
227227
src/TwitCasting: 'git@github.com:SocialiteProviders/TwitCasting.git'
228228
src/Twitch: 'git@github.com:SocialiteProviders/Twitch.git'
229+
src/Trovo: 'git@github.com:SocialiteProviders/Trovo.git'
229230
src/Twitter: 'git@github.com:SocialiteProviders/Twitter.git'
230231
src/UAEPass: 'git@github.com:SocialiteProviders/UAEPass.git'
231232
src/UCL: 'git@github.com:SocialiteProviders/UCL.git'

src/Trovo/Provider.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace SocialiteProviders\Trovo;
4+
5+
use GuzzleHttp\RequestOptions;
6+
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
7+
use SocialiteProviders\Manager\OAuth2\User;
8+
9+
class Provider extends AbstractProvider
10+
{
11+
public const IDENTIFIER = 'TROVO';
12+
13+
protected $scopes = ['user_details_self'];
14+
15+
protected $scopeSeparator = '+';
16+
17+
protected function getAuthUrl($state): string
18+
{
19+
return $this->buildAuthUrlFromBase(
20+
'https://open.trovo.live/page/login.html',
21+
$state
22+
);
23+
}
24+
25+
protected function getTokenUrl(): string
26+
{
27+
return 'https://open-api.trovo.live/openplatform/exchangetoken';
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function getAccessTokenResponse($code)
34+
{
35+
$fields = $this->getTokenFields($code);
36+
unset($fields['client_id']);
37+
38+
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
39+
RequestOptions::HEADERS => [
40+
'Accept' => 'application/json',
41+
'Client-ID' => $this->clientId,
42+
],
43+
RequestOptions::JSON => $fields,
44+
]);
45+
46+
return json_decode((string) $response->getBody(), true);
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
protected function getUserByToken($token)
53+
{
54+
$response = $this->getHttpClient()->get(
55+
'https://open-api.trovo.live/openplatform/getuserinfo',
56+
[
57+
RequestOptions::HEADERS => [
58+
'Accept' => 'application/json',
59+
'Client-ID' => $this->clientId,
60+
'Authorization' => 'OAuth '.$token,
61+
],
62+
]
63+
);
64+
65+
return json_decode((string) $response->getBody(), true);
66+
}
67+
68+
/**
69+
* {@inheritdoc}
70+
*/
71+
protected function mapUserToObject(array $user)
72+
{
73+
return (new User)->setRaw($user)->map([
74+
'id' => $user['userId'],
75+
'nickname' => $user['nickName'] ?? null,
76+
'name' => $user['userName'] ?? null,
77+
'email' => $user['email'],
78+
'avatar' => $user['profilePic'] ?? null,
79+
]);
80+
}
81+
}

src/Trovo/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Trovo
2+
3+
```bash
4+
composer require socialiteproviders/trovo
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+
'trovo' => [
15+
'client_id' => env('TROVO_CLIENT_ID'),
16+
'client_secret' => env('TROVO_CLIENT_SECRET'),
17+
'redirect' => env('TROVO_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('trovo', \SocialiteProviders\Trovo\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\Trovo\TrovoExtendSocialite::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('trovo')->redirect();
58+
```
59+
60+
### Returned User fields
61+
62+
- ``id``
63+
- ``name``
64+
- ``nickname``
65+
- ``email``
66+
- ``avatar``

src/Trovo/TrovoExtendSocialite.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\Trovo;
4+
5+
use SocialiteProviders\Manager\SocialiteWasCalled;
6+
7+
class TrovoExtendSocialite
8+
{
9+
public function handle(SocialiteWasCalled $socialiteWasCalled): void
10+
{
11+
$socialiteWasCalled->extendSocialite('trovo', Provider::class);
12+
}
13+
}

src/Trovo/composer.json

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

0 commit comments

Comments
 (0)