1111use CodebarAg \DocuWare \Requests \Authentication \OAuth \GetResponsibleIdentityService ;
1212use CodebarAg \DocuWare \Requests \Authentication \OAuth \RequestTokenWithCredentials ;
1313use CodebarAg \DocuWare \Requests \Authentication \OAuth \RequestTokenWithCredentialsTrustedUser ;
14+ use Illuminate \Support \Arr ;
1415use Illuminate \Support \Facades \Cache ;
1516use Illuminate \Support \Facades \Crypt ;
1617use Psr \SimpleCache \InvalidArgumentException ;
@@ -52,39 +53,39 @@ protected function defaultAuth(): TokenAuthenticator
5253
5354 /**
5455 * @throws InvalidArgumentException
56+ * @throws \Exception
5557 */
56- protected function getOrCreateNewOAuthToken ()
58+ protected function getOrCreateNewOAuthToken (): string
5759 {
5860 $ cache = Cache::store ($ this ->configuration ->cacheDriver );
5961
60- // get instance name of $this->configuration as string
61- $ instanceName = get_class ($ this ->configuration );
62-
63- $ cacheKey = 'docuware.oauth. ' .$ instanceName .'. ' .$ this ->configuration ->url .'. ' .($ this ->configuration ->username ?? '' );
64-
65- $ token = null ;
66-
67- if ($ cache ->has (key: $ cacheKey )) {
68- $ token = Crypt::decrypt ($ cache ->get (key: $ cacheKey ));
62+ $ cacheKey = 'docuware.oauth. ' .$ this ->configuration ->identifier ;
6963
64+ // Check if the token exists in cache and return it if found
65+ if ($ cache ->has ($ cacheKey )) {
66+ $ token = Crypt::decrypt ($ cache ->get ($ cacheKey ));
7067 DocuWareOAuthLog::dispatch ($ this ->configuration ->url , $ this ->configuration ->username , 'Token retrieved from cache ' );
71- } else {
72- if ($ this ->configuration instanceof ConfigWithCredentials) {
73- $ token = $ this ->getNewOAuthTokenWithCredentials ();
7468
75- DocuWareOAuthLog::dispatch ($ this ->configuration ->url , $ this ->configuration ->username , 'Token retrieved from API ' );
76- }
77-
78- if ($ this ->configuration instanceof ConfigWithCredentialsTrustedUser) {
79- $ token = $ this ->getNewOAuthTokenWithCredentialsTrustedUser ();
69+ return $ token ->accessToken ;
70+ }
8071
81- DocuWareOAuthLog::dispatch ($ this ->configuration ->url , $ this ->configuration ->username , 'Token retrieved from API ' );
82- }
72+ // Handle token retrieval for ConfigWithCredentials
73+ if ($ this ->configuration instanceof ConfigWithCredentials) {
74+ $ token = $ this ->getNewOAuthTokenWithCredentials ();
75+ DocuWareOAuthLog::dispatch ($ this ->configuration ->url , $ this ->configuration ->username , 'Token retrieved from API ' );
76+ $ cache ->put ($ cacheKey , Crypt::encrypt ($ token ), $ token ->expiresIn - 60 );
8377
84- $ cache -> put (key: $ cacheKey , value: Crypt:: encrypt ( $ token), ttl: $ token -> expiresIn - 60 ) ;
78+ return $ token-> accessToken ;
8579 }
8680
87- return $ token ->accessToken ;
81+ // Handle token retrieval for ConfigWithCredentialsTrustedUser
82+ if ($ this ->configuration instanceof ConfigWithCredentialsTrustedUser) {
83+ $ token = $ this ->getNewOAuthTokenWithCredentialsTrustedUser ();
84+ DocuWareOAuthLog::dispatch ($ this ->configuration ->url , $ this ->configuration ->username , 'Token retrieved from API ' );
85+ $ cache ->put ($ cacheKey , Crypt::encrypt ($ token ), $ token ->expiresIn - 60 );
86+
87+ return $ token ->accessToken ;
88+ }
8889 }
8990
9091 protected function getAuthenticationTokenEndpoint (): IdentityServiceConfiguration
@@ -98,6 +99,10 @@ protected function getAuthenticationTokenEndpoint(): IdentityServiceConfiguratio
9899 return $ identityServiceConfigurationResponse ->dto ();
99100 }
100101
102+ /**
103+ * @throws \Throwable
104+ * @throws \JsonException
105+ */
101106 protected function getNewOAuthTokenWithCredentials (): RequestTokenDto
102107 {
103108 $ requestTokenResponse = (new RequestTokenWithCredentials (
@@ -108,9 +113,24 @@ protected function getNewOAuthTokenWithCredentials(): RequestTokenDto
108113 password: $ this ->configuration ->password ,
109114 ))->send ();
110115
116+ throw_if (
117+ $ requestTokenResponse ->failed (),
118+ trim (preg_replace ('/\s\s+/ ' , ' ' , Arr::get (
119+ array: $ requestTokenResponse ->json (),
120+ key: 'error_description ' ,
121+ default: $ requestTokenResponse ->body ()
122+ )))
123+ );
124+
125+ throw_if ($ requestTokenResponse ->dto () == null , 'Token response is null ' );
126+
111127 return $ requestTokenResponse ->dto ();
112128 }
113129
130+ /**
131+ * @throws \Throwable
132+ * @throws \JsonException
133+ */
114134 protected function getNewOAuthTokenWithCredentialsTrustedUser (): RequestTokenDto
115135 {
116136 $ requestTokenResponse = (new RequestTokenWithCredentialsTrustedUser (
@@ -122,6 +142,17 @@ protected function getNewOAuthTokenWithCredentialsTrustedUser(): RequestTokenDto
122142 impersonateName: $ this ->configuration ->impersonatedUsername ,
123143 ))->send ();
124144
145+ throw_if (
146+ $ requestTokenResponse ->failed (),
147+ trim (preg_replace ('/\s\s+/ ' , ' ' , Arr::get (
148+ array: $ requestTokenResponse ->json (),
149+ key: 'error_description ' ,
150+ default: $ requestTokenResponse ->body ()
151+ )))
152+ );
153+
154+ throw_if ($ requestTokenResponse ->dto () == null , 'Token response is null ' );
155+
125156 return $ requestTokenResponse ->dto ();
126157 }
127158}
0 commit comments