~eduroam/eduroam-configurator

ref: a961e2e019cf63c2fe0df948018451628c594577 eduroam-configurator/src/eduroam/cat/profile.php -rw-r--r-- 10.7 KiB
a961e2e0 — Jørn Åne de Jong Bump php-cat-client 2 years ago
                                                                                
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php declare(strict_types=1);

/*
 * This file is part of the PHP eduroam CAT client
 * A client to download data from https://cat.eduroam.org/
 *
 * Copyright: 2018-2020, Jørn Åne de Jong, Uninett AS <jorn.dejong@uninett.no>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace eduroam\CAT;

use stdClass;

/**
 * Configuration profile.
 *
 * An identity provider may have one or more profiles.  These represent a method
 * to configure eduroam and will typically be available for multiple devices.
 */
class Profile
{
	/**
	 * List of all profiles by CAT base, identity provider and language.
	 *
	 * This profile data structure contains the name of the profile.
	 *
	 * This variable is static to facilitate lazy-loading.
	 * The CAT API has no support to get one identity provider,
	 * so we'll have to get them all at the same time.
	 *
	 * @see https://cat.eduroam.org/doc/UserAPI/tutorial_UserAPI.pkg.html#actions.listProfiles
	 */
	public static $profiles;

	/**
	 * List of all profile attributes by CAT base, identity provider and language.
	 *
	 * This profile data structure contains data about the profile, such as
	 * devices, support information and custom texts.
	 *
	 * This variable is static to facilitate lazy-loading.
	 * The CAT API has no support to get one identity provider,
	 * so we'll have to get them all at the same time.
	 *
	 * @see https://cat.eduroam.org/doc/UserAPI/tutorial_UserAPI.pkg.html#actions.listProfiles
	 */
	public static $profileAttributes;

	/**
	 * CAT instance
	 *
	 * @var CAT
	 */
	private $cat;

	/**
	 * Identity provider Entity ID in CAT API
	 *
	 * @var int
	 */
	private $idpID;

	/**
	 * Profile ID in CAT API
	 *
	 * @var int
	 */
	private $profileID;

	/**
	 * Language flag to use in requests against CAT
	 *
	 * @var string
	 */
	private $lang;

	/**
	 * Construct a lazy loaded profile.
	 *
	 * @param CAT    $cat       CAT instance
	 * @param int    $idpID     Identity provider ID
	 * @param int    $profileID Profile ID
	 * @param string $lang      Language
	 */
	public function __construct( CAT $cat, int $idpID, int $profileID, string $lang = '' )
	{
		$this->cat = $cat;
		$this->idpID = $idpID;
		$this->profileID = $profileID;
		$this->lang = $lang;
	}

	/**
	 * Get all profiles as lazy loaded objects in an indexed array.
	 *
	 * @param CAT    $cat   CAT instance
	 * @param int    $idpID Identity provider ID
	 * @param string $lang  Language
	 *
	 * @return Profile[]
	 */
	public static function getProfilesByIdPEntityID( CAT $cat, int $idpID, string $lang = '' ): array
	{
		static::loadProfilesByIdPEntityID( $cat, $idpID, $lang );
		$profiles = [];
		foreach ( static::$profiles[$cat->getBase()][$idpID][$lang] as $profile ) {
			$profiles[$profile->id] = new self( $cat, $idpID, (int)$profile->id, $lang );
		}

		return $profiles;
	}

	/**
	 * Get raw devices by profile ID.
	 *
	 * This method gets the devices from the already loaded profile attributes,
	 * or returns null.  This way, the devices that were sent with the attributes
	 * may be reused instead of retrieving them again.
	 *
	 * @param CAT    $cat       CAT instance
	 * @param int    $profileID Profile ID
	 * @param string $lang      Language
	 *
	 * @return stdClass[]
	 */
	public static function getRawDevicesByProfileID( CAT $cat, int $profileID, string $lang = '' ): array
	{
		$attr = self::loadProfileAttributesByID( $cat, $profileID, $lang );
		if ( isset( $attr->devices ) ) {
			return $attr->devices;
		}

		return [];
	}

