Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
robbanl committed Feb 6, 2023
1 parent b35c48d commit 47d4efe
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 1 deletion.
37 changes: 36 additions & 1 deletion README.md
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']
]);
```
24 changes: 24 additions & 0 deletions composer.json
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"
}
27 changes: 27 additions & 0 deletions src/OptionProvider/FortnoxOptionProvider.php
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;
}
}
89 changes: 89 additions & 0 deletions src/Provider/Fortnox.php
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)
{
}
}

0 comments on commit 47d4efe

Please sign in to comment.