-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
176 additions
and
1 deletion.
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 |
---|---|---|
@@ -1 +1,36 @@ | ||
# codepeak/oauth2-fortnox | ||
# Fortnox provider for league/oauth2-client | ||
|
||
This is a package to integrate [Fortnox](https://developer.fortnox.se/general/authentication/) authentication with the OAuth2 client library by [The League of Extraordinary Packages](https://github.com/thephpleague/oauth2-client). | ||
|
||
|
||
## Installation | ||
|
||
```bash | ||
composer require codepeak/oauth2-fortnox | ||
``` | ||
|
||
## Usage | ||
|
||
### Create instance of the provider | ||
|
||
```php | ||
$provider = new \Codepeak\OAuth2\Client\Provider\Fortnox([ | ||
'clientId' => "YOUR_CLIENT_ID", | ||
'clientSecret' => "YOUR_CLIENT_SECRET", | ||
'redirectUri' => "http://your-redirect-uri" | ||
]); | ||
``` | ||
|
||
### Get authorization URL | ||
|
||
```php | ||
$authorizationUrl = $provider->getAuthorizationUrl(); | ||
``` | ||
|
||
### Get the access token | ||
|
||
```php | ||
$token = $this->provider->getAccessToken("authorizaton_code", [ | ||
'code' => $_GET['code'] | ||
]); | ||
``` |
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,24 @@ | ||
{ | ||
"name": "codepeak/oauth2-fortnox", | ||
"description": "A Fortnox provider for league/oauth2-client", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Robert Lord, Codepeak AB", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Codepeak\\OAuth2\\Client\\": "src/" | ||
} | ||
}, | ||
"require": { | ||
"php": ">=8.0", | ||
"league/oauth2-client": "^2.6" | ||
}, | ||
"suggest": { | ||
"illuminate/support": "Laravel integration" | ||
}, | ||
"minimum-stability": "dev" | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace Codepeak\OAuth2\Client\OptionProvider; | ||
|
||
use League\OAuth2\Client\OptionProvider\PostAuthOptionProvider; | ||
|
||
class FortnoxOptionProvider extends PostAuthOptionProvider | ||
{ | ||
protected string $base64AuthString = ''; | ||
|
||
public function __construct(string $base64AuthString = '') | ||
{ | ||
$this->base64AuthString = $base64AuthString; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getAccessTokenOptions($method, array $params) | ||
{ | ||
$options = parent::getAccessTokenOptions($method, $params); | ||
|
||
$options['headers']['Authorization'] = 'Basic ' . $this->base64AuthString; | ||
|
||
return $options; | ||
} | ||
} |
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,89 @@ | ||
<?php | ||
|
||
namespace Codepeak\OAuth2\Client\Provider; | ||
|
||
use Codepeak\OAuth2\Client\OptionProvider\FortnoxOptionProvider; | ||
use League\OAuth2\Client\Provider\AbstractProvider; | ||
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; | ||
use League\OAuth2\Client\Token\AccessToken; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class Fortnox extends AbstractProvider | ||
{ | ||
public function __construct(array $options = [], array $collaborators = []) | ||
{ | ||
$this->fillProperties($options); | ||
|
||
$collaborators['optionProvider'] = new FortnoxOptionProvider( | ||
base64_encode($this->clientId . ':' . $this->clientSecret) | ||
); | ||
|
||
parent::__construct($options, $collaborators); | ||
} | ||
|
||
public function getBaseAuthorizationUrl(): string | ||
{ | ||
return 'https://apps.fortnox.se/oauth-v1/auth'; | ||
} | ||
|
||
public function getBaseAccessTokenUrl(array $params): string | ||
{ | ||
return 'https://apps.fortnox.se/oauth-v1/token'; | ||
} | ||
|
||
/** | ||
* Returns the string that should be used to separate scopes when building | ||
* the URL for requesting an access token. | ||
* | ||
* @return string Scope separator | ||
*/ | ||
protected function getScopeSeparator() | ||
{ | ||
return ' '; | ||
} | ||
|
||
protected function getAuthorizationParameters(array $options) | ||
{ | ||
$options = parent::getAuthorizationParameters($options); | ||
|
||
// Add offline access type | ||
if (! isset($options['access_type'])) { | ||
$options['access_type'] = 'offline'; | ||
} | ||
|
||
// Remove approval_prompt since Fortnox forces approval | ||
unset($options['approval_prompt']); | ||
|
||
return $options; | ||
} | ||
|
||
public function getResourceOwnerDetailsUrl(AccessToken $token) | ||
{ | ||
return ''; | ||
} | ||
|
||
protected function getDefaultScopes() | ||
{ | ||
return []; | ||
} | ||
|
||
protected function checkResponse(ResponseInterface $response, $data) | ||
{ | ||
$statusCode = $response->getStatusCode(); | ||
if ($statusCode >= 400) { | ||
$message = $response->getReasonPhrase(); | ||
if (isset($data['error_description'])) { | ||
$message = $data['error_description']; | ||
} | ||
throw new IdentityProviderException( | ||
$message, | ||
$statusCode, | ||
$response | ||
); | ||
} | ||
} | ||
|
||
protected function createResourceOwner(array $response, AccessToken $token) | ||
{ | ||
} | ||
} |