	/**
	 * Get the raw data associated with this profile, containing the name.
	 *
	 * This is the JSON data converted to a PHP object.
	 *
	 * @return stdClass
	 */
	public function getRaw(): stdClass
	{
		self::loadProfileAttributesByID( $this->cat, $this->profileID, $this->lang );

		return static::$profileAttributes[$this->cat->getBase()][$this->profileID][$this->lang];
	}

	/**
	 * Get the raw data of this profile's attributes, containing contact info and
	 * devices.
	 *
	 * This is the JSON data converted to a PHP object.
	 *
	 * @return stdClass
	 */
	public function getRawAttributes(): stdClass
	{
		self::loadProfilesByIdPEntityID( $this->cat, $this->idpID, $this->lang );

		return static::$profiles[$this->cat->getBase()][$this->idpID][$this->lang][$this->profileID];
	}

	/**
	 * Get the ID of this profile as it is stored in the CAT database.
	 *
	 * @return int The profile ID
	 */
	public function getProfileID(): int
	{
		return $this->profileID;
	}

	/**
	 * Get the ID of this identity provider as it is stored in the CAT database.
	 *
	 * @return int The identity provider ID
	 */
	public function getIdpID(): int
	{
		return $this->idpID;
	}

	/**
	 * Get the friendly name of this profile.
	 *
	 * @return string The friendly name of this profile
	 */
	public function getDisplay(): string
	{
		static::loadProfilesByIdPEntityID( $this->cat, $this->idpID, $this->lang );
		if ( !static::$profiles[$this->cat->getBase()][$this->idpID][$this->lang][$this->profileID]->display ) {
			return $this->getIdentityProvider()->getDisplay();
		}

		return $this->getRawAttributes()->display;
	}

	/*
	 * omitting #hasLogo() and #getIdentityProviderName() because these
	 * belong in the IdentityProvider class.
	 */

	/**
	 * Get the support e-mail address for this profile.
	 *
	 * @return null|string support e-mail address for this profile
	 */
	public function getLocalEmail()
	{
		$raw = $this->getRaw();
		if ( isset( $raw->local_email ) ) {
			return $raw->local_email;
		}
	}

	/**
	 * Get the support telephone number address for this profile.
	 *
	 * @return null|string support telephone number address for this profile
	 */
	public function getLocalPhone()
	{
		$raw = $this->getRaw();
		if ( isset( $raw->local_phone ) ) {
			return $raw->local_phone;
		}
	}

	/**
	 * Get the support URL address for this profile.
	 *
	 * @return null|string support URL address for this profile
	 */
	public function getLocalUrl()
	{
		$raw = $this->getRaw();
		if ( isset( $raw->local_url ) ) {
			return $raw->local_url;
		}
	}

	/**
	 * Get the description for this profile in plain unformatted text.
	 * This is displayed on the download page.
	 *
	 * @return null|string support e-mail address for this profile
	 */
	public function getDescription()
	{
		$raw = $this->getRaw();
		if ( isset( $raw->description ) ) {
			return $raw->description;
		}
	}

	/**
	 * Get the supported devices for this profile in an indexed array.
	 *
	 * @return Device[]
	 */
	public function getDevices(): array
	{
		static::loadProfileAttributesByID( $this->cat, $this->profileID );
		$devices = [];
		$addPem = false;
		foreach ( $this->getRaw()->devices as $device ) {
			if ( ( $device->redirect || $device->status === 0 ) && ( !isset( $device->options->hidden ) || !$device->options->hidden ) ) {
				$devices[$device->id] = new Device( $this->cat, $this->idpID, $this->profileID, $device->id, $this->lang );
			}
			//$addPem |= !$device->redirect;
			if ( !$device->redirect ) {
				$addPem = true;
			}
		}
		if ( $addPem ) {
			$devices['x-pem'] = new PemDevice( $this->cat, $this->idpID, $this->profileID, 'eap-config', $this->lang );
		}

		return $devices;
	}

