-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: atymic <atymicq@gmail.com>
- Loading branch information
Showing
4 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace SocialiteProviders\GovBR; | ||
|
||
use SocialiteProviders\Manager\SocialiteWasCalled; | ||
|
||
class GovBRExtendSocialite | ||
{ | ||
/** | ||
* Register the provider. | ||
* | ||
* @param \SocialiteProviders\Manager\SocialiteWasCalled $socialiteWasCalled | ||
*/ | ||
public function handle(SocialiteWasCalled $socialiteWasCalled) | ||
{ | ||
$socialiteWasCalled->extendSocialite('govbr', Provider::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?php | ||
|
||
namespace SocialiteProviders\GovBR; | ||
|
||
use RuntimeException; | ||
use SocialiteProviders\Manager\Contracts\OAuth2\ProviderInterface; | ||
use SocialiteProviders\Manager\OAuth2\AbstractProvider; | ||
use SocialiteProviders\Manager\OAuth2\User; | ||
|
||
class Provider extends AbstractProvider implements ProviderInterface | ||
{ | ||
/** | ||
* Unique Provider Identifier. | ||
*/ | ||
const IDENTIFIER = 'GOVBR'; | ||
|
||
const SCOPE_OPENID = 'openid'; | ||
|
||
const SCOPE_EMAIL = 'email'; | ||
|
||
const SCOPE_PROFILE = 'profile'; | ||
|
||
const SCOPE_GOVBR_EMPRESA = 'govbr_empresa'; | ||
|
||
const SCOPE_GOVBR_CONFIABILIDADES = 'govbr_confiabilidades'; | ||
|
||
/** | ||
* Staging URL. | ||
* | ||
* @var string | ||
*/ | ||
protected $stagingUrl = 'https://sso.staging.acesso.gov.br'; | ||
|
||
/** | ||
* Production URL. | ||
* | ||
* @var string | ||
*/ | ||
protected $productionUrl = 'https://sso.acesso.gov.br'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $scopeSeparator = ' '; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $scopes = [ | ||
self::SCOPE_OPENID, | ||
self::SCOPE_EMAIL, | ||
self::SCOPE_PROFILE, | ||
self::SCOPE_GOVBR_CONFIABILIDADES, | ||
]; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $usesPKCE = true; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getAuthUrl($state) | ||
{ | ||
return $this->buildAuthUrlFromBase($this->getBaseUrlForEnvironment().'/authorize', $state); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getTokenUrl() | ||
{ | ||
return $this->getBaseUrlForEnvironment().'/token'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getUserByToken($token) | ||
{ | ||
$response = $this->getHttpClient()->get($this->getBaseUrlForEnvironment().'/userinfo', [ | ||
'headers' => [ | ||
'Authorization' => 'Bearer '.$token, | ||
], | ||
]); | ||
|
||
return json_decode($response->getBody(), true); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function mapUserToObject(array $user) | ||
{ | ||
return (new User())->setRaw($user)->map([ | ||
'id' => $user['sub'], | ||
'cpf' => $user['sub'], | ||
'name' => $user['name'], | ||
'email' => $user['email'] ?? null, | ||
'email_verified' => $user['email_verified'] ?? null, | ||
'phone_number' => $user['phone_number'] ?? null, | ||
'phone_number_verified' => $user['phone_number_verified'] ?? null, | ||
'avatar_url' => $user['picture'] ?? null, | ||
'profile' => $user['profile'] ?? null, | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getTokenFields($code) | ||
{ | ||
return array_merge(parent::getTokenFields($code), [ | ||
'grant_type' => 'authorization_code', | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function additionalConfigKeys() | ||
{ | ||
return ['environment']; | ||
} | ||
|
||
/** | ||
* Get the URL for the given environment. | ||
* | ||
* @throws RuntimeException | ||
*/ | ||
protected function getBaseUrlForEnvironment(): string | ||
{ | ||
$environment = $this->getConfig('environment', 'production'); | ||
|
||
return match ($environment) { | ||
'staging' => $this->stagingUrl, | ||
'production' => $this->productionUrl, | ||
default => throw new RuntimeException("Invalid environment '{$environment}' selected for GovBR provider."), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# GovBR | ||
|
||
```bash | ||
composer require socialiteproviders/govbr | ||
``` | ||
|
||
## Installation & Basic Usage | ||
|
||
Please see the [Base Installation Guide](https://socialiteproviders.com/usage/), then follow the provider specific instructions below. | ||
|
||
### Add configuration to `config/services.php` | ||
|
||
```php | ||
'govbr' => [ | ||
'client_id' => env('GOVBR_CLIENT_ID'), | ||
'client_secret' => env('GOVBR_CLIENT_SECRET'), | ||
'redirect' => env('GOVBR_REDIRECT_URI'), | ||
'environment' => env('GOVBR_ENVIRONMENT', 'production'), | ||
], | ||
``` | ||
|
||
### Add provider event listener | ||
|
||
Configure the package's listener to listen for `SocialiteWasCalled` events. | ||
|
||
Add the event to your `listen[]` array in `app/Providers/EventServiceProvider`. See the [Base Installation Guide](https://socialiteproviders.com/usage/) for detailed instructions. | ||
|
||
```php | ||
protected $listen = [ | ||
\SocialiteProviders\Manager\SocialiteWasCalled::class => [ | ||
// ... other providers | ||
\SocialiteProviders\GovBR\GovBRExtendSocialite::class.'@handle', | ||
], | ||
]; | ||
``` | ||
|
||
### Usage | ||
|
||
You should now be able to use the provider like you would regularly use Socialite (assuming you have the facade installed): | ||
|
||
```php | ||
return Socialite::driver('govbr')->redirect(); | ||
``` | ||
|
||
### Returned User fields | ||
|
||
- `id` | ||
- `cpf` | ||
- `name` | ||
- `email` | ||
- `email_verified` | ||
- `phone_number` | ||
- `phone_number_verified` | ||
- `avatar_url` | ||
- `profile` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "socialiteproviders/govbr", | ||
"description": "GovBR OAuth2 Provider for Laravel Socialite", | ||
"keywords": [ | ||
"laravel", | ||
"govbr", | ||
"oauth", | ||
"provider", | ||
"socialite" | ||
], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Saade", | ||
"email": "saade@outlook.com.br" | ||
} | ||
], | ||
"support": { | ||
"issues": "https://github.com/socialiteproviders/providers/issues", | ||
"source": "https://github.com/socialiteproviders/providers", | ||
"docs": "https://socialiteproviders.com/govbr" | ||
}, | ||
"require": { | ||
"php": "^8.0", | ||
"socialiteproviders/manager": "~4.0", | ||
"ext-json": "*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"SocialiteProviders\\GovBR\\": "" | ||
} | ||
} | ||
} |