-
Notifications
You must be signed in to change notification settings - Fork 39
/
ResponseHandler.php
118 lines (94 loc) · 4.59 KB
/
ResponseHandler.php
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
use Jose\Component\Core\AlgorithmManager;
use Jose\Component\Core\JWK;
use Jose\Component\KeyManagement\JWKFactory;
use Jose\Component\Signature\Algorithm\RS256;
use Jose\Component\Signature\JWSBuilder;
use Jose\Component\Signature\Serializer\JWSSerializerManager;
use Jose\Component\Signature\Serializer\CompactSerializer as JWSSerializer;
use Jose\Component\Signature\JWSVerifier;
use Jose\Component\Signature\JWSLoader;
use Jose\Component\Encryption\Algorithm\KeyEncryption\A256KW;
use Jose\Component\Encryption\Algorithm\ContentEncryption\A256CBCHS512;
use Jose\Component\Encryption\Compression\CompressionMethodManager;
use Jose\Component\Encryption\Compression\Deflate;
use Jose\Component\Encryption\JWEBuilder;
use Jose\Component\Encryption\Serializer\JWESerializerManager;
use Jose\Component\Encryption\Serializer\CompactSerializer as JWESerializer;
use Jose\Component\Encryption\JWEDecrypter;
abstract class ResponseHandler {
const TOKEN_PRIVATE_KEY = "../cert/spid-sp.pem";
const TOKEN_PUBLIC_CERT = "../cert/spid-sp.crt";
const DEFAULT_SECRET = "";
const DEFAULT_TOKEN_EXPIRATION_TIME = 1200;
function __construct($issuer, $config) {
$this->issuer = $issuer;
$this->config = $config;
}
function set($key, $value) {
$this->$key = $value;
}
function get($key) {
return $this->$key;
}
abstract public function sendResponse($redirect_uri, $data, $state);
protected function makeJWE($payload, $exp_time, $iss, $aud, $secret): string {
$iat = new DateTimeImmutable();
$exp_time = $exp_time?: DEFAULT_TOKEN_EXPIRATION_TIME;
$exp = $iat->modify("+".$exp_time." seconds")->getTimestamp();
$data = [
'iss' => $iss, // Issuer - spDomain
'aud' => $aud, // Audience - Redirect_uri
'iat' => $iat->getTimestamp(), // Issued at: time when the token was generated
'nbf' => $iat->getTimestamp(), // Not before
'exp' => $exp, // Expire
'data' => $payload, // Authentication Data
];
$keyEncryptionAlgorithmManager = new AlgorithmManager([ new A256KW() ]);
$contentEncryptionAlgorithmManager = new AlgorithmManager([ new A256CBCHS512() ]);
$compressionMethodManager = new CompressionMethodManager([ new Deflate() ]);
$jweBuilder = new JWEBuilder(
$keyEncryptionAlgorithmManager,
$contentEncryptionAlgorithmManager,
$compressionMethodManager
);
$jwk = JWKFactory::createFromSecret($secret?:DEFAULT_SECRET);
$jwe = $jweBuilder
->create()
->withPayload(json_encode($data))
->withSharedProtectedHeader([
'alg' => 'A256KW',
'enc' => 'A256CBC-HS512',
'zip' => 'DEF'
])
->addRecipient($jwk)
->build();
$serializer = new JWESerializer();
$token = $serializer->serialize($jwe, 0);
return $token;
}
protected function makeJWS($payload, $exp_time, $iss, $aud, $jwk_pem): string {
$iat = new DateTimeImmutable();
$exp_time = $exp_time?: DEFAULT_TOKEN_EXPIRATION_TIME;
$exp = $iat->modify("+".$exp_time." seconds")->getTimestamp();
$data = [
'iss' => $iss, // Issuer - spDomain
'aud' => $aud, // Audience - Redirect_uri
'iat' => $iat->getTimestamp(), // Issued at: time when the token was generated
'nbf' => $iat->getTimestamp(), // Not before
'exp' => $exp, // Expire
'data' => $payload, // Authentication Data
];
$algorithmManager = new AlgorithmManager([new RS256()]);
$jwk = JWKFactory::createFromKeyFile($jwk_pem);
$jwsBuilder = new JWSBuilder($algorithmManager);
$jws = $jwsBuilder
->create()
->withPayload(json_encode($data))
->addSignature($jwk, ['alg' => 'RS256'])
->build();
$serializer = new JWSSerializer();
$token = $serializer->serialize($jws, 0);
return $token;
}
}