> */ public static function validate( $value ): Result { if ( ! $value instanceof JsonObject ) { return Result::err( 'msg_malformed', 'msg is an object' ); } $kind = $value->get( 'kind' ); if ( ! is_string( $kind ) || ! in_array( $kind, Constants::MSG_KINDS, true ) ) { return Result::err( 'msg_kind_unknown', 'msg.kind is not in the compiled set' ); } switch ( $kind ) { case 'key.install': // Named on its own so the gateway sees which half of continuity // it left out, rather than a shape error. if ( self::exactly( $value, array( 'kind', 'kid', 'public_key' ) ) ) { return Result::err( 'key_proof_missing', 'key.install carries a possession proof for the incoming key' ); } if ( ! self::exactly( $value, array( 'kind', 'kid', 'public_key', 'proof' ) ) ) { return Result::err( 'msg_malformed', 'key.install carries kind, kid, public_key and proof' ); } $kid = $value->get( 'kid' ); $public_key = $value->get( 'public_key' ); $proof = $value->get( 'proof' ); if ( ! is_string( $kid ) || '' === $kid ) { return Result::err( 'msg_malformed', 'kid is a string' ); } if ( ! is_string( $public_key ) ) { return Result::err( 'msg_malformed', 'public_key is a string' ); } $parsed = \Pielyt\Crypto\Keys::parsePublicKey( $public_key ); if ( ! $parsed->isOk() ) { return Result::err( 'msg_malformed', $parsed->error()->message() ); } if ( ! is_string( $proof ) || '' === $proof ) { return Result::err( 'key_proof_missing', 'proof is a compact JWS' ); } return Result::ok( array( 'kind' => 'key.install', 'kid' => $kid, 'public_key' => $public_key, 'proof' => $proof, ) ); case 'key.retire': if ( ! self::exactly( $value, array( 'kind', 'kid' ) ) ) { return Result::err( 'msg_malformed', 'key.retire carries kind and kid' ); } $kid = $value->get( 'kid' ); if ( ! is_string( $kid ) || '' === $kid ) { return Result::err( 'msg_malformed', 'kid is a string' ); } return Result::ok( array( 'kind' => 'key.retire', 'kid' => $kid, ) ); case 'epoch.set': if ( ! self::exactly( $value, array( 'kind', 'epoch' ) ) ) { return Result::err( 'msg_malformed', 'epoch.set carries kind and epoch' ); } $epoch = $value->get( 'epoch' ); if ( ! is_int( $epoch ) || $epoch < 0 ) { return Result::err( 'msg_malformed', 'epoch is a non-negative integer' ); } return Result::ok( array( 'kind' => 'epoch.set', 'epoch' => $epoch, ) ); case 'binding.status': if ( ! self::exactly( $value, array( 'kind', 'challenge' ) ) ) { return Result::err( 'msg_malformed', 'binding.status carries kind and challenge' ); } $challenge = $value->get( 'challenge' ); if ( ! is_string( $challenge ) || 1 !== preg_match( self::CHALLENGE_PATTERN, $challenge ) ) { return Result::err( 'msg_malformed', 'challenge is 1 to 256 base64url or hex characters' ); } return Result::ok( array( 'kind' => 'binding.status', 'challenge' => $challenge, ) ); case 'user.provision': if ( ! self::exactly( $value, array( 'kind', 'principal', 'role', 'display_name' ) ) ) { return Result::err( 'msg_malformed', 'user.provision carries kind, principal, role and display_name' ); } return self::userMessage( $value, 'user.provision', array( 'principal', 'role', 'display_name' ) ); case 'user.role': if ( ! self::exactly( $value, array( 'kind', 'principal', 'role' ) ) ) { return Result::err( 'msg_malformed', 'user.role carries kind, principal and role' ); } return self::userMessage( $value, 'user.role', array( 'principal', 'role' ) ); case 'user.revoke': if ( ! self::exactly( $value, array( 'kind', 'principal' ) ) ) { return Result::err( 'msg_malformed', 'user.revoke carries kind and principal' ); } return self::userMessage( $value, 'user.revoke', array( 'principal' ) ); default: if ( ! self::exactly( $value, array( 'kind' ) ) ) { return Result::err( 'msg_malformed', $kind . ' carries only kind' ); } return Result::ok( array( 'kind' => $kind ) ); } } /** * @param array $members */ private static function userMessage( JsonObject $value, string $kind, array $members ): Result { $out = array( 'kind' => $kind ); foreach ( $members as $member ) { $v = $value->get( $member ); if ( ! is_string( $v ) || '' === $v ) { return Result::err( 'msg_malformed', $member . ' is a non-empty string' ); } $out[ $member ] = $v; } if ( isset( $out['principal'] ) && 1 !== preg_match( Constants::SUB_PATTERN, $out['principal'] ) ) { return Result::err( 'msg_malformed', 'principal is agent:{id} or user:{id}' ); } return Result::ok( $out ); } /** * @param array $names */ private static function exactly( JsonObject $o, array $names ): bool { $present = $o->keys(); sort( $present ); $wanted = $names; sort( $wanted ); return $present === $wanted; } }