22
33namespace Webfox \Xero \Oauth2CredentialManagers ;
44
5- use Illuminate \Session \Store ;
65use League \OAuth2 \Client \Token \AccessTokenInterface ;
7- use Webfox \Xero \Oauth2Provider ;
6+ use Webfox \Xero \Exceptions \ XeroCredentialsNotFound ;
87use Webfox \Xero \OauthCredentialManager ;
98
10- class ArrayStore implements OauthCredentialManager
9+ class ArrayStore extends BaseCredentialManager implements OauthCredentialManager
1110{
1211 public ?array $ dataStorage = null ;
1312
14- public function __construct (protected Store $ session , protected Oauth2Provider $ oauthProvider )
15- {
16- }
17-
18- public function getAccessToken (): string
19- {
20- return $ this ->data ('token ' );
21- }
22-
23- public function getRefreshToken (): string
24- {
25- return $ this ->data ('refresh_token ' );
26- }
27-
28- public function getTenants (): ?array
29- {
30- return $ this ->data ('tenants ' );
31- }
32-
33- public function getTenantId (int $ tenant = 0 ): string
34- {
35- if (! isset ($ this ->data ('tenants ' )[$ tenant ])) {
36- throw new \Exception ('No such tenant exists ' );
37- }
38-
39- return $ this ->data ('tenants ' )[$ tenant ]['Id ' ];
40- }
41-
42- public function getExpires (): int
43- {
44- return $ this ->data ('expires ' );
45- }
46-
47- public function getState (): string
48- {
49- return $ this ->session ->get ('xero_oauth2_state ' ) ?? '' ;
50- }
51-
52- public function getAuthorizationUrl (): string
53- {
54- $ redirectUrl = $ this ->oauthProvider ->getAuthorizationUrl (['scope ' => config ('xero.oauth.scopes ' )]);
55- $ this ->session ->put ('xero_oauth2_state ' , $ this ->oauthProvider ->getState ());
56-
57- return $ redirectUrl ;
58- }
59-
60- public function getData (): array
61- {
62- return $ this ->data ();
63- }
64-
6513 public function exists (): bool
6614 {
6715 return $ this ->dataStorage !== null ;
6816 }
6917
70- public function isExpired (): bool
71- {
72- return time () >= $ this ->data ('expires ' );
73- }
74-
75- public function refresh (): void
76- {
77- $ newAccessToken = $ this ->oauthProvider ->getAccessToken ('refresh_token ' , [
78- 'grant_type ' => 'refresh_token ' ,
79- 'refresh_token ' => $ this ->getRefreshToken (),
80- ]);
81-
82- $ this ->store ($ newAccessToken );
83- }
84-
8518 public function store (AccessTokenInterface $ token , array $ tenants = null ): void
8619 {
8720 $ this ->dataStorage = [
@@ -93,30 +26,10 @@ public function store(AccessTokenInterface $token, array $tenants = null): void
9326 ];
9427 }
9528
96- public function getUser (): ?array
97- {
98- try {
99- $ jwt = new \XeroAPI \XeroPHP \JWTClaims ();
100- $ jwt ->setTokenId ($ this ->data ('id_token ' ));
101- $ decodedToken = $ jwt ->decode ();
102-
103- return [
104- 'given_name ' => $ decodedToken ->getGivenName (),
105- 'family_name ' => $ decodedToken ->getFamilyName (),
106- 'email ' => $ decodedToken ->getEmail (),
107- 'user_id ' => $ decodedToken ->getXeroUserId (),
108- 'username ' => $ decodedToken ->getPreferredUsername (),
109- 'session_id ' => $ decodedToken ->getGlobalSessionId (),
110- ];
111- } catch (\Throwable $ e ) {
112- return null ;
113- }
114- }
115-
11629 protected function data (string $ key = null )
11730 {
11831 if (! $ this ->exists ()) {
119- throw new \ Exception ('Xero oauth credentials are missing ' );
32+ throw new XeroCredentialsNotFound ('Xero oauth credentials are missing ' );
12033 }
12134
12235 return $ key === null ? $ this ->dataStorage : $ this ->dataStorage [$ key ] ?? null ;
0 commit comments