Skip to content

Commit 4dfd5cc

Browse files
committed
fix(serve): remove checking user credential logistic and only generate token
- change "/auth" to "/auth/token" and rename "authIdentity" method to "getTokenInfo". - remove checking user credential logistic in "getTokenInfo" method. - update all related test cases
1 parent 7c3f9b4 commit 4dfd5cc

11 files changed

+37
-153
lines changed

packages/integration-testing/src/example1/example1-2.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ afterEach(async () => {
3131
await server.close();
3232
});
3333

34-
it('Example1-2: authenticate user identity by /auth API', async () => {
34+
it('Example1-2: authenticate user identity by /auth/token API', async () => {
3535
const builder = new VulcanBuilder(projectConfig);
3636
await builder.build();
3737
server = new VulcanServer(projectConfig);
3838
const httpServer = (await server.start())['http'];
3939

4040
const agent = supertest(httpServer);
4141
const result = await agent.get(
42-
'/auth?type=basic&username=user1&password=test1'
42+
'/auth/token?type=basic&username=user1&password=test1'
4343
);
4444
expect(result.body).toEqual({
4545
token: 'dXNlcjE6dGVzdDE=',

packages/serve/src/lib/auth/httpBasicAuthenticator.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,12 @@ export class BasicAuthenticator extends BaseAuthenticator<BasicOptions> {
8787
}
8888
}
8989

90-
public async authIdentity(ctx: KoaContext) {
90+
public async getTokenInfo(ctx: KoaContext) {
9191
const username = ctx.request.query['username'] as string;
9292
const password = ctx.request.query['password'] as string;
9393
if (!username || !password)
9494
throw new Error('please provide "username" and "password".');
9595

96-
if (
97-
!(username in this.usersCredentials) ||
98-
!(md5(password) === this.usersCredentials[username].md5Password)
99-
)
100-
throw new Error('authenticate user identity failed.');
101-
10296
const token = Buffer.from(`${username}:${password}`).toString('base64');
10397

10498
return {

packages/serve/src/lib/auth/passwordFileAuthenticator.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,12 @@ export class PasswordFileAuthenticator extends BaseAuthenticator<PasswordFileOpt
6666
}
6767
}
6868

69-
public async authIdentity(ctx: KoaContext) {
69+
public async getTokenInfo(ctx: KoaContext) {
7070
const username = ctx.request.query['username'] as string;
7171
const password = ctx.request.query['password'] as string;
7272
if (!username || !password)
7373
throw new Error('please provide "username" and "password".');
7474

75-
if (
76-
!(username in this.usersCredentials) ||
77-
!bcrypt.compareSync(
78-
password,
79-
this.usersCredentials[username].bcryptPassword
80-
)
81-
)
82-
throw new Error(`authenticate user identity failed.`);
83-
8475
const token = Buffer.from(`${username}:${password}`).toString('base64');
8576

8677
return {

packages/serve/src/lib/auth/simpleTokenAuthenticator.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,10 @@ export class SimpleTokenAuthenticator extends BaseAuthenticator<SimpleTokenOptio
4343
}
4444
}
4545

46-
public async authIdentity(ctx: KoaContext) {
46+
public async getTokenInfo(ctx: KoaContext) {
4747
const token = ctx.request.query['token'] as string;
4848
if (!token) throw new Error('please provide "token".');
4949

50-
if (!(token in this.usersCredentials))
51-
throw new Error(`authenticate user identity failed.`);
52-
5350
return {
5451
token: token,
5552
};

packages/serve/src/lib/middleware/auth/authCredentialMiddleware.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class AuthCredentialMiddleware extends BuiltInMiddleware<AuthOptions> {
5858
const options = this.getOptions() as AuthOptions;
5959

6060
// The /auth endpoint not need contains "Authorization" in header and auth credentials
61-
if (context.path === '/auth') return;
61+
if (context.path === '/auth/token') return;
6262

6363
// pass current context to auth token for users
6464
for (const name of Object.keys(this.authenticators)) {

packages/serve/src/lib/middleware/auth/authRouteMiddleware.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class AuthRouteMiddleware extends BuiltInMiddleware<AuthOptions> {
7474
}
7575

7676
private setAuthRoute() {
77-
this.router.get(`/auth`, async (context: KoaContext, next) => {
77+
this.router.get(`/auth/token`, async (context: KoaContext, next) => {
7878
await next();
7979
// not found type query string
8080
if (!('type' in context.request.query)) {
@@ -100,7 +100,7 @@ export class AuthRouteMiddleware extends BuiltInMiddleware<AuthOptions> {
100100
}
101101
// type does not support
102102
try {
103-
const result = await this.authenticators[type].authIdentity(context);
103+
const result = await this.authenticators[type].getTokenInfo(context);
104104
context.body = result;
105105
} catch (err) {
106106
context.status = 400;

packages/serve/src/models/extensions/authenticator.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export interface AuthResult {
3030
}
3131

3232
export interface IAuthenticator {
33-
/** authenticate identity for getting token or redirect url according to auth type */
34-
authIdentity(context: KoaContext): Promise<any>;
33+
/** get token related information */
34+
getTokenInfo(context: KoaContext): Promise<any>;
3535
/** auth credential (e.g: token) to get user info */
3636
authCredential(context: KoaContext): Promise<AuthResult>;
3737
}
@@ -41,7 +41,7 @@ export abstract class BaseAuthenticator<AuthTypeOption>
4141
extends ExtensionBase
4242
implements IAuthenticator
4343
{
44-
public abstract authIdentity(
44+
public abstract getTokenInfo(
4545
context: KoaContext
4646
): Promise<Record<string, any>>;
4747
public abstract authCredential(context: KoaContext): Promise<AuthResult>;

packages/serve/test/auth/basicAuthenticator.spec.ts

+5-53
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ const authCredential = async (
2121
return await authenticator.authCredential(ctx);
2222
};
2323

24-
const authIdentity = async (
24+
const getTokenInfo = async (
2525
ctx: KoaContext,
2626
options: any
2727
): Promise<Record<string, any>> => {
2828
const authenticator = new BasicAuthenticator({ options }, '');
2929
await authenticator.activate();
30-
return await authenticator.authIdentity(ctx);
30+
return await authenticator.getTokenInfo(ctx);
3131
};
3232

3333
describe('Test http basic authenticator', () => {
@@ -310,35 +310,13 @@ describe('Test http basic authenticator', () => {
310310
};
311311
const expected = Buffer.from('user1:test1').toString('base64');
312312
// Act
313-
const result = await authIdentity(ctx, {
313+
const result = await getTokenInfo(ctx, {
314314
basic: { 'users-list': userLists },
315315
});
316316
// Assert
317317
expect(result['token']).toEqual(expected);
318318
});
319319

320-
it('Should auth identity failed when request not matched in empty "users-list" options', async () => {
321-
// Arrange
322-
const expected = new Error('authenticate user identity failed.');
323-
const ctx = {
324-
...sinon.stubInterface<KoaContext>(),
325-
request: {
326-
...sinon.stubInterface<Request>(),
327-
query: {
328-
...sinon.stubInterface<ParsedUrlQuery>(),
329-
username: 'user1',
330-
password: 'test1',
331-
},
332-
},
333-
};
334-
// Act
335-
const action = authIdentity(ctx, {
336-
basic: { 'users-list': [] },
337-
});
338-
// Assert
339-
expect(action).rejects.toThrow(expected);
340-
});
341-
342320
it('Should auth identity successfully when request match in "htpasswd-file" path of options', async () => {
343321
// Arrange
344322
const expected = Buffer.from('user3:test3').toString('base64');
@@ -357,7 +335,7 @@ describe('Test http basic authenticator', () => {
357335
};
358336

359337
// Act
360-
const result = await authIdentity(ctx, {
338+
const result = await getTokenInfo(ctx, {
361339
basic: {
362340
'htpasswd-file': {
363341
path: path.resolve(__dirname, './test-files/basic.htpasswd'),
@@ -369,32 +347,6 @@ describe('Test http basic authenticator', () => {
369347
expect(result['token']).toEqual(expected);
370348
});
371349

372-
it('Should auth identity failed when request not match in "htpasswd-file" path of options', async () => {
373-
// Arrange
374-
const expected = new Error('authenticate user identity failed.');
375-
const ctx = {
376-
...sinon.stubInterface<KoaContext>(),
377-
request: {
378-
...sinon.stubInterface<Request>(),
379-
query: {
380-
...sinon.stubInterface<ParsedUrlQuery>(),
381-
username: 'user1',
382-
password: 'test1',
383-
},
384-
},
385-
};
386-
// Act
387-
const action = authIdentity(ctx, {
388-
basic: {
389-
'htpasswd-file': {
390-
path: path.resolve(__dirname, './test-files/basic.htpasswd'),
391-
},
392-
},
393-
});
394-
// Assert
395-
expect(action).rejects.toThrow(expected);
396-
});
397-
398350
it.each([
399351
['username', { 'users-list': userLists }],
400352
['password', { 'users-list': userLists }],
@@ -430,7 +382,7 @@ describe('Test http basic authenticator', () => {
430382
},
431383
};
432384
// Act
433-
const action = authIdentity(ctx, {
385+
const action = getTokenInfo(ctx, {
434386
basic: options,
435387
});
436388
// Assert

packages/serve/test/auth/passwordFileAuthenticator.spec.ts

+4-30
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ const authCredential = async (
1616
return await authenticator.authCredential(ctx);
1717
};
1818

19-
const authIdentity = async (
19+
const getTokenInfo = async (
2020
ctx: KoaContext,
2121
options: any
2222
): Promise<Record<string, any>> => {
2323
const authenticator = new PasswordFileAuthenticator({ options }, '');
2424
await authenticator.activate();
25-
return await authenticator.authIdentity(ctx);
25+
return await authenticator.getTokenInfo(ctx);
2626
};
2727

2828
describe('Test password-file authenticator', () => {
@@ -242,7 +242,7 @@ describe('Test password-file authenticator', () => {
242242
};
243243

244244
// Act
245-
const result = await authIdentity(ctx, {
245+
const result = await getTokenInfo(ctx, {
246246
'password-file': {
247247
path: path.resolve(__dirname, './test-files/password-file'),
248248
},
@@ -252,32 +252,6 @@ describe('Test password-file authenticator', () => {
252252
expect(result['token']).toEqual(expected);
253253
});
254254

255-
it('Should auth identity failed when request not match in "password-file" path of options', async () => {
256-
// Arrange
257-
const expected = new Error('authenticate user identity failed.');
258-
const ctx = {
259-
...sinon.stubInterface<KoaContext>(),
260-
request: {
261-
...sinon.stubInterface<Request>(),
262-
query: {
263-
...sinon.stubInterface<ParsedUrlQuery>(),
264-
username: 'user1',
265-
password: 'test1',
266-
},
267-
},
268-
};
269-
270-
// Act
271-
const action = authIdentity(ctx, {
272-
'password-file': {
273-
path: path.resolve(__dirname, './test-files/password-file'),
274-
},
275-
});
276-
277-
// Assert
278-
expect(action).rejects.toThrow(expected);
279-
});
280-
281255
it.each([['username'], ['password']])(
282256
'Should auth identity failed when miss some of request fields',
283257
async (field: string) => {
@@ -294,7 +268,7 @@ describe('Test password-file authenticator', () => {
294268
},
295269
};
296270
// Act
297-
const action = authIdentity(ctx, {
271+
const action = getTokenInfo(ctx, {
298272
'password-file': {
299273
path: path.resolve(__dirname, './test-files/password-file'),
300274
},

packages/serve/test/auth/simpleTokenAuthenticator.spec.ts

+4-28
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ const authCredential = async (
1818
return await authenticator.authCredential(ctx);
1919
};
2020

21-
const authIdentity = async (
21+
const getTokenInfo = async (
2222
ctx: KoaContext,
2323
options: any
2424
): Promise<Record<string, any>> => {
2525
const authenticator = new SimpleTokenAuthenticator({ options }, '');
2626
await authenticator.activate();
27-
return await authenticator.authIdentity(ctx);
27+
return await authenticator.getTokenInfo(ctx);
2828
};
2929

3030
describe('Test simple-token authenticator', () => {
@@ -186,38 +186,14 @@ describe('Test simple-token authenticator', () => {
186186
} as KoaContext;
187187

188188
// Act
189-
const result = await authIdentity(ctx, {
189+
const result = await getTokenInfo(ctx, {
190190
'simple-token': userLists,
191191
});
192192

193193
// Assert
194194
expect(result['token']).toEqual(token);
195195
});
196196

197-
it('Should auth identity failed when request not matched in "simple-token" options', async () => {
198-
// Arrange
199-
const expected = new Error('authenticate user identity failed.');
200-
const token = Buffer.from('user3:test3').toString('base64');
201-
const ctx = {
202-
...sinon.stubInterface<KoaContext>(),
203-
request: {
204-
...sinon.stubInterface<Request>(),
205-
query: {
206-
...sinon.stubInterface<ParsedUrlQuery>(),
207-
token: token,
208-
},
209-
},
210-
} as KoaContext;
211-
212-
// Act
213-
const action = authIdentity(ctx, {
214-
'simple-token': userLists,
215-
});
216-
217-
// Assert
218-
expect(action).rejects.toThrow(expected);
219-
});
220-
221197
it('Should auth identity failed when miss request field', async () => {
222198
// Arrange
223199
const expected = new Error('please provide "token".');
@@ -231,7 +207,7 @@ describe('Test simple-token authenticator', () => {
231207
},
232208
};
233209
// Act
234-
const action = authIdentity(ctx, {
210+
const action = getTokenInfo(ctx, {
235211
'simple-token': userLists,
236212
});
237213
// Assert

0 commit comments

Comments
 (0)