Skip to content

Commit 381298d

Browse files
Su-YongJellyBrick
andauthored
Feature/scope validator (#2)
* init scope validator * 코드 스타일 수정 / Create scope-validator-factory.ts * clean style * setup pattern * add pattern * add more test * fix request * reinforce pattern matching add test * fix regular expression * fix some style and make ScopeValidatorContext template * add return type * rename package scope-validator-core to scope-validator Co-authored-by: jellybrick <[email protected]>
1 parent 9df640d commit 381298d

21 files changed

+579
-9
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ script
22
node_modules
33
dist
44
gulpfile.js
5+
jest.config.js

.eslintrc.json

-8
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@
1111
"plugin:prettier/recommended",
1212
"prettier/@typescript-eslint"
1313
],
14-
"parser": "@typescript-eslint/parser",
15-
"parserOptions": {
16-
"ecmaVersion": 2020,
17-
"sourceType": "module",
18-
"project": [
19-
"tsconfig.json"
20-
]
21-
},
2214
"plugins": [
2315
"@typescript-eslint"
2416
],
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
script
2+
node_modules
3+
dist
4+
gulpfile.js
5+
jest.config.js
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": [
3+
"../../.eslintrc.json"
4+
],
5+
"parser": "@typescript-eslint/parser",
6+
"parserOptions": {
7+
"ecmaVersion": 2020,
8+
"sourceType": "module",
9+
"project": [
10+
"tsconfig.json"
11+
]
12+
}
13+
}

packages/scope-validator/.gitignore

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
9+
# Diagnostic reports (https://nodejs.org/api/report.html)
10+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11+
12+
# Runtime data
13+
pids
14+
*.pid
15+
*.seed
16+
*.pid.lock
17+
18+
# Directory for instrumented libs generated by jscoverage/JSCover
19+
lib-cov
20+
21+
# Coverage directory used by tools like istanbul
22+
coverage
23+
*.lcov
24+
25+
# nyc test coverage
26+
.nyc_output
27+
28+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29+
.grunt
30+
31+
# Bower dependency directory (https://bower.io/)
32+
bower_components
33+
34+
# node-waf configuration
35+
.lock-wscript
36+
37+
# Compiled binary addons (https://nodejs.org/api/addons.html)
38+
build/Release
39+
40+
# Dependency directories
41+
node_modules/
42+
jspm_packages/
43+
44+
# Snowpack dependency directory (https://snowpack.dev/)
45+
web_modules/
46+
47+
# TypeScript cache
48+
*.tsbuildinfo
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Microbundle cache
57+
.rpt2_cache/
58+
.rts2_cache_cjs/
59+
.rts2_cache_es/
60+
.rts2_cache_umd/
61+
62+
# Optional REPL history
63+
.node_repl_history
64+
65+
# Output of 'npm pack'
66+
*.tgz
67+
68+
# Yarn Integrity file
69+
.yarn-integrity
70+
71+
# dotenv environment variables file
72+
.env
73+
.env.test
74+
75+
# parcel-bundler cache (https://parceljs.org/)
76+
.cache
77+
.parcel-cache
78+
79+
# Next.js build output
80+
.next
81+
out
82+
83+
# Nuxt.js build / generate output
84+
.nuxt
85+
dist
86+
87+
# Gatsby files
88+
.cache/
89+
# Comment in the public line in if your project uses Gatsby and not Next.js
90+
# https://nextjs.org/blog/next-9-1#public-directory-support
91+
# public
92+
93+
# vuepress build output
94+
.vuepress/dist
95+
96+
# Serverless directories
97+
.serverless/
98+
99+
# FuseBox cache
100+
.fusebox/
101+
102+
# DynamoDB Local files
103+
.dynamodb/
104+
105+
# TernJS port file
106+
.tern-port
107+
108+
# Stores VSCode versions used for testing VSCode extensions
109+
.vscode-test
110+
111+
# yarn v2
112+
.yarn/cache
113+
.yarn/unplugged
114+
.yarn/build-state.yml
115+
.yarn/install-state.gz
116+
.pnp.*
117+
118+
# intelliJ
119+
.idea/
120+
121+
package-lock.json

packages/scope-validator/.npmignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*
2+
!dist/*
3+
!dist/**/*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"singleQuote": true
3+
}

packages/scope-validator/gulpfile.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const gulp = require('gulp');
2+
const ts = require('gulp-typescript');
3+
4+
const tsProject = ts.createProject(
5+
process.env.NODE_ENV ? `tsconfig.${process.env.NODE_ENV.toLowerCase()}.json` : 'tsconfig.json'
6+
);
7+
8+
gulp.task('default', () => {
9+
return tsProject.src()
10+
.pipe(tsProject())
11+
.pipe(gulp.dest('dist')
12+
);
13+
});
14+
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = {
2+
verbose: true,
3+
transform: {
4+
'^.+\\.ts$': 'ts-jest'
5+
},
6+
testRegex: '\\.spec\\.ts$',
7+
moduleFileExtensions: [
8+
'ts',
9+
'js'
10+
],
11+
globals: {
12+
'ts-jest': {
13+
'diagnostics': true
14+
}
15+
}
16+
};

