-
Notifications
You must be signed in to change notification settings - Fork 35
[6.x] Credential managers #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hailwood
merged 26 commits into
foxbytehq:develop
from
JamesFreeman:feature/new-credential-managers
Oct 5, 2024
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
ac6f6d2
First stage refactor CredentitalManagers
JamesFreeman 0d57219
migrate to new dataproviders for credentialmanagers
JamesFreeman 9246eda
Apply fixes from StyleCI
StyleCIBot 127ba8e
Merge pull request #1 from JamesFreeman/analysis-GP5732
JamesFreeman ffd6622
revert big bc, change session key for cache store
JamesFreeman 523817c
revert big bc, change session key for cache store
JamesFreeman b118b93
Adding new Model based storage
JamesFreeman 1fe7600
revert
JamesFreeman 8a9b2d6
refactor
JamesFreeman f4375a3
Apply fixes from StyleCI
StyleCIBot 242da4e
Merge pull request #2 from JamesFreeman/analysis-BoJQRV
JamesFreeman c80178d
first pass
JamesFreeman 7c31876
fix tests
JamesFreeman 29755fc
remove config tests
JamesFreeman e177fe4
Apply fixes from StyleCI
StyleCIBot 13c3610
Merge pull request #3 from JamesFreeman/analysis-4wPZYk
JamesFreeman 00a9c68
New exceptions, changes to how constructors work.
JamesFreeman 82d2f42
Apply fixes from StyleCI
StyleCIBot 04b5162
Merge pull request #4 from JamesFreeman/analysis-RPNmjD
JamesFreeman d43db97
change tests
JamesFreeman ca04e09
Merge branch 'feature/new-credential-managers' of github.com:JamesFre…
JamesFreeman 8329355
Apply fixes from StyleCI
StyleCIBot 70142b1
Merge pull request #5 from JamesFreeman/analysis-vZx3mN
JamesFreeman b21dc06
changes
JamesFreeman bdc2ca9
Merge branch 'feature/new-credential-managers' of github.com:JamesFre…
JamesFreeman ccabe0a
change to XeroException
JamesFreeman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,7 @@ | ||
| <?php | ||
|
|
||
| namespace Webfox\Xero\Exceptions; | ||
|
|
||
| class XeroCredentialsNotFound extends XeroException | ||
| { | ||
| } |
This file contains hidden or 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,19 @@ | ||
| <?php | ||
|
|
||
| namespace Webfox\Xero\Exceptions; | ||
|
|
||
| use Exception; | ||
| use Throwable; | ||
|
|
||
| abstract class XeroException extends Exception | ||
| { | ||
| public function __construct($message, $code = 0, Throwable $previous = null) | ||
| { | ||
| parent::__construct($message, $code, $previous); | ||
| } | ||
|
|
||
| public function __toString() | ||
| { | ||
| return __CLASS__.": [{$this->code}]: {$this->message}\n"; | ||
| } | ||
| } |
This file contains hidden or 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,7 @@ | ||
| <?php | ||
|
|
||
| namespace Webfox\Xero\Exceptions; | ||
|
|
||
| class XeroTenantNotFound extends XeroException | ||
| { | ||
| } |
This file contains hidden or 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,7 @@ | ||
| <?php | ||
|
|
||
| namespace Webfox\Xero\Exceptions; | ||
|
|
||
| class XeroUserNotAuthenticated extends XeroException | ||
| { | ||
| } |
This file contains hidden or 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
This file contains hidden or 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,20 @@ | ||
| <?php | ||
|
|
||
| namespace Webfox\Xero\Oauth2CredentialManagers; | ||
|
|
||
| use Illuminate\Support\Facades\Auth; | ||
| use Webfox\Xero\Exceptions\XeroUserNotAuthenticated; | ||
|
|
||
| class AuthenticatedUserStore extends ModelStore | ||
| { | ||
| public function __construct() | ||
| { | ||
| if (! Auth::check()) { | ||
| throw new XeroUserNotAuthenticated('User is not authenticated'); | ||
| } | ||
|
|
||
| parent::__construct(); | ||
|
|
||
| $this->model = Auth::user(); | ||
| } | ||
| } |
This file contains hidden or 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,104 @@ | ||
| <?php | ||
|
|
||
| namespace Webfox\Xero\Oauth2CredentialManagers; | ||
|
|
||
| use Illuminate\Session\Store; | ||
| use Webfox\Xero\Exceptions\XeroTenantNotFound; | ||
| use Webfox\Xero\Oauth2Provider; | ||
| use XeroAPI\XeroPHP\JWTClaims; | ||
|
|
||
| abstract class BaseCredentialManager | ||
| { | ||
| protected Store $session; | ||
| protected Oauth2Provider $oauthProvider; | ||
|
|
||
| public function __construct() | ||
| { | ||
| $this->session = app(Store::class); | ||
| $this->oauthProvider = app(Oauth2Provider::class); | ||
| } | ||
|
|
||
| abstract protected function data(string $key = null); | ||
|
|
||
| public function getAccessToken(): string | ||
| { | ||
| return $this->data('token'); | ||
JamesFreeman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public function getRefreshToken(): string | ||
| { | ||
| return $this->data('refresh_token'); | ||
| } | ||
|
|
||
| public function getTenants(): ?array | ||
| { | ||
| return $this->data('tenants'); | ||
| } | ||
|
|
||
| public function getTenantId(int $tenant = 0): string | ||
| { | ||
| if (! isset($this->data('tenants')[$tenant])) { | ||
| throw new XeroTenantNotFound('No such tenant exists'); | ||
| } | ||
|
|
||
| return $this->data('tenants')[$tenant]['Id']; | ||
| } | ||
|
|
||
| public function getExpires(): int | ||
| { | ||
| return $this->data('expires'); | ||
| } | ||
|
|
||
| public function getData(): array | ||
| { | ||
| return $this->data(); | ||
| } | ||
|
|
||
| public function getUser(): ?array | ||
| { | ||
| try { | ||
| $jwt = new JWTClaims(); | ||
| $jwt->setTokenId($this->data('id_token')); | ||
| $decodedToken = $jwt->decode(); | ||
|
|
||
| return [ | ||
| 'given_name' => $decodedToken->getGivenName(), | ||
| 'family_name' => $decodedToken->getFamilyName(), | ||
| 'email' => $decodedToken->getEmail(), | ||
| 'user_id' => $decodedToken->getXeroUserId(), | ||
| 'username' => $decodedToken->getPreferredUsername(), | ||
| 'session_id' => $decodedToken->getGlobalSessionId(), | ||
| ]; | ||
| } catch (\Throwable $e) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| public function isExpired(): bool | ||
| { | ||
| return time() >= $this->data('expires'); | ||
| } | ||
|
|
||
| public function refresh(): void | ||
| { | ||
| $newAccessToken = $this->oauthProvider->getAccessToken('refresh_token', [ | ||
| 'grant_type' => 'refresh_token', | ||
| 'refresh_token' => $this->getRefreshToken(), | ||
| ]); | ||
|
|
||
| $this->store($newAccessToken); | ||
| } | ||
|
|
||
| public function getState(): string | ||
| { | ||
| return $this->session->get('xero_oauth2_state') ?? ''; | ||
| } | ||
|
|
||
| public function getAuthorizationUrl(): string | ||
| { | ||
| $redirectUrl = $this->oauthProvider->getAuthorizationUrl(['scope' => config('xero.oauth.scopes')]); | ||
| $this->session->put('xero_oauth2_state', $this->oauthProvider->getState()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also declare |
||
|
|
||
| return $redirectUrl; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than specifying the same message in multiple places, let's just make these the default values for the
$messageparameter across each of the exceptions.