Skip to content

Commit

Permalink
feature userAccessPolicy: code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
light-source committed Jan 10, 2025
1 parent 515ac62 commit d5417db
Show file tree
Hide file tree
Showing 47 changed files with 134 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import type { imageCaptchaConfigSchema } from "@imageCaptchaConfig/imageCaptchaConfigSchema.js";
import type { z } from "zod";
import type {imageCaptchaConfigSchema} from "@imageCaptchaConfig/imageCaptchaConfigSchema.js";

type ImageCaptchaConfig = z.infer<typeof imageCaptchaConfigSchema>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import { number, object } from "zod";

const imageCaptchaConfigSchema = object({
solvedCount: number().optional(),
unsolvedCount: number().optional(),
solvedCount: number().optional(),
unsolvedCount: number().optional(),
});

export { imageCaptchaConfigSchema };
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {ApiEndpoint} from "@api/apiEndpoint.js";
import type {RulesStorage} from "@rules/storage/rulesStorage.js";
import type {ApiResponse} from "@api/response/apiResponse.js";
import {ApiResponseStatus} from "@api/response/apiResponseStatus.js";
import {deleteManyRulesApiSchema} from "@rules/api/schemas/deleteManyRulesApiSchema.js";
import {deleteManyRulesApiSchema} from "@rules/api/deleteMany/deleteManyRulesApiSchema.js";

class DeleteManyRulesApiEndpoint
implements ApiEndpoint<typeof deleteManyRulesApiSchema>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { array, object, string } from "zod";
import {ipApiSchema} from "@rules/api/schemas/record/ip/ipApiSchema.js";
import {ruleIpSchema} from "@rules/rule/ip/ruleIpSchema.js";

