|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* The MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2014-2019 Spomky-Labs |
|
9
|
|
|
* |
|
10
|
|
|
* This software may be modified and distributed under the terms |
|
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Component\OpenIdConnect\IFrame; |
|
15
|
|
|
|
|
16
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
|
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
19
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
20
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
21
|
|
|
|
|
22
|
|
|
final class IFrameEndpoint implements MiddlewareInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var ResponseFactoryInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $responseFactory; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* IFrameEndpoint constructor. |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(ResponseFactoryInterface $responseFactory) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->responseFactory = $responseFactory; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
38
|
|
|
{ |
|
39
|
|
|
$content = $this->renderTemplate(); |
|
40
|
|
|
|
|
41
|
|
|
$response = $this->responseFactory->createResponse(); |
|
42
|
|
|
$headers = ['Content-Type' => 'text/html; charset=UTF-8', 'Cache-Control' => 'no-cache, no-store, max-age=0, must-revalidate, private', 'Pragma' => 'no-cache']; |
|
43
|
|
|
foreach ($headers as $k => $v) { |
|
44
|
|
|
$response = $response->withHeader($k, $v); |
|
45
|
|
|
} |
|
46
|
|
|
$response->getBody()->write($content); |
|
47
|
|
|
|
|
48
|
|
|
return $response; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
private function renderTemplate(): string |
|
52
|
|
|
{ |
|
53
|
|
|
return <<<'EOT' |
|
54
|
|
|
<html> |
|
55
|
|
|
<head> |
|
56
|
|
|
<meta http-equiv="content-type" content="text/html;charset=UTF-8" /> |
|
57
|
|
|
<title>OP iFrame</title> |
|
58
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/sha256-min.js"></script> |
|
59
|
|
|
window.addEventListener("message",receiveMessage, false); |
|
60
|
|
|
|
|
61
|
|
|
function getCookie(c_name) |
|
62
|
|
|
{ |
|
63
|
|
|
var i,x,y,ARRcookies=document.cookie.split(";"); |
|
64
|
|
|
for (i=0;i<ARRcookies.length;i++) { |
|
65
|
|
|
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("=")); |
|
66
|
|
|
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1); |
|
67
|
|
|
x=x.replace(/^\s+|\s+$/g,""); |
|
68
|
|
|
if (x==c_name) { |
|
69
|
|
|
return unescape(y); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
function receiveMessage(e){ |
|
75
|
|
|
if ( e.origin !== origin) { |
|
76
|
|
|
console.log(e.origin + ' !== ' + origin); |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
var state = ''; |
|
80
|
|
|
var parts = e.data.split(' '); |
|
81
|
|
|
var client_id = parts[0]; |
|
82
|
|
|
var session_state = parts[1]; |
|
83
|
|
|
var ss_parts = session_state.split('.'); |
|
84
|
|
|
var salt = ss_parts[1]; |
|
85
|
|
|
|
|
86
|
|
|
var ops = getCookie('ops'); |
|
87
|
|
|
var ss = CryptoJS.SHA256(client_id + e.origin + ops + salt) + "." + salt; |
|
88
|
|
|
if (session_state == ss) { |
|
89
|
|
|
state = 'unchanged'; |
|
90
|
|
|
} else { |
|
91
|
|
|
state = 'changed'; |
|
92
|
|
|
} |
|
93
|
|
|
e.source.postMessage(state, e.origin); |
|
94
|
|
|
}; |
|
95
|
|
|
//]]></script> |
|
96
|
|
|
</head> |
|
97
|
|
|
<body> |
|
98
|
|
|
</body> |
|
99
|
|
|
</html> |
|
100
|
|
|
EOT; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|