updated plugin ActivityPub version 9.1.0
This commit is contained in:
@ -51,6 +51,23 @@ class Scope {
|
||||
self::PROFILE,
|
||||
);
|
||||
|
||||
/**
|
||||
* SWICG ActivityPub API Basic Profile canonical scope aliases.
|
||||
*
|
||||
* Advertised in OAuth metadata so Basic Profile clients can discover them,
|
||||
* and accepted in scope requests (any `activitypub:read:*` collapses to
|
||||
* `read`, any `activitypub:write:*` collapses to `write`). Enforcement
|
||||
* stays coarse: there is no per-activity-type access control yet.
|
||||
*
|
||||
* @since 9.0.0
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
const CANONICAL_ALIASES = array(
|
||||
'activitypub:read:all',
|
||||
'activitypub:write:all',
|
||||
);
|
||||
|
||||
/**
|
||||
* Human-readable descriptions for each scope.
|
||||
*
|
||||
@ -79,25 +96,83 @@ class Scope {
|
||||
/**
|
||||
* Validate and filter requested scopes.
|
||||
*
|
||||
* Canonical SWICG ActivityPub API Basic Profile scope names of the form
|
||||
* `activitypub:read:*` and `activitypub:write:*` are normalized to the
|
||||
* plugin's internal `read` and `write` scopes before validation.
|
||||
*
|
||||
* @param string|array $scopes The requested scopes (space-separated string or array).
|
||||
* @return array Valid scopes.
|
||||
*/
|
||||
public static function validate( $scopes ) {
|
||||
if ( is_string( $scopes ) ) {
|
||||
if ( \is_string( $scopes ) ) {
|
||||
$scopes = self::parse( $scopes );
|
||||
}
|
||||
|
||||
if ( ! is_array( $scopes ) ) {
|
||||
if ( ! \is_array( $scopes ) ) {
|
||||
return self::DEFAULT_SCOPES;
|
||||
}
|
||||
|
||||
$valid_scopes = array_intersect( $scopes, self::ALL );
|
||||
$scopes = self::normalize( $scopes );
|
||||
$valid_scopes = \array_intersect( $scopes, self::ALL );
|
||||
|
||||
if ( empty( $valid_scopes ) ) {
|
||||
return self::DEFAULT_SCOPES;
|
||||
}
|
||||
|
||||
return array_values( $valid_scopes );
|
||||
return \array_values( \array_unique( $valid_scopes ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize canonical Basic Profile scope names to internal scopes.
|
||||
*
|
||||
* Maps any `activitypub:read:*` to {@see self::READ} and any
|
||||
* `activitypub:write:*` to {@see self::WRITE}. Unknown values pass through
|
||||
* unchanged so they can be filtered out by the caller.
|
||||
*
|
||||
* @since 9.0.0
|
||||
*
|
||||
* @param array $scopes Requested scope strings.
|
||||
* @return array Normalized scope strings.
|
||||
*/
|
||||
public static function normalize( $scopes ) {
|
||||
if ( ! \is_array( $scopes ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$normalized = array();
|
||||
foreach ( $scopes as $scope ) {
|
||||
if ( ! \is_string( $scope ) || '' === $scope ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( 0 === \strpos( $scope, 'activitypub:read:' ) ) {
|
||||
$normalized[] = self::READ;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( 0 === \strpos( $scope, 'activitypub:write:' ) ) {
|
||||
$normalized[] = self::WRITE;
|
||||
continue;
|
||||
}
|
||||
|
||||
$normalized[] = $scope;
|
||||
}
|
||||
|
||||
return $normalized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the scope identifiers advertised in OAuth authorization-server metadata.
|
||||
*
|
||||
* Includes the plugin's internal scopes plus the SWICG Basic Profile
|
||||
* canonical aliases so spec-aware clients can discover them.
|
||||
*
|
||||
* @since 9.0.0
|
||||
*
|
||||
* @return array Scope identifiers.
|
||||
*/
|
||||
public static function supported() {
|
||||
return \array_merge( self::ALL, self::CANONICAL_ALIASES );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -107,13 +182,13 @@ class Scope {
|
||||
* @return array Scope array.
|
||||
*/
|
||||
public static function parse( $scope_string ) {
|
||||
if ( empty( $scope_string ) || ! is_string( $scope_string ) ) {
|
||||
if ( empty( $scope_string ) || ! \is_string( $scope_string ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$scopes = preg_split( '/\s+/', trim( $scope_string ) );
|
||||
$scopes = \preg_split( '/\s+/', \trim( $scope_string ) );
|
||||
|
||||
return array_filter( array_map( 'trim', $scopes ) );
|
||||
return \array_filter( \array_map( 'trim', $scopes ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -123,11 +198,11 @@ class Scope {
|
||||
* @return string Space-separated scope string.
|
||||
*/
|
||||
public static function to_string( $scopes ) {
|
||||
if ( ! is_array( $scopes ) ) {
|
||||
if ( ! \is_array( $scopes ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return implode( ' ', $scopes );
|
||||
return \implode( ' ', $scopes );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -137,7 +212,7 @@ class Scope {
|
||||
* @return bool True if valid, false otherwise.
|
||||
*/
|
||||
public static function is_valid( $scope ) {
|
||||
return in_array( $scope, self::ALL, true );
|
||||
return \in_array( $scope, self::ALL, true );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -167,7 +242,7 @@ class Scope {
|
||||
* @return bool True if the scope is present.
|
||||
*/
|
||||
public static function contains( $scopes, $scope ) {
|
||||
return is_array( $scopes ) && in_array( $scope, $scopes, true );
|
||||
return \is_array( $scopes ) && \in_array( $scope, $scopes, true );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -177,11 +252,11 @@ class Scope {
|
||||
* @return array Sanitized scopes array.
|
||||
*/
|
||||
public static function sanitize( $value ) {
|
||||
if ( is_string( $value ) ) {
|
||||
if ( \is_string( $value ) ) {
|
||||
$value = self::parse( $value );
|
||||
}
|
||||
|
||||
if ( ! is_array( $value ) ) {
|
||||
if ( ! \is_array( $value ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user