P::OBTAIN_OWN_ACCOUNT_INFO, PublicScopeRepository::ACCOUNT_EMAIL => P::OBTAIN_ACCOUNT_EMAIL, PublicScopeRepository::MINECRAFT_SERVER_SESSION => P::MINECRAFT_SERVER_SESSION, InternalScopeRepository::INTERNAL_ACCOUNT_INFO => P::OBTAIN_EXTENDED_ACCOUNT_INFO, ]; /** * The OpenID Connect scopes, which grant the same permissions as our own scopes. * They're kept separately, since the permissions must be converted back only to our own scopes names. */ private const array ALIASES_TO_PERMISSIONS = [ PublicScopeRepository::PROFILE => P::OBTAIN_OWN_ACCOUNT_INFO, PublicScopeRepository::EMAIL => P::OBTAIN_ACCOUNT_EMAIL, ]; /** * Converts the public scope name to the RBAC permission name. Unknown values are returned as is. */ public static function toPermission(string $scope): string { return self::PUBLIC_SCOPES_TO_PERMISSIONS[$scope] ?? self::ALIASES_TO_PERMISSIONS[$scope] ?? $scope; } /** * Converts the RBAC permission name to the public scope name. Unknown values are returned as is. * * @return ($permission is non-empty-string ? non-empty-string : string) */ public static function toScope(string $permission): string { return array_flip(self::PUBLIC_SCOPES_TO_PERMISSIONS)[$permission] ?? $permission; } /** * Converts the RBAC permissions names to the public scopes names and removes the duplicates. * When both the OpenID Connect scope and our own scope with the same permission are present * (e.g. "profile" and "account_info"), only the OpenID Connect one is kept. * * @param iterable $scopes * @return list */ public static function normalizeScopes(iterable $scopes): array { $result = []; foreach ($scopes as $scope) { $result[] = self::toScope($scope); } $result = array_unique($result); foreach (self::ALIASES_TO_PERMISSIONS as $alias => $permission) { if (in_array($alias, $result, true)) { $result = array_diff($result, [self::toScope($permission)]); } } return array_values($result); } }