Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UsersAu Provider #1340

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions src/UsersAu/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

namespace SocialiteProviders\Usersau;

use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;

class Provider extends AbstractProvider
{
public const IDENTIFIER = 'USERSAU';

/**
* {@inheritdoc}
*/
protected $scopes = [''];

/**
* {@inheritdoc}
*/
protected $scopeSeparator = ' ';

/**
* {@inheritdoc}
*/
public static function additionalConfigKeys()
{
return [
'host',
'authorize_uri',
'token_uri',
'userinfo_uri',
'userinfo_key',
'user_id',
'user_nickname',
'user_name',
'user_email',
'user_avatar',
'guzzle',
];
}

/**
* Get the authentication URL for the provider.
*
* @param string $state
*
* @return string
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase($this->getUsersauUrl('authorize_uri'), $state);
}

/**
* Get the token URL for the provider.
*
* @return string
*/
protected function getTokenUrl()
{
return $this->getUsersauUrl('token_uri');
}

/**
* Get the raw user for the given access token.
*
* @param string $token
*
* @return array
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get($this->getUsersauUrl('userinfo_uri'), [
RequestOptions::HEADERS => [
'Authorization' => 'Bearer '.$token,
],
]);

return (array) json_decode((string) $response->getBody(), true);
}

/**
* Map the raw user array to a Socialite User instance.
*
* @param array $user
*
* @return \Laravel\Socialite\User
*/
protected function mapUserToObject(array $user)
{
$key = $this->getConfig('userinfo_key', null);
$data = is_null($key) === true ? $user : Arr::get($user, $key, []);

return (new User())->setRaw($data)->map([
'id' => $this->getUserData($data, 'id'),
'nickname' => $this->getUserData($data, 'nickname'),
'name' => $this->getUserData($data, 'name'),
'email' => $this->getUserData($data, 'email'),
'avatar' => $this->getUserData($data, 'avatar'),
]);
}

/**
* Get the POST fields for the token request.
*
* @param string $code
*
* @return array
*/
protected function getTokenFields($code)
{
return array_merge(parent::getTokenFields($code), [
'grant_type' => 'authorization_code',
]);
}

protected function getUsersauUrl($type)
{
return rtrim($this->getConfig('host'), '/').'/'.ltrim(($this->getConfig($type, Arr::get([
'authorize_uri' => 'oauth/authorize',
'token_uri' => 'oauth/token',
'userinfo_uri' => 'api/user',
], $type))), '/');
}

protected function getUserData($user, $key)
{
return Arr::get($user, $this->getConfig('user_'.$key, $key));
}
}
51 changes: 51 additions & 0 deletions src/UsersAu/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Users.au

```bash
composer require socialiteproviders/usersau
```

## 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
'usersau' => [
'client_id' => env('USERSAU_CLIENT_ID'),
'client_secret' => env('USERSAU_CLIENT_SECRET'),
'redirect' => env('USERSAU_REDIRECT_URI'),
'host' => env('USERSAU_HOST'),
],
```

### 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\UsersAu\UsersauExtendSocialite::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('usersau')->redirect();
```

### Returned User fields

- ``id``
- ``nickname``
- ``name``
- ``email``
- ``avatar``
18 changes: 18 additions & 0 deletions src/UsersAu/UsersauExtendSocialite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace SocialiteProviders\Usersau;

use SocialiteProviders\Manager\SocialiteWasCalled;

class UsersauExtendSocialite
{
/**
* Register the provider.
*
* @param \SocialiteProviders\Manager\SocialiteWasCalled $socialiteWasCalled
*/
public function handle(SocialiteWasCalled $socialiteWasCalled)
{
$socialiteWasCalled->extendSocialite('usersau', Provider::class);
}
}
33 changes: 33 additions & 0 deletions src/UsersAu/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "socialiteproviders/usersau",
"description": "Users.au OAuth2 Provider for Laravel Socialite",
"keywords": [
"users.au",
"laravel",
"oauth",
"provider",
"socialite"
],
"license": "MIT",
"authors": [
{
"name": "David Shen",
"email": "[email protected]"
}
],
"require": {
"php": "^7.4 || ^8.0",
"ext-json": "*",
"socialiteproviders/manager": "~4.0"
},
"autoload": {
"psr-4": {
"SocialiteProviders\\Usersau\\": ""
}
},
"support": {
"issues": "https://github.com/socialiteproviders/providers/issues",
"source": "https://github.com/socialiteproviders/providers",
"docs": "https://socialiteproviders.com/usersau"
}
}