const deleteManyRulesApiSchema = array(
object({
clientId: string().optional(),
userIp: ipApiSchema.optional(),
userIp: ruleIpSchema.optional(),
userId: string().optional(),
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {ApiEndpoint} from "@api/apiEndpoint.js";
import type {RulesStorage} from "@rules/storage/rulesStorage.js";
import type {ApiResponse} from "@api/response/apiResponse.js";
import {ApiResponseStatus} from "@api/response/apiResponseStatus.js";
import {insertManyRulesApiSchema} from "@rules/api/schemas/insertManyRulesApiSchema.js";
import {insertManyRulesApiSchema} from "@rules/api/insertMany/insertManyRulesApiSchema.js";

class InsertManyRulesApiEndpoint
implements ApiEndpoint<typeof insertManyRulesApiSchema>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { array, boolean, object, string } from "zod";
import {ipApiSchema} from "@rules/api/schemas/record/ip/ipApiSchema.js";
import {configApiSchema} from "@rules/api/schemas/record/configApiSchema.js";
import {ruleIpSchema} from "@rules/rule/ip/ruleIpSchema.js";
import {ruleConfigSchema} from "@rules/rule/config/ruleConfigSchema.js";

const insertManyRulesApiSchema = array(
object({
isUserBlocked: boolean(),
clientId: string().optional(),
description: string().optional(),
userIp: ipApiSchema.optional(),
userIp: ruleIpSchema.optional(),
userId: string().optional(),
config: configApiSchema.optional(),
config: ruleConfigSchema.optional(),
}),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import type {ApiRoute} from "@api/route/apiRoute.js";
import {ruleApiPaths} from "@rules/api/ruleApiPaths.js";
import type {RulesStorage} from "@rules/storage/rulesStorage.js";
import type {ApiRoutesProvider} from "@api/route/apiRoutesProvider.js";
import {DeleteManyRulesApiEndpoint} from "@rules/api/endpoints/deleteManyRulesApiEndpoint.js";
import {InsertManyRulesApiEndpoint} from "@rules/api/endpoints/insertManyRulesApiEndpoint.js";
import {DeleteManyRulesApiEndpoint} from "@rules/api/deleteMany/deleteManyRulesApiEndpoint.js";
import {InsertManyRulesApiEndpoint} from "@rules/api/insertMany/insertManyRulesApiEndpoint.js";

class RuleApiRoutesProvider implements ApiRoutesProvider {
public getRoutes(rulesStorage: RulesStorage): ApiRoute[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type {RulesStorage} from "@rules/storage/rulesStorage.js";
import type {Rule} from "@rules/rule/rule.js";
import type {SearchRuleFilters} from "@rules/storage/filters/search/searchRuleFilters.js";
import type {SearchRuleFilterSettings} from "@rules/storage/filters/search/searchRuleFilterSettings.js";
import {IpVersion} from "@rules/rule/ip/ipVersion.js";
import {RuleIpVersion} from "@rules/rule/ip/ruleIpVersion.js";
import type {RuleMongooseRecord} from "@rules/mongoose/ruleMongooseRecord.js";
import type {RuleRecord} from "@rules/storage/ruleRecord.js";

Expand Down Expand Up @@ -172,22 +172,22 @@ class RulesMongooseStorage implements RulesStorage {
protected getFilterByUserIpAddress(userIpAddress: IPAddress): object {
const isIpV4 = userIpAddress instanceof Address4;

const userIpVersion = isIpV4 ? IpVersion.v4 : IpVersion.v6;
const userIpVersion = isIpV4 ? RuleIpVersion.v4 : RuleIpVersion.v6;

const userIpAsNumeric = isIpV4
? userIpAddress.bigInt()
: userIpAddress.bigInt().toString();

const userIpKey =
userIpVersion === IpVersion.v4
userIpVersion === RuleIpVersion.v4
? "userIp.v4.asNumeric"
: "userIp.v6.asNumericString";
const rangeMinKey =
userIpVersion === IpVersion.v4
userIpVersion === RuleIpVersion.v4
? "userIp.v4.mask.rangeMinAsNumeric"
: "userIp.v6.mask.rangeMinAsNumericString";
const rangeMaxKey =
userIpVersion === IpVersion.v4
userIpVersion === RuleIpVersion.v4
? "userIp.v4.mask.rangeMaxAsNumeric"
: "userIp.v6.mask.rangeMaxAsNumericString";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { Schema } from "mongoose";
import type {Config} from "@rules/rule/config.js";
import {imageCaptchaConfigMongooseSchema} from "@rules/mongoose/schemas/config/imageCaptchaConfigMongooseSchema.js";
import type {RuleConfig} from "@rules/rule/config/ruleConfig.js";

const configMongooseSchema = new Schema<Config>(
const configMongooseSchema = new Schema<RuleConfig>(
{
imageCaptcha: {
type: imageCaptchaConfigMongooseSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { Schema } from "mongoose";
import type {Ip} from "@rules/rule/ip/ip.js";
import type {RuleIp} from "@rules/rule/ip/ruleIp.js";
import {ipV6MongooseSchema} from "@rules/mongoose/schemas/ip/v6/ipV6MongooseSchema.js";
import {ipV4MongooseSchema} from "@rules/mongoose/schemas/ip/v4/ipV4MongooseSchema.js";

const ipMongooseSchema = new Schema<Ip>(
const ipMongooseSchema = new Schema<RuleIp>(
{
v4: {
type: ipV4MongooseSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { Schema } from "mongoose";
import type {IpV4Mask} from "@rules/rule/ip/v4/ipV4Mask.js";
import type {RuleIpV4Mask} from "@rules/rule/ip/v4/mask/ruleIpV4Mask.js";

const ipV4MaskMongooseSchema = new Schema<IpV4Mask>(
const ipV4MaskMongooseSchema = new Schema<RuleIpV4Mask>(
{
// Type choice note: Int32 can't store 10 digits of the numeric presentation of ipV4,
// so we use BigInt, which is supported by Mongoose and turned into Mongo's Long (Int64)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { Schema } from "mongoose";
import type {IpV4} from "@rules/rule/ip/v4/ipV4.js";
import type {RuleIpV4} from "@rules/rule/ip/v4/ruleIpV4.js";
import {ipV4MaskMongooseSchema} from "@rules/mongoose/schemas/ip/v4/ipV4MaskMongooseSchema.js";

const ipV4MongooseSchema = new Schema<IpV4>(
const ipV4MongooseSchema = new Schema<RuleIpV4>(
{
// Type choice note: Int32 can't store 10 digits of the numeric presentation of ipV4,
// so we use BigInt, which is supported by Mongoose and turned into Mongo's Long (Int64)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { Schema } from "mongoose";
import {IPV6_NUMERIC_MAX_LENGTH} from "@rules/rule/ip/v6/ipV6NumericMaxLength.js";
import type {IpV6Mask} from "@rules/rule/ip/v6/ipV6Mask.js";
import {RULE_IPV6_NUMERIC_MAX_LENGTH} from "@rules/rule/ip/v6/ruleIpV6NumericMaxLength.js";
import type {RuleIpV6Mask} from "@rules/rule/ip/v6/mask/ruleIpV6Mask.js";

const ipV6MaskMongooseSchema = new Schema<IpV6Mask>(
const ipV6MaskMongooseSchema = new Schema<RuleIpV6Mask>(
{
// 1. Type choice note:
/**
Expand All @@ -33,14 +33,14 @@ const ipV6MaskMongooseSchema = new Schema<IpV6Mask>(
required: true,
// we must have the exact same string length to guarantee the right comparison.
set: (value: string): string =>
value.padStart(IPV6_NUMERIC_MAX_LENGTH, "0"),
value.padStart(RULE_IPV6_NUMERIC_MAX_LENGTH, "0"),
},
rangeMaxAsNumericString: {
type: String,
required: true,
// we must have the exact same string length to guarantee the right comparison.
set: (value: string): string =>
value.padStart(IPV6_NUMERIC_MAX_LENGTH, "0"),
value.padStart(RULE_IPV6_NUMERIC_MAX_LENGTH, "0"),
},
asNumeric: { type: Number, required: true },
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { Schema } from "mongoose";
import {IPV6_NUMERIC_MAX_LENGTH} from "@rules/rule/ip/v6/ipV6NumericMaxLength.js";
import {RULE_IPV6_NUMERIC_MAX_LENGTH} from "@rules/rule/ip/v6/ruleIpV6NumericMaxLength.js";
import {ipV6MaskMongooseSchema} from "@rules/mongoose/schemas/ip/v6/ipV6MaskMongooseSchema.js";
import type {IpV6} from "@rules/rule/ip/v6/ipV6.js";
import type {RuleIpV6} from "@rules/rule/ip/v6/ruleIpV6.js";

const ipV6MongooseSchema = new Schema<IpV6>(
const ipV6MongooseSchema = new Schema<RuleIpV6>(
{
// 1. Type choice note:
/**
Expand All @@ -34,7 +34,7 @@ const ipV6MongooseSchema = new Schema<IpV6>(
required: true,
// we must have the exact same string length to guarantee the right comparison.
set: (value: string): string =>
value.padStart(IPV6_NUMERIC_MAX_LENGTH, "0"),
value.padStart(RULE_IPV6_NUMERIC_MAX_LENGTH, "0"),
},
asString: { type: String, required: true },
mask: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import type { z } from "zod";
import type {configApiSchema} from "@rules/api/schemas/record/configApiSchema.js";
import type {ruleConfigSchema} from "@rules/rule/config/ruleConfigSchema.js";

type Config = z.infer<typeof configApiSchema>;
type RuleConfig = z.infer<typeof ruleConfigSchema>;

export type { Config };
export type { RuleConfig };
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import { object } from "zod";
import {imageCaptchaConfigSchema} from "@imageCaptchaConfig/imageCaptchaConfigSchema.js";

const configApiSchema = object({
const ruleConfigSchema = object({
imageCaptcha: imageCaptchaConfigSchema.optional(),
});

export { configApiSchema };
export { ruleConfigSchema };
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import type { z } from "zod";
import type {ipApiSchema} from "@rules/api/schemas/record/ip/ipApiSchema.js";
import type {ruleIpSchema} from "@rules/rule/ip/ruleIpSchema.js";

type Ip = z.infer<typeof ipApiSchema>;
type RuleIp = z.infer<typeof ruleIpSchema>;

export type { Ip };
export type { RuleIp };
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { object } from "zod";
import {ipV4ApiSchema} from "@rules/api/schemas/record/ip/v4/ipV4ApiSchema.js";
import {ipV6ApiSchema} from "@rules/api/schemas/record/ip/v6/ipV6ApiSchema.js";
import {ruleIpV4Schema} from "@rules/rule/ip/v4/ruleIpV4Schema.js";
import {ruleIpV6Schema} from "@rules/rule/ip/v6/ruleIpV6Schema.js";

const ipApiSchema = object({
v4: ipV4ApiSchema.optional(),
v6: ipV6ApiSchema.optional(),
const ruleIpSchema = object({
v4: ruleIpV4Schema.optional(),
v6: ruleIpV6Schema.optional(),
});

export { ipApiSchema };
export { ruleIpSchema };
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
enum IpVersion {
enum RuleIpVersion {
v4 = "v4",
v6 = "v6",
}

export { IpVersion };
export { RuleIpVersion };
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import type { z } from "zod";
import type {ipV4MaskApiSchema} from "@rules/api/schemas/record/ip/v4/ipV4MaskApiSchema.js";
import type {ruleIpV4MaskSchema} from "@rules/rule/ip/v4/mask/ruleIpV4MaskSchema.js";

type IpV4Mask = z.infer<typeof ipV4MaskApiSchema>;
type RuleIpV4Mask = z.infer<typeof ruleIpV4MaskSchema>;

export type { IpV4Mask };
export type { RuleIpV4Mask };
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
// limitations under the License.
import { bigint, number, object } from "zod";

const ipV4MaskApiSchema = object({
const ruleIpV4MaskSchema = object({
rangeMinAsNumeric: bigint(),
rangeMaxAsNumeric: bigint(),
asNumeric: number(),
});

export { ipV4MaskApiSchema };
export { ruleIpV4MaskSchema };
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import type { z } from "zod";
import type {ipV6ApiSchema} from "@rules/api/schemas/record/ip/v6/ipV6ApiSchema.js";
import type {ruleIpV4Schema} from "@rules/rule/ip/v4/ruleIpV4Schema.js";

type IpV6 = z.infer<typeof ipV6ApiSchema>;
type RuleIpV4 = z.infer<typeof ruleIpV4Schema>;

export type { IpV6 };
export type { RuleIpV4 };
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { bigint, object, string } from "zod";
import {ipV4MaskApiSchema} from "@rules/api/schemas/record/ip/v4/ipV4MaskApiSchema.js";
import {ruleIpV4MaskSchema} from "@rules/rule/ip/v4/mask/ruleIpV4MaskSchema.js";

const ipV4ApiSchema = object({
const ruleIpV4Schema = object({
asNumeric: bigint(),
asString: string(),
mask: ipV4MaskApiSchema.optional(),
mask: ruleIpV4MaskSchema.optional(),
});

export { ipV4ApiSchema };
export { ruleIpV4Schema };
19 changes: 0 additions & 19 deletions packages/user-access-policy/src/rules/rule/ip/v6/ipV6Mask.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2021-2024 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import type { z } from "zod";
import type {ruleIpV6MaskSchema} from "@rules/rule/ip/v6/mask/ruleIpV6MaskSchema.js";

type RuleIpV6Mask = z.infer<typeof ruleIpV6MaskSchema>;

export type { RuleIpV6Mask };
Loading

0 comments on commit d5417db

Please sign in to comment.