Skip to content

Commit

Permalink
2amigos#558 Created Da\User\AuthClient\Xero auth client
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Aug 9, 2024
1 parent f3765a0 commit ce6b0a3
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Enh: Changed exception thrown in PasswordRecoveryService from `RuntimeException` to `NotFoundException`. (eseperio)
- New #553: created Da\User\AuthClient\Microsoft365 auth client (edegaudenzi)
- Ehh: Added SecurityHelper to the Bootstrap classMap
- New #558: created Da\User\AuthClient\Xero auth client (edegaudenzi)

## 1.6.3 Mar 18th, 2024

Expand Down
110 changes: 110 additions & 0 deletions src/User/AuthClient/Xero.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

/*
* This file is part of the 2amigos/yii2-usuario project.
*
* (c) 2amigOS! <http://2amigos.us/>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Da\User\AuthClient;

use Da\User\Contracts\AuthClientInterface;
use Da\User\Traits\AuthClientUserIdTrait;
use yii\authclient\OAuth2;

/**
* Xero allows authentication via Xero OAuth2 flow.
* Before using Xero OAuth2 you must register your Xero Application
* @see https://developer.xero.com/
*
* Note: the registered App must have the following:
* -Authentication: 'Redirect URIs' set 'user/security/auth?authclient=xero' as an absolute URL
* e.g. https://domain.com/index.php/user/security/auth?authclient=xero
*
* Example application configuration:
*
* ```
* 'components' => [
* ...
* 'authClientCollection' => [
* 'class' => 'yii\authclient\Collection',
* 'clients' => [
* 'xero' => [
* 'class' => 'yii\authclient\clients\Xero',
* 'clientId' => '15ED1343598243FE666D5276CD42F825',
* 'clientSecret' => '8P_WI03t9VRlcWSpjFJc89PhpyAkkD5mgl4tkhfCHt_qwK0d',
* ],
* ],
* ]
* ...
* ]
* ```
*/
class Xero extends OAuth2 implements AuthClientInterface
{
use AuthClientUserIdTrait;

public $apiBaseUrl = 'https://identity.xero.com/connect';
public $authUrl = 'https://login.xero.com/identity/connect/authorize';
public $scope = 'email openid profile';
public $tokenUrl = 'https://identity.xero.com/connect/token';

/**
* {@inheritdoc}
*/
public function applyAccessTokenToRequest($request, $accessToken)
{
$request->headers->set('Authorization', 'Bearer '.$accessToken->getToken());
}

/**
* {@inheritdoc}
*/
protected function defaultName()
{
return 'xero';
}

/**
* {@inheritdoc}
*/
protected function defaultTitle()
{
return 'Xero';
}

/**
* {@inheritdoc}
*/
public function getEmail()
{
return $this->getUserAttributes()['email'] ?? null;
}

/**
* {@inheritdoc}
*/
public function getUserId()
{
return $this->getUserAttributes()['xero_userid'] ?? null;
}

/**
* {@inheritdoc}
*/
public function getUsername()
{
return $this->getUserAttributes()['preferred_username'] ?? null;
}

/**
* {@inheritdoc}
*/
protected function initUserAttributes()
{
return $this->api('userinfo', 'GET');
}
}

0 comments on commit ce6b0a3

Please sign in to comment.