|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CornellCustomDev\LaravelStarterKit\CUAuth\DataObjects; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
| 6 | + |
| 7 | +class ShibIdentity |
| 8 | +{ |
| 9 | + // Shibboleth fields generally available from either cit or weill IdPs. |
| 10 | + public const SHIB_FIELDS = [ |
| 11 | + 'Shib_Application_ID', // <vhost|applicationId> |
| 12 | + 'Shib_Authentication_Instant', // YYYY-MM-DDT00:00:00.000Z |
| 13 | + 'Shib_Identity_Provider', // https://shibidp.cit.cornell.edu/idp/shibboleth|https://login.weill.cornell.edu/idp |
| 14 | + 'Shib_Session_Expires', // timestamp |
| 15 | + 'Shib_Session_Inactivity', // timestamp |
| 16 | + 'displayName', // John Doe |
| 17 | + 'eduPersonAffiliations', // employee;member;staff |
| 18 | + |
| 19 | + 'eduPersonScopedAffiliation', // employee@[med.]cornell.edu;member@[med.]cornell.edu;[email protected] |
| 20 | + 'givenName', // John |
| 21 | + 'mail', // alias email |
| 22 | + 'sn', // Doe |
| 23 | + 'uid', // netid|cwid |
| 24 | + ]; |
| 25 | + |
| 26 | + public function __construct( |
| 27 | + public readonly string $idp, |
| 28 | + public readonly string $uid, |
| 29 | + public readonly string $displayName = '', |
| 30 | + public readonly string $email = '', |
| 31 | + public readonly array $serverVars = [], |
| 32 | + ) {} |
| 33 | + |
| 34 | + /** |
| 35 | + * Shibboleth server variables will be retrieved from the request if not provided. |
| 36 | + */ |
| 37 | + public static function fromServerVars(?array $serverVars = null): self |
| 38 | + { |
| 39 | + if (empty($serverVars)) { |
| 40 | + $serverVars = app('request')->server(); |
| 41 | + } |
| 42 | + |
| 43 | + return new ShibIdentity( |
| 44 | + idp: $serverVars['Shib_Identity_Provider'] ?? '', |
| 45 | + uid: $serverVars['uid'] ?? '', |
| 46 | + displayName: $serverVars['displayName'] |
| 47 | + ?? $serverVars['cn'] |
| 48 | + ?? trim(($serverVars['givenName'] ?? '').' '.($serverVars['sn'] ?? '')), |
| 49 | + email: $serverVars['eduPersonPrincipalName'] |
| 50 | + ?? $serverVars['mail'] ?? '', |
| 51 | + serverVars: $serverVars, |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + public static function getRemoteUser(?Request $request = null): ?string |
| 56 | + { |
| 57 | + if (empty($request)) { |
| 58 | + $request = app('request'); |
| 59 | + } |
| 60 | + |
| 61 | + // If this is a local development environment, allow the local override. |
| 62 | + $remote_user_override = self::getRemoteUserOverride(); |
| 63 | + |
| 64 | + // Apache mod_shib populates the remote user variable if someone is logged in. |
| 65 | + return $request->server(config('cu-auth.apache_shib_user_variable')) ?: $remote_user_override; |
| 66 | + } |
| 67 | + |
| 68 | + public static function getRemoteUserOverride(): ?string |
| 69 | + { |
| 70 | + // If this is a local development environment, allow the local override. |
| 71 | + return app()->isLocal() ? config('cu-auth.remote_user_override') : null; |
| 72 | + } |
| 73 | + |
| 74 | + public function isCornellIdP(): bool |
| 75 | + { |
| 76 | + return str_contains($this->idp, 'cit.cornell.edu'); |
| 77 | + } |
| 78 | + |
| 79 | + public function isWeillIdP(): bool |
| 80 | + { |
| 81 | + return str_contains($this->idp, 'weill.cornell.edu'); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Provides a uid that is unique across Cornell IdPs. |
| 86 | + */ |
| 87 | + public function uniqueUid(): string |
| 88 | + { |
| 89 | + return match (true) { |
| 90 | + $this->isCornellIdP() => $this->uid, |
| 91 | + $this->isWeillIdP() => $this->uid.'_w', |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Returns the primary email ([email protected]|[email protected]) if available, otherwise the alias email. |
| 97 | + */ |
| 98 | + public function email(): string |
| 99 | + { |
| 100 | + return $this->email; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Returns the display name if available, otherwise the common name, fallback is "givenName sn". |
| 105 | + */ |
| 106 | + public function name(): string |
| 107 | + { |
| 108 | + return $this->displayName; |
| 109 | + } |
| 110 | +} |
0 commit comments