Skip to content

Commit

Permalink
Merge pull request #24 from parkourben99/feature/oauth2
Browse files Browse the repository at this point in the history
Upgrade to oauth2
  • Loading branch information
Jordan Hall authored Feb 20, 2020
2 parents 1e1bfb0 + 5c45851 commit 02ba19d
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 107 deletions.
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,25 @@ credentials for your Xero app(s). You can create apps and find the
required credentials in the [My Apps](https://developer.xero.com/myapps/)
section of your Xero account.

If you only intend to use one private Xero app, the standard configuration
If you only intend to use one Xero app, the standard configuration
file should be sufficient. All you will need to do is add the following
variables to your `.env` file and place the `privatekey.pem` file in
`storage/app/xero-key-pair/`.
variables to your `.env` file.

```
XERO_CONSUMER_KEY=XXXXXXIXHKXXXXXXST0TU44ZXXXXXX
XERO_CONSUMER_SECRET=XXXXXXBNNUXXXXXXGWAJNFOUXXXXXX
XERO_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XERO_TENANT_ID=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
```

If you wish to use a public app, you will have to add a new app to the
configuration file or change the existing one. The `app_type` should be
`public` rather than `private`.
## Migration from 1.x/OAuth 1a

There is now only one flow for all applications, which is most similar to the legacy Public application.
All applications now require the OAuth 2 authorisation flow and specific organisations to be authorised
at runtime, rather than creating certificates during app creation.

Following [this example](https://github.com/calcinai/xero-php#authorization-code-flow) you can generate the
required token and tenant id.

For more information on scopes try the [xero documentation](https://developer.xero.com/documentation/oauth2/scopes).

## Usage

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"require": {
"laravel/framework": "^5.1||^6.0",
"calcinai/xero-php": "^1.8"
"calcinai/xero-php": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
30 changes: 0 additions & 30 deletions src/Apps/PrivateXeroApp.php

This file was deleted.

30 changes: 0 additions & 30 deletions src/Apps/PublicXeroApp.php

This file was deleted.

2 changes: 0 additions & 2 deletions src/Wrappers/QueryWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
namespace LangleyFoxall\XeroLaravel\Wrappers;

use Illuminate\Support\Collection;
use LangleyFoxall\XeroLaravel\Apps\PrivateXeroApp;
use LangleyFoxall\XeroLaravel\Traits\HasXeroRelationships;
use XeroPHP\Application;
use XeroPHP\Remote\Query;

Expand Down
27 changes: 4 additions & 23 deletions src/Xero.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
namespace LangleyFoxall\XeroLaravel;

use Exception;
use LangleyFoxall\XeroLaravel\Apps\PrivateXeroApp;
use LangleyFoxall\XeroLaravel\Apps\PublicXeroApp;
use XeroPHP\Application;

class Xero
{
Expand All @@ -19,7 +16,7 @@ class Xero
*/
public function app($key = 'default')
{
if (!isset($this->apps[$key])) {
if (! isset($this->apps[$key])) {
$this->apps[$key] = $this->createApp($key);
}

Expand All @@ -30,34 +27,18 @@ public function app($key = 'default')
* Creates the XeroApp object
*
* @param string $key
* @return Application
* @return XeroApp
* @throws Exception
*/
private function createApp($key)
{
$config = config(Constants::CONFIG_KEY);

if (!isset($config['apps']) || !isset($config['apps'][$key])) {
if (! isset($config['apps'][$key])) {
throw new Exception('The specified key could not be found in the Xero \'apps\' array, ' .
'or the \'apps\' array does not exist.');
}

$appConfig = $config['apps'][$key];

switch ($appConfig['app_type']) {
case 'private':
return new PrivateXeroApp($appConfig);
break;

case 'public':
return new PublicXeroApp($appConfig);
break;

case 'partner':
throw new Exception('Partner Xero app types are not yet supported.');
break;
}

throw new Exception('Xero app type is invalid. Should be \'private\', \'public\', or \'partner\'.');
return new XeroApp($config['apps'][$key]['token'], $config['apps'][$key]['tenant_id']);
}
}
31 changes: 31 additions & 0 deletions src/XeroApp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace LangleyFoxall\XeroLaravel;

use Exception;
use LangleyFoxall\XeroLaravel\Traits\HasXeroRelationships;
use XeroPHP\Application;

/**
* Class XeroApp
*
* @package LangleyFoxall\XeroLaravel
*/
class XeroApp extends Application
{
use HasXeroRelationships;

/**
* XeroApp constructor.
*
* @param $token
* @param $tenantId
* @throws Exception
*/
public function __construct($token, $tenantId)
{
parent::__construct($token, $tenantId);

$this->populateRelationshipToModelMaps();
}
}
17 changes: 4 additions & 13 deletions src/config/xero-laravel-lf.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,9 @@
*/

'apps' => [

'default' => [

'app_type' => 'private',
'oauth' => [
'callback' => 'http://localhost/',
'consumer_key' => env('XERO_CONSUMER_KEY'),
'consumer_secret' => env('XERO_CONSUMER_SECRET'),
'rsa_private_key' => 'file://'.storage_path('app/xero-key-pair/privatekey.pem'),
],

]
]

'token' => env('XERO_TOKEN'),
'tenant_id' => env('XERO_TENANT_ID'),
],
],
];

0 comments on commit 02ba19d

Please sign in to comment.