Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
StanBarrows committed Nov 13, 2024
1 parent 598b78f commit 6c3478d
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 138 deletions.
272 changes: 135 additions & 137 deletions src/Connectors/DocuWareConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,141 +20,139 @@

class DocuWareConnector extends Connector
{
public function __construct(
public ConfigWithCredentials|ConfigWithCredentialsTrustedUser $configuration
)
{
}

/**
* @throws \Exception
*/
public function resolveBaseUrl(): string
{
return $this->configuration->url . '/DocuWare/Platform';
}

public function defaultHeaders(): array
{
return [
'Accept' => 'application/json',
];
}

public function defaultConfig(): array
{
return [
'timeout' => $this->configuration->requestTimeoutInSeconds,
];
}

protected function defaultAuth(): TokenAuthenticator
{
return new TokenAuthenticator($this->getOrCreateNewOAuthToken());
}

/**
* @throws InvalidArgumentException
* @throws \Exception
*/
protected function getOrCreateNewOAuthToken(): string
{
$cache = Cache::store($this->configuration->cacheDriver);

$cacheKey = 'docuware.oauth.' . $this->configuration->identifier;

// Check if the token exists in cache and return it if found
if ($cache->has($cacheKey)) {
$token = Crypt::decrypt($cache->get($cacheKey));
DocuWareOAuthLog::dispatch($this->configuration->url, $this->configuration->username, 'Token retrieved from cache');

return $token->accessToken;
}

// Handle token retrieval for ConfigWithCredentials
if ($this->configuration instanceof ConfigWithCredentials) {
$token = $this->getNewOAuthTokenWithCredentials();
DocuWareOAuthLog::dispatch($this->configuration->url, $this->configuration->username, 'Token retrieved from API');
$cache->put($cacheKey, Crypt::encrypt($token), $token->expiresIn - 60);

return $token->accessToken;
}

// Handle token retrieval for ConfigWithCredentialsTrustedUser
if ($this->configuration instanceof ConfigWithCredentialsTrustedUser) {
$token = $this->getNewOAuthTokenWithCredentialsTrustedUser();
DocuWareOAuthLog::dispatch($this->configuration->url, $this->configuration->username, 'Token retrieved from API');
$cache->put($cacheKey, Crypt::encrypt($token), $token->expiresIn - 60);

return $token->accessToken;
}
}

protected function getAuthenticationTokenEndpoint(): IdentityServiceConfiguration
{
$responsibleIdentityServiceResponse = (new GetResponsibleIdentityService($this->configuration->url))->send();

$identityServiceConfigurationResponse = (new GetIdentityServiceConfiguration(
identityServiceUrl: $responsibleIdentityServiceResponse->dto()->identityServiceUrl
))->send();

return $identityServiceConfigurationResponse->dto();
}

/**
* @throws \Throwable
* @throws \JsonException
*/
protected function getNewOAuthTokenWithCredentials(): RequestTokenDto
{
$requestTokenResponse = (new RequestTokenWithCredentials(
tokenEndpoint: $this->getAuthenticationTokenEndpoint()->tokenEndpoint,
clientId: $this->configuration->clientId,
scope: $this->configuration->scope,
username: $this->configuration->username,
password: $this->configuration->password,
))->send();

throw_if(
$requestTokenResponse->failed(),
trim(preg_replace('/\s\s+/', ' ', Arr::get(
array: $requestTokenResponse->json(),
key: 'error_description',
default: $requestTokenResponse->body()
)))
);

throw_if($requestTokenResponse->dto() == null, 'Token response is null');

return $requestTokenResponse->dto();
}

/**
* @throws \Throwable
* @throws \JsonException
*/
protected function getNewOAuthTokenWithCredentialsTrustedUser(): RequestTokenDto
{
$requestTokenResponse = (new RequestTokenWithCredentialsTrustedUser(
tokenEndpoint: $this->getAuthenticationTokenEndpoint()->tokenEndpoint,
clientId: $this->configuration->clientId,
scope: $this->configuration->scope,
username: $this->configuration->username,
password: $this->configuration->password,
impersonateName: $this->configuration->impersonatedUsername,
))->send();

throw_if(
$requestTokenResponse->failed(),
trim(preg_replace('/\s\s+/', ' ', Arr::get(
array: $requestTokenResponse->json(),
key: 'error_description',
default: $requestTokenResponse->body()
)))
);

throw_if($requestTokenResponse->dto() == null, 'Token response is null');

return $requestTokenResponse->dto();
}
public function __construct(
public ConfigWithCredentials|ConfigWithCredentialsTrustedUser $configuration
) {}

/**
* @throws \Exception
*/
public function resolveBaseUrl(): string
{
return $this->configuration->url.'/DocuWare/Platform';
}

public function defaultHeaders(): array
{
return [
'Accept' => 'application/json',
];
}

public function defaultConfig(): array
{
return [
'timeout' => $this->configuration->requestTimeoutInSeconds,
];
}

protected function defaultAuth(): TokenAuthenticator
{
return new TokenAuthenticator($this->getOrCreateNewOAuthToken());
}

/**
* @throws InvalidArgumentException
* @throws \Exception
*/
protected function getOrCreateNewOAuthToken(): string
{
$cache = Cache::store($this->configuration->cacheDriver);

$cacheKey = 'docuware.oauth.'.$this->configuration->identifier;

// Check if the token exists in cache and return it if found
if ($cache->has($cacheKey)) {
$token = Crypt::decrypt($cache->get($cacheKey));
DocuWareOAuthLog::dispatch($this->configuration->url, $this->configuration->username, 'Token retrieved from cache');

return $token->accessToken;
}

// Handle token retrieval for ConfigWithCredentials
if ($this->configuration instanceof ConfigWithCredentials) {
$token = $this->getNewOAuthTokenWithCredentials();
DocuWareOAuthLog::dispatch($this->configuration->url, $this->configuration->username, 'Token retrieved from API');
$cache->put($cacheKey, Crypt::encrypt($token), $token->expiresIn - 60);

return $token->accessToken;
}

// Handle token retrieval for ConfigWithCredentialsTrustedUser
if ($this->configuration instanceof ConfigWithCredentialsTrustedUser) {
$token = $this->getNewOAuthTokenWithCredentialsTrustedUser();
DocuWareOAuthLog::dispatch($this->configuration->url, $this->configuration->username, 'Token retrieved from API');
$cache->put($cacheKey, Crypt::encrypt($token), $token->expiresIn - 60);

return $token->accessToken;
}
}

protected function getAuthenticationTokenEndpoint(): IdentityServiceConfiguration
{
$responsibleIdentityServiceResponse = (new GetResponsibleIdentityService($this->configuration->url))->send();

$identityServiceConfigurationResponse = (new GetIdentityServiceConfiguration(
identityServiceUrl: $responsibleIdentityServiceResponse->dto()->identityServiceUrl
))->send();

return $identityServiceConfigurationResponse->dto();
}

/**
* @throws \Throwable
* @throws \JsonException
*/
protected function getNewOAuthTokenWithCredentials(): RequestTokenDto
{
$requestTokenResponse = (new RequestTokenWithCredentials(
tokenEndpoint: $this->getAuthenticationTokenEndpoint()->tokenEndpoint,
clientId: $this->configuration->clientId,
scope: $this->configuration->scope,
username: $this->configuration->username,
password: $this->configuration->password,
))->send();

throw_if(
$requestTokenResponse->failed(),
trim(preg_replace('/\s\s+/', ' ', Arr::get(
array: $requestTokenResponse->json(),
key: 'error_description',
default: $requestTokenResponse->body()
)))
);

throw_if($requestTokenResponse->dto() == null, 'Token response is null');

return $requestTokenResponse->dto();
}

/**
* @throws \Throwable
* @throws \JsonException
*/
protected function getNewOAuthTokenWithCredentialsTrustedUser(): RequestTokenDto
{
$requestTokenResponse = (new RequestTokenWithCredentialsTrustedUser(
tokenEndpoint: $this->getAuthenticationTokenEndpoint()->tokenEndpoint,
clientId: $this->configuration->clientId,
scope: $this->configuration->scope,
username: $this->configuration->username,
password: $this->configuration->password,
impersonateName: $this->configuration->impersonatedUsername,
))->send();

throw_if(
$requestTokenResponse->failed(),
trim(preg_replace('/\s\s+/', ' ', Arr::get(
array: $requestTokenResponse->json(),
key: 'error_description',
default: $requestTokenResponse->body()
)))
);

throw_if($requestTokenResponse->dto() == null, 'Token response is null');

return $requestTokenResponse->dto();
}
}
2 changes: 1 addition & 1 deletion src/DTO/Config/ConfigWithCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class ConfigWithCredentials

public string $url;

public string $passphrase;
public ?string $passphrase;

public string $cacheDriver;

Expand Down

0 comments on commit 6c3478d

Please sign in to comment.