Skip to content

Commit

Permalink
Update code organization
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenmaguire committed Mar 12, 2018
1 parent ca77bc8 commit f695340
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
14 changes: 13 additions & 1 deletion src/Provider/Exception/EncryptionConfigurationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,17 @@

class EncryptionConfigurationException extends Exception
{
// nothing special here, just a name
/**
* Returns properly formatted exception when response decryption fails.
*
* @return \Stevenmaguire\OAuth2\Client\Provider\Exception\EncryptionConfigurationException
*/
public static function undeterminedEncryption()
{
return new static(
'The given response may be encrypted and sufficient '.
'encryption configuration has not been provided.',
400
);
}
}
46 changes: 26 additions & 20 deletions src/Provider/Keycloak.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,24 @@ public function __construct(array $options = [], array $collaborators = [])
*/
public function decryptResponse($response)
{
if (is_string($response)) {
if ($this->encryptionAlgorithm && $this->encryptionKey) {
$response = json_decode(
json_encode(
JWT::decode(
$response,
$this->encryptionKey,
array($this->encryptionAlgorithm)
)
),
true
);
} else {
throw new EncryptionConfigurationException(
'The given response may be encrypted and sufficient '.
'encryption configuration has not been provided.',
400
);
}
if (!is_string($response)) {
return $response;
}

return $response;
if ($this->usesEncryption()) {
return json_decode(
json_encode(
JWT::decode(
$response,
$this->encryptionKey,
array($this->encryptionAlgorithm)
)
),
true
);
}

throw EncryptionConfigurationException::undeterminedEncryption();
}

/**
Expand Down Expand Up @@ -270,4 +266,14 @@ public function setEncryptionKeyPath($encryptionKeyPath)

return $this;
}

/**
* Checks if provider is configured to use encryption.
*
* @return bool
*/
public function usesEncryption()
{
return (bool) $this->encryptionAlgorithm && $this->encryptionKey;
}
}

0 comments on commit f695340

Please sign in to comment.