1: <?php
2: /*
3: * SimpleID
4: *
5: * Copyright (C) Kelvin Mo 2015-2025
6: *
7: * This program is free software; you can redistribute it and/or
8: * modify it under the terms of the GNU General Public
9: * License as published by the Free Software Foundation; either
10: * version 2 of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program; if not, write to the Free
19: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20: *
21: */
22:
23: namespace SimpleID\Protocols\Connect;
24:
25: use Psr\Log\LogLevel;
26: use SimpleID\Module;
27: use SimpleID\ModuleManager;
28: use SimpleID\Auth\AuthManager;
29: use SimpleID\Base\RouteContentNegotiationEvent;
30: use SimpleID\Base\ScopeInfoCollectionEvent;
31: use SimpleID\Store\StoreManager;
32:
33: /**
34: * Module that implements the OpenID 2 to OpenID Connect migration
35: * specification.
36: *
37: * @link http://openid.net/specs/openid-connect-migration-1_0.html
38: */
39: class OpenID2MigrationModule extends Module {
40:
41: public function __construct() {
42: parent::__construct();
43:
44: $mgr = ModuleManager::instance();
45: $mgr->loadModule('SimpleID\Protocols\Connect\ConnectModule');
46: }
47:
48: /**
49: * @return void
50: */
51: public function onRouteContentNegotiationEvent(RouteContentNegotiationEvent $event) {
52: if ($event->getRoute() != 'user') return;
53:
54: $content_type = $event->negotiate([ 'text/html', 'application/xml', 'application/xhtml+xml', 'application/json' ]);
55:
56: if ($content_type == 'application/json') {
57: $this->userJSON();
58: $event->stopPropagation();
59: }
60: }
61:
62: /**
63: * Returns the user's OpenID 2.0 verification page.
64: *
65: * @return void
66: * @see SimpleID\Base\UserModule::user()
67: */
68: public function userJSON() {
69: $mgr = ModuleManager::instance();
70:
71: /** @var \SimpleID\Protocols\Connect\ConnectModule $connect_module */
72: $connect_module = $mgr->getModule('SimpleID\Protocols\Connect\ConnectModule');
73: $iss = $connect_module->getCanonicalHost();
74: $store = StoreManager::instance();
75: $user = $store->loadUser($this->f3->get('PARAMS.uid'));
76:
77: if ($user != NULL) {
78: header('Content-Type: application/json');
79: print json_encode([ 'iss' => $iss ]);
80: } else {
81: $this->fatalError($this->f3->get('intl.common.user_not_found', $this->f3->get('PARAMS.uid')), 404);
82: }
83: }
84:
85: /**
86: * @see SimpleID\Protocols\Connect\ConnectBuildClaimsEvent
87: * @return void
88: */
89: public function onConnectBuildClaimsEvent(ConnectBuildClaimsEvent $event) {
90: $context = $event->getContext();
91: $scope = $event->getScope();
92: $user = $event->getUser();
93:
94: if (($context == 'id_token') && in_array('openid2', $scope)) {
95: if (isset($user['openid']['identity'])) {
96: $event->addResult([ 'openid2_id' => $user['openid']['identity'] ]);
97: }
98: }
99: }
100:
101: /**
102: * @see SimpleID\Base\ScopeInfoCollectionEvent
103: * @return void
104: */
105: public function onScopeInfoCollectionEvent(ScopeInfoCollectionEvent $event) {
106: $event->addScopeInfo('oauth', [
107: 'openid2' => [
108: 'description' => $this->f3->get('intl.core.connect.scope.openid2'),
109: 'claims' => [ 'openid2_id' ],
110: 'weight' => -1
111: ]
112: ]);
113: }
114: }
115: ?>
116: