forked from simplesamlphp/simplesamlphp-module-oidc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOAuth2Bridge.php
More file actions
66 lines (58 loc) · 1.93 KB
/
OAuth2Bridge.php
File metadata and controls
66 lines (58 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
declare(strict_types=1);
namespace SimpleSAML\Module\oidc\Bridges;
use Defuse\Crypto\Crypto;
use Defuse\Crypto\Key;
use SimpleSAML\Module\oidc\Exceptions\OidcException;
use SimpleSAML\Module\oidc\ModuleConfig;
class OAuth2Bridge
{
public function __construct(
protected readonly ModuleConfig $moduleConfig,
) {
}
/**
* Bridge `encrypt` function, which can be used instead of
* \League\OAuth2\Server\CryptTrait::encrypt()
*
* @param string $unencryptedData
* @param Key|string $encryptionKey
* @return string
* @throws OidcException
*/
public function encrypt(
string $unencryptedData,
null|Key|string $encryptionKey = null,
): string {
$encryptionKey ??= $this->moduleConfig->getEncryptionKey();
try {
return $encryptionKey instanceof Key ?
Crypto::encrypt($unencryptedData, $encryptionKey) :
Crypto::encryptWithPassword($unencryptedData, $encryptionKey);
} catch (\Exception $e) {
throw new OidcException('Error encrypting data: ' . $e->getMessage(), (int)$e->getCode(), $e);
}
}
/**
* Bridge `decrypt` function, which can be used instead of
* \League\OAuth2\Server\CryptTrait::decrypt()
*
* @param string $encryptedData
* @param Key|string $encryptionKey
* @return string
* @throws OidcException
*/
public function decrypt(
string $encryptedData,
null|Key|string $encryptionKey = null,
): string {
$encryptionKey ??= $this->moduleConfig->getEncryptionKey();
try {
return $encryptionKey instanceof Key ?
Crypto::decrypt($encryptedData, $encryptionKey) :
Crypto::decryptWithPassword($encryptedData, $encryptionKey);
} catch (\Exception $e) {
throw new OidcException('Error decrypting data: ' . $e->getMessage(), (int)$e->getCode(), $e);
}
}
}