Skip to content

Commit

Permalink
Added Autodesk Platform Services (Forge) OAuth provider (#1008)
Browse files Browse the repository at this point in the history
Co-authored-by: atymic <[email protected]>
  • Loading branch information
SuperDJ and atymic authored Apr 29, 2023
0 parents commit 9b94b81
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 0 deletions.
18 changes: 18 additions & 0 deletions AutodeskAPSExtendSocialite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace SocialiteProviders\AutodeskAPS;

use SocialiteProviders\Manager\SocialiteWasCalled;

class AutodeskAPSExtendSocialite
{
/**
* Register the provider.
*
* @param SocialiteWasCalled $socialiteWasCalled
*/
public function handle(SocialiteWasCalled $socialiteWasCalled): void
{
$socialiteWasCalled->extendSocialite('aps', Provider::class);
}
}
92 changes: 92 additions & 0 deletions Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace SocialiteProviders\AutodeskAPS;

use GuzzleHttp\RequestOptions;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;

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

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

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

/**
* {@inheritdoc}
*/
protected function getAuthUrl($state): string
{
return
$this->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,
]);
}
}
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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/);
36 changes: 36 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
}
],
"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\\": ""
}
}
}

0 comments on commit 9b94b81

Please sign in to comment.