|
20 | 20 |
|
21 | 21 | class DocuWareConnector extends Connector |
22 | 22 | { |
23 | | - public function __construct( |
24 | | - public ConfigWithCredentials|ConfigWithCredentialsTrustedUser $configuration |
25 | | - ) |
26 | | - { |
27 | | - } |
28 | | - |
29 | | - /** |
30 | | - * @throws \Exception |
31 | | - */ |
32 | | - public function resolveBaseUrl(): string |
33 | | - { |
34 | | - return $this->configuration->url . '/DocuWare/Platform'; |
35 | | - } |
36 | | - |
37 | | - public function defaultHeaders(): array |
38 | | - { |
39 | | - return [ |
40 | | - 'Accept' => 'application/json', |
41 | | - ]; |
42 | | - } |
43 | | - |
44 | | - public function defaultConfig(): array |
45 | | - { |
46 | | - return [ |
47 | | - 'timeout' => $this->configuration->requestTimeoutInSeconds, |
48 | | - ]; |
49 | | - } |
50 | | - |
51 | | - protected function defaultAuth(): TokenAuthenticator |
52 | | - { |
53 | | - return new TokenAuthenticator($this->getOrCreateNewOAuthToken()); |
54 | | - } |
55 | | - |
56 | | - /** |
57 | | - * @throws InvalidArgumentException |
58 | | - * @throws \Exception |
59 | | - */ |
60 | | - protected function getOrCreateNewOAuthToken(): string |
61 | | - { |
62 | | - $cache = Cache::store($this->configuration->cacheDriver); |
63 | | - |
64 | | - $cacheKey = 'docuware.oauth.' . $this->configuration->identifier; |
65 | | - |
66 | | - // Check if the token exists in cache and return it if found |
67 | | - if ($cache->has($cacheKey)) { |
68 | | - $token = Crypt::decrypt($cache->get($cacheKey)); |
69 | | - DocuWareOAuthLog::dispatch($this->configuration->url, $this->configuration->username, 'Token retrieved from cache'); |
70 | | - |
71 | | - return $token->accessToken; |
72 | | - } |
73 | | - |
74 | | - // Handle token retrieval for ConfigWithCredentials |
75 | | - if ($this->configuration instanceof ConfigWithCredentials) { |
76 | | - $token = $this->getNewOAuthTokenWithCredentials(); |
77 | | - DocuWareOAuthLog::dispatch($this->configuration->url, $this->configuration->username, 'Token retrieved from API'); |
78 | | - $cache->put($cacheKey, Crypt::encrypt($token), $token->expiresIn - 60); |
79 | | - |
80 | | - return $token->accessToken; |
81 | | - } |
82 | | - |
83 | | - // Handle token retrieval for ConfigWithCredentialsTrustedUser |
84 | | - if ($this->configuration instanceof ConfigWithCredentialsTrustedUser) { |
85 | | - $token = $this->getNewOAuthTokenWithCredentialsTrustedUser(); |
86 | | - DocuWareOAuthLog::dispatch($this->configuration->url, $this->configuration->username, 'Token retrieved from API'); |
87 | | - $cache->put($cacheKey, Crypt::encrypt($token), $token->expiresIn - 60); |
88 | | - |
89 | | - return $token->accessToken; |
90 | | - } |
91 | | - } |
92 | | - |
93 | | - protected function getAuthenticationTokenEndpoint(): IdentityServiceConfiguration |
94 | | - { |
95 | | - $responsibleIdentityServiceResponse = (new GetResponsibleIdentityService($this->configuration->url))->send(); |
96 | | - |
97 | | - $identityServiceConfigurationResponse = (new GetIdentityServiceConfiguration( |
98 | | - identityServiceUrl: $responsibleIdentityServiceResponse->dto()->identityServiceUrl |
99 | | - ))->send(); |
100 | | - |
101 | | - return $identityServiceConfigurationResponse->dto(); |
102 | | - } |
103 | | - |
104 | | - /** |
105 | | - * @throws \Throwable |
106 | | - * @throws \JsonException |
107 | | - */ |
108 | | - protected function getNewOAuthTokenWithCredentials(): RequestTokenDto |
109 | | - { |
110 | | - $requestTokenResponse = (new RequestTokenWithCredentials( |
111 | | - tokenEndpoint: $this->getAuthenticationTokenEndpoint()->tokenEndpoint, |
112 | | - clientId: $this->configuration->clientId, |
113 | | - scope: $this->configuration->scope, |
114 | | - username: $this->configuration->username, |
115 | | - password: $this->configuration->password, |
116 | | - ))->send(); |
117 | | - |
118 | | - throw_if( |
119 | | - $requestTokenResponse->failed(), |
120 | | - trim(preg_replace('/\s\s+/', ' ', Arr::get( |
121 | | - array: $requestTokenResponse->json(), |
122 | | - key: 'error_description', |
123 | | - default: $requestTokenResponse->body() |
124 | | - ))) |
125 | | - ); |
126 | | - |
127 | | - throw_if($requestTokenResponse->dto() == null, 'Token response is null'); |
128 | | - |
129 | | - return $requestTokenResponse->dto(); |
130 | | - } |
131 | | - |
132 | | - /** |
133 | | - * @throws \Throwable |
134 | | - * @throws \JsonException |
135 | | - */ |
136 | | - protected function getNewOAuthTokenWithCredentialsTrustedUser(): RequestTokenDto |
137 | | - { |
138 | | - $requestTokenResponse = (new RequestTokenWithCredentialsTrustedUser( |
139 | | - tokenEndpoint: $this->getAuthenticationTokenEndpoint()->tokenEndpoint, |
140 | | - clientId: $this->configuration->clientId, |
141 | | - scope: $this->configuration->scope, |
142 | | - username: $this->configuration->username, |
143 | | - password: $this->configuration->password, |
144 | | - impersonateName: $this->configuration->impersonatedUsername, |
145 | | - ))->send(); |
146 | | - |
147 | | - throw_if( |
148 | | - $requestTokenResponse->failed(), |
149 | | - trim(preg_replace('/\s\s+/', ' ', Arr::get( |
150 | | - array: $requestTokenResponse->json(), |
151 | | - key: 'error_description', |
152 | | - default: $requestTokenResponse->body() |
153 | | - ))) |
154 | | - ); |
155 | | - |
156 | | - throw_if($requestTokenResponse->dto() == null, 'Token response is null'); |
157 | | - |
158 | | - return $requestTokenResponse->dto(); |
159 | | - } |
| 23 | + public function __construct( |
| 24 | + public ConfigWithCredentials|ConfigWithCredentialsTrustedUser $configuration |
| 25 | + ) {} |
| 26 | + |
| 27 | + /** |
| 28 | + * @throws \Exception |
| 29 | + */ |
| 30 | + public function resolveBaseUrl(): string |
| 31 | + { |
| 32 | + return $this->configuration->url.'/DocuWare/Platform'; |
| 33 | + } |
| 34 | + |
| 35 | + public function defaultHeaders(): array |
| 36 | + { |
| 37 | + return [ |
| 38 | + 'Accept' => 'application/json', |
| 39 | + ]; |
| 40 | + } |
| 41 | + |
| 42 | + public function defaultConfig(): array |
| 43 | + { |
| 44 | + return [ |
| 45 | + 'timeout' => $this->configuration->requestTimeoutInSeconds, |
| 46 | + ]; |
| 47 | + } |
| 48 | + |
| 49 | + protected function defaultAuth(): TokenAuthenticator |
| 50 | + { |
| 51 | + return new TokenAuthenticator($this->getOrCreateNewOAuthToken()); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @throws InvalidArgumentException |
| 56 | + * @throws \Exception |
| 57 | + */ |
| 58 | + protected function getOrCreateNewOAuthToken(): string |
| 59 | + { |
| 60 | + $cache = Cache::store($this->configuration->cacheDriver); |
| 61 | + |
| 62 | + $cacheKey = 'docuware.oauth.'.$this->configuration->identifier; |
| 63 | + |
| 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)); |
| 67 | + DocuWareOAuthLog::dispatch($this->configuration->url, $this->configuration->username, 'Token retrieved from cache'); |
| 68 | + |
| 69 | + return $token->accessToken; |
| 70 | + } |
| 71 | + |
| 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); |
| 77 | + |
| 78 | + return $token->accessToken; |
| 79 | + } |
| 80 | + |
| 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 | + } |
| 89 | + } |
| 90 | + |
| 91 | + protected function getAuthenticationTokenEndpoint(): IdentityServiceConfiguration |
| 92 | + { |
| 93 | + $responsibleIdentityServiceResponse = (new GetResponsibleIdentityService($this->configuration->url))->send(); |
| 94 | + |
| 95 | + $identityServiceConfigurationResponse = (new GetIdentityServiceConfiguration( |
| 96 | + identityServiceUrl: $responsibleIdentityServiceResponse->dto()->identityServiceUrl |
| 97 | + ))->send(); |
| 98 | + |
| 99 | + return $identityServiceConfigurationResponse->dto(); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @throws \Throwable |
| 104 | + * @throws \JsonException |
| 105 | + */ |
| 106 | + protected function getNewOAuthTokenWithCredentials(): RequestTokenDto |
| 107 | + { |
| 108 | + $requestTokenResponse = (new RequestTokenWithCredentials( |
| 109 | + tokenEndpoint: $this->getAuthenticationTokenEndpoint()->tokenEndpoint, |
| 110 | + clientId: $this->configuration->clientId, |
| 111 | + scope: $this->configuration->scope, |
| 112 | + username: $this->configuration->username, |
| 113 | + password: $this->configuration->password, |
| 114 | + ))->send(); |
| 115 | + |
| 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 | + |
| 127 | + return $requestTokenResponse->dto(); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @throws \Throwable |
| 132 | + * @throws \JsonException |
| 133 | + */ |
| 134 | + protected function getNewOAuthTokenWithCredentialsTrustedUser(): RequestTokenDto |
| 135 | + { |
| 136 | + $requestTokenResponse = (new RequestTokenWithCredentialsTrustedUser( |
| 137 | + tokenEndpoint: $this->getAuthenticationTokenEndpoint()->tokenEndpoint, |
| 138 | + clientId: $this->configuration->clientId, |
| 139 | + scope: $this->configuration->scope, |
| 140 | + username: $this->configuration->username, |
| 141 | + password: $this->configuration->password, |
| 142 | + impersonateName: $this->configuration->impersonatedUsername, |
| 143 | + ))->send(); |
| 144 | + |
| 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 | + |
| 156 | + return $requestTokenResponse->dto(); |
| 157 | + } |
160 | 158 | } |
0 commit comments