	/**
	 * (undocumented feature)
	 *
	 * GÉANT has a service Managed IdP, which had the code name Silver Bullet
	 * It's possible in CAT to make a Managed IdP profile, but this does not show up
	 * in the eap-config file.
	 *
	 * @return bool silverbullet is set and is not intval(0)
	 */
	public function isSilverBullet(): bool
	{
		return isset( $this->getRaw()->silverbullet ) && 0 !== $this->getRaw()->silverbullet;
	}

	public function getDevice( string $deviceID ): Device
	{
		$devices = $this->getDevices();
		if ( \array_key_exists( $deviceID, $devices ) ) {
			return $devices[$deviceID];
		}
		throw new \DomainException( 'Profile ' . $this->getDisplay() . " has no device ${deviceID}" );
	}

	/**
	 * Determines whether this profile is supported by the identity provider.
	 * A profile is considered supported if one of the following is available:
	 * support email, support phone, local url.
	 *
	 * @return bool This profile is supported
	 */
	public function hasSupport(): bool
	{
		return $this->getLocalEmail() || $this->getLocalPhone() || $this->getLocalUrl();
	}

	/**
	 * Get a lazy loaded instance of the identity provider that is associated with
	 * this profile.
	 *
	 * @return IdentityProvider
	 */
	public function getIdentityProvider(): IdentityProvider
	{
		return new IdentityProvider( $this->cat, $this->idpID, $this->lang );
	}

	/**
	 * Determines whether a redirect is set for this profile.
	 *
	 * @return bool Profile has a redirect set
	 */
	public function isRedirect(): bool
	{
		$raw = $this->getRaw();

		// Special case, a redirect-only profile will only have one devices
		// that does not have a name, so the call to getDevices will fail.
		if ( isset( $raw->devices ) && 1 === \count( $raw->devices ) && isset( $raw->devices[0] ) ) {
			if ( isset( $raw->devices[0]->redirect ) ) {
				if ( $raw->devices[0]->redirect ) {
					return true;
				}
			}
		}

		// Return true if every device is a redirect
		foreach ( $this->getDevices() as $device ) {
			if ( !$device->isProfileRedirect() ) {
				return false;
			}
		}

		return true;
	}
	/**
	 * Attempt to get the redirect url for this profile.
	 *
	 * @return ?string The redirect URL
	 */
	public function getRedirectUrl(): ?string{
		$raw = $this->getRaw();
		if ( isset( $raw->devices ) && 1 === \count( $raw->devices ) && isset( $raw->devices[0] ) ) {
			if ( isset( $raw->devices[0]->redirect ) ) {
				if ( $raw->devices[0]->redirect ) {
					return $raw->devices[0]->redirect;
				}
			}
		}
		return null;
	}

	/**
	 * Fill lazy loaded $profiles.
	 *
	 * @param CAT    $cat   CAT instance
	 * @param int    $idpID Identity provider ID
	 * @param string $lang  Language
	 */
	private static function loadProfilesByIdPEntityID( CAT $cat, int $idpID, string $lang = '' ): void
	{
		if ( !isset( static::$profiles[$cat->getBase()][$idpID][$lang] ) ) {
			foreach ( $cat->listProfiles( $idpID, $lang ) as $profile ) {
				static::$profiles[$cat->getBase()][$idpID][$lang][$profile->id] = $profile;
			}
		}
	}

	/**
	 * Fill lazy loaded profile with its attributes.
	 *
	 * @param CAT    $cat       CAT instance
	 * @param int    $profileID Profile ID
	 * @param string $lang      Language
	 */
	private static function loadProfileAttributesByID( CAT $cat, int $profileID, string $lang = '' ): stdClass
	{
		if ( !isset( static::$profileAttributes[$cat->getBase()][$profileID][$lang] ) ) {
			static::$profileAttributes[$cat->getBase()][$profileID][$lang] = $cat->profileAttributes( $profileID, $lang );
		}
		if ( !isset( static::$profileAttributes[$cat->getBase()][$profileID][$lang] ) ) {
			throw new \DomainException( 'Unable to load profile attributes for profile ' . $profileID );
		}

		return static::$profileAttributes[$cat->getBase()][$profileID][$lang];
	}
}