1: <?php
2: /*
3: * SimpleID
4: *
5: * Copyright (C) Kelvin Mo 2021-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\Base;
24:
25: use SimpleID\Util\Events\BaseEvent;
26:
27: /**
28: * An event to collect scope information.
29: *
30: * Different identity protocols use the concept of *scope* to limit
31: * the extent to which authorisation is provided by the user. This
32: * event is used to collect all the possible scopes that this
33: * SimpleID installation can provide, as well as human-friendly
34: * information on these scopes.
35: *
36: * Scope information is categorised into *types*. Generally each
37: * identity protocol would have a separate type assigned. Currently
38: * the available types are:
39: *
40: * - `openid` for the OpenID 1 and 2 protocols
41: * - `oauth` for OAuth based protocols (including OpenID Connect)
42: *
43: * Listeners should use {@link addScopeInfo()} to add scope information.
44: */
45: class ScopeInfoCollectionEvent extends BaseEvent {
46: /** @var array<string, array<string, mixed>> */
47: protected $scope_info = [];
48:
49: /**
50: * Add scope information.
51: *
52: * The scope information is arranged as an array, with the key being the
53: * protocol specific name of the scope, and the value being an array of
54: * protocol specific settings. Commonly used settings include:
55: *
56: * - `description` - a description of the scope presented to the user in
57: * the consent page
58: * - `weight` - used for sorting multiple scopes when presented to the
59: * user
60: * - `claims` - in OpenID Connect, the claims available in the `userinfo`
61: * endpoint if granted
62: *
63: * @param string $type the type of the scope information
64: * @param array<string, array<string, mixed>> $scopes the scope information
65: * @return void
66: */
67: public function addScopeInfo($type, $scopes) {
68: $this->scope_info = array_merge_recursive($this->scope_info, [ $type => $scopes ]);
69: }
70:
71: /**
72: * Returns scopes for a particular type.
73: *
74: * @param string $type the type of scopes to return
75: * @return array<string> an array of scopes, or an empty array
76: * if no scope information is found for this particular type
77: */
78: public function getScopesForType($type) {
79: if (!isset($this->scope_info[$type])) return [];
80: return array_keys($this->scope_info[$type]);
81: }
82:
83: /**
84: * Returns scope informations for a particular type.
85: *
86: * @param string $type the type of scopes to return
87: * @return array<string, mixed> an array containing scope information for the specified
88: * type, or an empty array if no scope information is found for this
89: * particular type
90: */
91: public function getScopeInfoForType($type) {
92: return $this->scope_info[$type];
93: }
94:
95: /**
96: * Returns the entire registry of scope information.
97: *
98: * The array returned is organised by type, with the name of the type
99: * as the key, and the scope information (equivalent to calling
100: * {@link getScopesForType()}) as the value.
101: *
102: * @return array<string, array<string, mixed>> the scope information
103: */
104: public function getAllScopeInfo() {
105: return $this->scope_info;
106: }
107: }
108:
109: ?>
110: