Skip to content

Commit 7c3f9b4

Browse files
committedSep 21, 2022
fix(serve): the /auth route still need to contain "Authorization" header issue
- add the /auth route checking in auth credential middleware. - refactor integration-testing example structure and add authenticate example
1 parent 044b649 commit 7c3f9b4

File tree

4 files changed

+89
-40
lines changed

4 files changed

+89
-40
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { VulcanBuilder } from '@vulcan-sql/build';
2+
import { VulcanServer } from '@vulcan-sql/serve';
3+
import * as supertest from 'supertest';
4+
import projectConfig from './projectConfig';
5+
6+
let server: VulcanServer;
7+
8+
afterEach(async () => {
9+
await server?.close();
10+
});
11+
12+
it.each([
13+
[
14+
'436193eb-f686-4105-ad7b-b5945276c14a',
15+
[
16+
{
17+
id: '436193eb-f686-4105-ad7b-b5945276c14a',
18+
name: 'ivan',
19+
},
20+
],
21+
],
22+
['2dc839e0-0f65-4dba-ac38-4eaf023d0008', []],
23+
])(
24+
'Example1: Build and serve should work',
25+
async (userId, expected) => {
26+
const builder = new VulcanBuilder(projectConfig);
27+
await builder.build();
28+
server = new VulcanServer(projectConfig);
29+
const httpServer = (await server.start())['http'];
30+
31+
const agent = supertest(httpServer);
32+
const result = await agent.get(`/api/user/${userId}`);
33+
expect(JSON.stringify(result.body)).toEqual(JSON.stringify(expected));
34+
},
35+
10000
36+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { IBuildOptions, VulcanBuilder } from '@vulcan-sql/build';
2+
import { ServeConfig, VulcanServer } from '@vulcan-sql/serve';
3+
4+
import * as supertest from 'supertest';
5+
import defaultConfig from './projectConfig';
6+
7+
let server: VulcanServer;
8+
9+
const projectConfig: ServeConfig & IBuildOptions = {
10+
...defaultConfig,
11+
auth: {
12+
enabled: true,
13+
options: {
14+
basic: {
15+
'users-list': [
16+
{
17+
name: 'user1',
18+
// md5('test1')
19+
md5Password: '5a105e8b9d40e1329780d62ea2265d8a',
20+
attr: {
21+
role: 'admin',
22+
},
23+
},
24+
],
25+
},
26+
},
27+
},
28+
};
29+
30+
afterEach(async () => {
31+
await server.close();
32+
});
33+
34+
it('Example1-2: authenticate user identity by /auth API', async () => {
35+
const builder = new VulcanBuilder(projectConfig);
36+
await builder.build();
37+
server = new VulcanServer(projectConfig);
38+
const httpServer = (await server.start())['http'];
39+
40+
const agent = supertest(httpServer);
41+
const result = await agent.get(
42+
'/auth?type=basic&username=user1&password=test1'
43+
);
44+
expect(result.body).toEqual({
45+
token: 'dXNlcjE6dGVzdDE=',
46+
});
47+
}, 10000);
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
import {
2-
VulcanBuilder,
3-
IBuildOptions,
4-
SchemaReaderType,
5-
} from '@vulcan-sql/build';
1+
import { SchemaReaderType } from '@vulcan-sql/build';
62
import {
73
ArtifactBuilderProviderType,
84
ArtifactBuilderSerializerType,
95
TemplateProviderType,
106
DocumentSpec,
117
} from '@vulcan-sql/core';
12-
import { VulcanServer, ServeConfig, APIProviderType } from '@vulcan-sql/serve';
8+
import { APIProviderType } from '@vulcan-sql/serve';
139
import * as path from 'path';
14-
import * as supertest from 'supertest';
1510
import faker from '@faker-js/faker';
1611

17-
const projectConfig: ServeConfig & IBuildOptions = {
12+
export default {
1813
name: 'example project 1',
1914
description: 'Vulcan project for integration testing',
2015
version: '0.0.1',
@@ -53,35 +48,3 @@ const projectConfig: ServeConfig & IBuildOptions = {
5348
},
5449
port: faker.datatype.number({ min: 20000, max: 30000 }),
5550
};
56-
57-
let server: VulcanServer;
58-
59-
afterEach(async () => {
60-
await server?.close();
61-
});
62-
63-
it.each([
64-
[
65-
'436193eb-f686-4105-ad7b-b5945276c14a',
66-
[
67-
{
68-
id: '436193eb-f686-4105-ad7b-b5945276c14a',
69-
name: 'ivan',
70-
},
71-
],
72-
],
73-
['2dc839e0-0f65-4dba-ac38-4eaf023d0008', []],
74-
])(
75-
'Example1: Build and serve should work',
76-
async (userId, expected) => {
77-
const builder = new VulcanBuilder(projectConfig);
78-
await builder.build();
79-
server = new VulcanServer(projectConfig);
80-
const httpServer = (await server.start())['http'];
81-
82-
const agent = supertest(httpServer);
83-
const result = await agent.get(`/api/user/${userId}`);
84-
expect(JSON.stringify(result.body)).toEqual(JSON.stringify(expected));
85-
},
86-
10000
87-
);

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

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ export class AuthCredentialMiddleware extends BuiltInMiddleware<AuthOptions> {
5757

5858
const options = this.getOptions() as AuthOptions;
5959

60+
// The /auth endpoint not need contains "Authorization" in header and auth credentials
61+
if (context.path === '/auth') return;
62+
6063
// pass current context to auth token for users
6164
for (const name of Object.keys(this.authenticators)) {
6265
// skip the disappeared auth type name in options

0 commit comments

Comments
 (0)
Please sign in to comment.