packages/scope-validator/lib/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export { default as Pattern } from './pattern';
2+
export { default as ScopeValidator } from './scope-validator';
3+
export { default as ScopeValidatorContext } from './scope-validator-context';
4+
export { default as ScopeValidatorFactory } from './scope-validator-factory';
5+
export { default as ScopeValidatorManager } from './scope-validator-manager';
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
export default class Pattern {
2+
str: string;
3+
4+
get regex(): RegExp {
5+
const regex = this.str
6+
.replace(/(\${\w+})/g, '*')
7+
.replace(/[$^.+?]/g, '\\$&')
8+
.replace(/\*/g, '(\\w+)');
9+
10+
return new RegExp(`^${regex}$`);
11+
}
12+
13+
constructor(str: string) {
14+
this.str = str;
15+
}
16+
17+
test(compare: string): boolean {
18+
return this.regex.test(compare);
19+
}
20+
21+
getParameters(compare: string): Record<string, unknown> {
22+
const result: Record<string, unknown> = {};
23+
24+
let parameterNames = this.str.split(/(?:\${|})/g);
25+
parameterNames = parameterNames.filter((value, index) => index % 2);
26+
27+
const regexStr = this.str
28+
.replace(/(\${\w+})/g, '*')
29+
.replace(/[$^.+?]/g, '\\$&')
30+
.replace(/\*/g, '(\\w+)')
31+
.replace(/(\*)/g, ')(.+)(?:')
32+
.replace(/^(.*)/, '(?:$&')
33+
.replace(/(.*)$/, '$&)');
34+
35+
const regex = new RegExp(`^${regexStr}$`);
36+
37+
const parameterValues = compare.match(regex);
38+
parameterValues?.shift();
39+
40+
parameterNames.forEach((name, index) => {
41+
result[name] = parameterValues?.[index];
42+
});
43+
44+
return result;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default interface ScopeValidatorContext<T> {
2+
parameters?: Record<string, unknown>;
3+
4+
received?: T;
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// eslint-disable-next-line max-classes-per-file
2+
import ScopeValidator from './scope-validator';
3+
import ScopeValidatorContext from './scope-validator-context';
4+
5+
type ScopeValidatorFunction<T> = (
6+
name: string,
7+
context: ScopeValidatorContext<T>
8+
) => boolean;
9+
10+
export default class ScopeValidatorFactory<T> {
11+
static create<T = Record<string, unknown>>(
12+
pattern: string,
13+
func: ScopeValidatorFunction<T>
14+
): ScopeValidator<T> {
15+
const CustomScopeValidator = class extends ScopeValidator<T> {
16+
constructor() {
17+
super(pattern);
18+
}
19+
20+
// eslint-disable-next-line class-methods-use-this
21+
validate(name: string, context: ScopeValidatorContext<T>): boolean {
22+
return func(name, context);
23+
}
24+
};
25+
26+
return new CustomScopeValidator();
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import ScopeValidator from './scope-validator';
2+
3+
export default class ScopeValidatorManager<T> {
4+
private readonly scopeValidators: ScopeValidator<T>[] = [];
5+
6+
private context?: T;
7+
8+
constructor(...scopeValidators: ScopeValidator<T>[]) {
9+
this.use(...scopeValidators);
10+
}
11+
12+
use(...scopeValidators: ScopeValidator<T>[]): void {
13+
this.scopeValidators.push(...scopeValidators);
14+
}
15+
16+
validateOne(scope: string): boolean {
17+
const matchValidators = this.scopeValidators.filter((validator) =>
18+
validator.test(scope)
19+
);
20+
21+
return (
22+
matchValidators.length >= 1 &&
23+
matchValidators.every((validator) =>
24+
validator.validate(scope, {
25+
received: this.context,
26+
parameters: validator.pattern.getParameters(scope),
27+
})
28+
)
29+
);
30+
}
31+
32+
validateMany(...scopes: string[]): boolean[] {
33+
return scopes.map((scope) => this.validateOne(scope));
34+
}
35+
36+
validate(scope: string[], context?: T): boolean[] {
37+
this.setContext(context);
38+
39+
return this.validateMany(...scope);
40+
}
41+
42+
setContext(context?: T): void {
43+
this.context = context;
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import ScopeValidatorContext from './scope-validator-context';
2+
import Pattern from './pattern';
3+
4+
export default abstract class ScopeValidator<T> {
5+
pattern: Pattern;
6+
7+
protected constructor(pattern: string | Pattern) {
8+
if (pattern instanceof Pattern) this.pattern = pattern;
9+
else this.pattern = new Pattern(pattern);
10+
}
11+
12+
test(str: string): boolean {
13+
return this.pattern.test(str);
14+
}
15+
16+
abstract validate(name: string, context: ScopeValidatorContext<T>): boolean;
17+
}

packages/scope-validator/package.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "scope-validator",
3+
"version": "1.0.0",
4+
"description": "Pattern matching based Oauth2.0 scope validation library",
5+
"main": "dist/index.js",
6+
"scripts": {
7+
"test": "jest",
8+
"build": "npm run clean && npm run compile",
9+
"clean": "rm -rf ./dist",
10+
"compile": "tsc",
11+
"lint": "eslint .",
12+
"lint:fix": "eslint --fix .",
13+
"prepublishOnly": "cross-env NODE_ENV=production npm run build"
14+
},
15+
"author": "Su-Yong",
16+
"license": "ISC",
17+
"devDependencies": {
18+
"@types/node": "^14.10.1",
19+
"cross-env": "^7.0.2",
20+
"eslint": "^7.9.0",
21+
"gulp": "^4.0.2",
22+
"gulp-typescript": "^6.0.0-alpha.1",
23+
"jest": "^26.4.2",
24+
"ts-jest": "^26.3.0",
25+
"typescript": "^4.0.2"
26+
},
27+
"types": "dist/index.d.ts"
28+
}

0 commit comments

Comments
 (0)