diff --git a/AutodeskAPSExtendSocialite.php b/AutodeskAPSExtendSocialite.php new file mode 100644 index 0000000..42545e8 --- /dev/null +++ b/AutodeskAPSExtendSocialite.php @@ -0,0 +1,18 @@ +extendSocialite('aps', Provider::class); + } +} diff --git a/Provider.php b/Provider.php new file mode 100644 index 0000000..0d75585 --- /dev/null +++ b/Provider.php @@ -0,0 +1,92 @@ +buildAuthUrlFromBase( + 'https://developer.api.autodesk.com/authentication/v1/authorize', + $state + ); + } + + /** + * {@inheritdoc} + */ + protected function getTokenUrl(): string + { + return 'https://developer.api.autodesk.com/authentication/v1/gettoken'; + } + + /** + * {@inheritdoc} + */ + protected function getTokenFields($code): array + { + return parent::getTokenFields($code); + } + + /** + * @param string $token + * + * @throws \GuzzleHttp\Exception\GuzzleException + * + * @return array + */ + protected function getUserByToken($token): array + { + $response = $this->getHttpClient()->get( + 'https://developer.api.autodesk.com/userprofile/v1/users/@me', + [ + RequestOptions::HEADERS => [ + 'Accept' => 'application/json', + 'Authorization' => 'Bearer '.$token, + ], + ] + ); + + return (array) json_decode((string) $response->getBody(), true); + } + + /** + * https://forge.autodesk.com/en/docs/oauth/v2/reference/http/users-@me-GET/. + * + * {@inheritdoc} + */ + protected function mapUserToObject(array $user): \SocialiteProviders\Manager\OAuth2\User + { + return (new User())->setRaw($user)->map([ + 'id' => $user['userId'], + 'email' => $user['emailId'], + 'username' => $user['userName'], + 'first_name' => $user['firstName'], + 'last_name' => $user['lastName'], + 'country_code' => $user['countryCode'], + 'language' => $user['language'], + 'profile_images' => $user['profileImages'], + 'website' => $user['websiteUrl'] ?? null, + ]); + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..92f48d7 --- /dev/null +++ b/README.md @@ -0,0 +1,56 @@ +# APS + +This is a socialite provider for Autodesk APS (formerly Forge). + +```bash +composer require socialiteproviders/autodesk-aps +``` + +## 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 +// Autodesk APS (Forge) +'autodesk_aps' => [ + 'client_id' => env('AUTODESK_APS_CLIENT_ID'), + 'client_secret' => env('AUTODESK_APS_CLIENT_SECRET'), + 'redirect' => env('AUTODESK_APS_REDIRECT_URI'), +], +``` + +### 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\APS\AutodeskAPSExtendSocialite::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('autodeskaps')->redirect(); +``` + +### Returned user fields + +- `id` +- `name` +- `email` +- `username` + +### Reference + +- [Autodesk documentation](https://aps.autodesk.com/en/docs/oauth/v1/tutorials/get-3-legged-token/); diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..cb2c119 --- /dev/null +++ b/composer.json @@ -0,0 +1,36 @@ +{ + "name": "socialiteproviders/aps", + "description": "Autodesk Platform Services (Forge) OAuth2 Provider for Laravel Socialite", + "license": "MIT", + "keywords": [ + "laravel", + "oauth", + "provider", + "socialite", + "autodesk", + "forge", + "aps", + "autodesk platform services" + ], + "authors": [ + { + "name": "Derkjan Super", + "email": "development@cadix.nl" + } + ], + "support": { + "issues": "https://github.com/socialiteproviders/providers/issues", + "source": "https://github.com/socialiteproviders/providers", + "docs": "https://socialiteproviders.com/aps" + }, + "require": { + "php": "^8.0", + "ext-json": "*", + "socialiteproviders/manager": "^4.0" + }, + "autoload": { + "psr-4": { + "SocialiteProviders\\APS\\": "" + } + } +}