@lowdefy/api 4.0.0-alpha.10 → 4.0.0-alpha.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/context/createApiContext.js +8 -6
- package/dist/context/createAuthorize.js +6 -3
- package/dist/context/{readConfigFile.js → createReadConfigFile.js} +0 -0
- package/dist/context/errors.js +2 -14
- package/dist/index.js +3 -2
- package/dist/routes/auth/callbacks/createCallbackPlugins.js +21 -0
- package/dist/routes/auth/callbacks/createCallbacks.js +43 -0
- package/dist/routes/auth/callbacks/createJWTCallback.js +63 -0
- package/dist/routes/auth/callbacks/createRedirectCallback.js +36 -0
- package/dist/routes/auth/callbacks/createSessionCallback.js +62 -0
- package/dist/routes/auth/callbacks/createSignInCallback.js +40 -0
- package/dist/routes/auth/createProviders.js +25 -0
- package/dist/routes/auth/events/createCreateUserEvent.js +33 -0
- package/dist/routes/auth/events/createEventPlugins.js +21 -0
- package/dist/routes/auth/events/createEvents.js +55 -0
- package/dist/routes/auth/events/createLinkAccountEvent.js +34 -0
- package/dist/routes/auth/events/createSessionEvent.js +34 -0
- package/dist/routes/auth/events/createSignInEvent.js +36 -0
- package/dist/routes/auth/events/createSignOutEvent.js +34 -0
- package/dist/routes/auth/events/createUpdateUserEvent.js +33 -0
- package/dist/routes/auth/getNextAuthConfig.js +58 -0
- package/dist/routes/request/callRequest.js +2 -1
- package/dist/routes/rootConfig/getHomeAndMenus.js +1 -2
- package/dist/routes/rootConfig/getRootConfig.js +0 -1
- package/dist/routes/rootConfig/menus/filterMenuList.js +1 -2
- package/dist/test/testContext.js +3 -6
- package/package.json +14 -14
- package/dist/context/createContext.js +0 -46
|
@@ -12,22 +12,24 @@
|
|
|
12
12
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
13
|
See the License for the specific language governing permissions and
|
|
14
14
|
limitations under the License.
|
|
15
|
-
*/ import
|
|
16
|
-
|
|
15
|
+
*/ import createAuthorize from './createAuthorize.js';
|
|
16
|
+
import createReadConfigFile from './createReadConfigFile.js';
|
|
17
|
+
async function createApiContext({ buildDirectory , connections , logger , operators , secrets , session , }) {
|
|
17
18
|
const readConfigFile = createReadConfigFile({
|
|
18
19
|
buildDirectory
|
|
19
20
|
});
|
|
20
21
|
const config = await readConfigFile('config.json');
|
|
21
22
|
return {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
,
|
|
23
|
+
authorize: createAuthorize({
|
|
24
|
+
session
|
|
25
|
+
}),
|
|
25
26
|
config,
|
|
26
27
|
connections,
|
|
27
28
|
logger,
|
|
28
29
|
operators,
|
|
29
30
|
readConfigFile,
|
|
30
|
-
secrets
|
|
31
|
+
secrets,
|
|
32
|
+
user: session?.user
|
|
31
33
|
};
|
|
32
34
|
}
|
|
33
35
|
export default createApiContext;
|
|
@@ -13,13 +13,16 @@
|
|
|
13
13
|
See the License for the specific language governing permissions and
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/ import { ServerError } from '../context/errors.js';
|
|
16
|
-
function createAuthorize({
|
|
16
|
+
function createAuthorize({ session }) {
|
|
17
|
+
// Next-auth getSession provides a session object if the user is authenticated
|
|
18
|
+
// else session will be null
|
|
19
|
+
const authenticated = !!session;
|
|
20
|
+
const roles = session?.user?.roles ?? [];
|
|
17
21
|
function authorize({ auth }) {
|
|
18
22
|
if (auth.public === true) return true;
|
|
19
23
|
if (auth.public === false) {
|
|
20
24
|
if (auth.roles) {
|
|
21
|
-
return authenticated && auth.roles.some((role)=>roles.includes(role)
|
|
22
|
-
);
|
|
25
|
+
return authenticated && auth.roles.some((role)=>roles.includes(role));
|
|
23
26
|
}
|
|
24
27
|
return authenticated;
|
|
25
28
|
}
|
|
File without changes
|
package/dist/context/errors.js
CHANGED
|
@@ -12,13 +12,7 @@
|
|
|
12
12
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
13
|
See the License for the specific language governing permissions and
|
|
14
14
|
limitations under the License.
|
|
15
|
-
*/ /* eslint-disable max-classes-per-file */ let
|
|
16
|
-
constructor(message){
|
|
17
|
-
super(message);
|
|
18
|
-
this.name = 'AuthenticationError';
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
let ConfigurationError = class ConfigurationError extends Error {
|
|
15
|
+
*/ /* eslint-disable max-classes-per-file */ let ConfigurationError = class ConfigurationError extends Error {
|
|
22
16
|
constructor(message){
|
|
23
17
|
super(message);
|
|
24
18
|
this.name = 'ConfigurationError';
|
|
@@ -36,10 +30,4 @@ let ServerError = class ServerError extends Error {
|
|
|
36
30
|
this.name = 'ServerError';
|
|
37
31
|
}
|
|
38
32
|
};
|
|
39
|
-
|
|
40
|
-
constructor(message){
|
|
41
|
-
super(message);
|
|
42
|
-
this.name = 'TokenExpiredError';
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
export { AuthenticationError, ConfigurationError, RequestError, ServerError, TokenExpiredError };
|
|
33
|
+
export { ConfigurationError, RequestError, ServerError };
|
package/dist/index.js
CHANGED
|
@@ -15,7 +15,8 @@
|
|
|
15
15
|
*/ import callRequest from './routes/request/callRequest.js';
|
|
16
16
|
import createApiContext from './context/createApiContext.js';
|
|
17
17
|
import getHomeAndMenus from './routes/rootConfig/getHomeAndMenus.js';
|
|
18
|
+
import getNextAuthConfig from './routes/auth/getNextAuthConfig.js';
|
|
18
19
|
import getPageConfig from './routes/page/getPageConfig.js';
|
|
19
20
|
import getRootConfig from './routes/rootConfig/getRootConfig.js';
|
|
20
|
-
import {
|
|
21
|
-
export {
|
|
21
|
+
import { ConfigurationError, RequestError, ServerError } from './context/errors.js';
|
|
22
|
+
export { callRequest, ConfigurationError, createApiContext, getHomeAndMenus, getNextAuthConfig, getPageConfig, getRootConfig, RequestError, ServerError };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2022 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ function createCallbackPlugins({ authConfig , plugins , type }) {
|
|
16
|
+
return authConfig.callbacks.map((callbackConfig)=>({
|
|
17
|
+
fn: plugins.callbacks[callbackConfig.type],
|
|
18
|
+
properties: callbackConfig.properties
|
|
19
|
+
})).filter((callback)=>callback.fn.meta.type === type);
|
|
20
|
+
}
|
|
21
|
+
export default createCallbackPlugins;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2022 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import createJWTCallback from './createJWTCallback.js';
|
|
16
|
+
import createRedirectCallback from './createRedirectCallback.js';
|
|
17
|
+
import createSessionCallback from './createSessionCallback.js';
|
|
18
|
+
import createSignInCallback from './createSignInCallback.js';
|
|
19
|
+
function createCallbacks({ authConfig , plugins }) {
|
|
20
|
+
const callbacks = {
|
|
21
|
+
session: createSessionCallback({
|
|
22
|
+
authConfig,
|
|
23
|
+
plugins
|
|
24
|
+
})
|
|
25
|
+
};
|
|
26
|
+
const jwt = createJWTCallback({
|
|
27
|
+
authConfig,
|
|
28
|
+
plugins
|
|
29
|
+
});
|
|
30
|
+
if (jwt) callbacks.jwt = jwt;
|
|
31
|
+
const redirect = createRedirectCallback({
|
|
32
|
+
authConfig,
|
|
33
|
+
plugins
|
|
34
|
+
});
|
|
35
|
+
if (redirect) callbacks.redirect = redirect;
|
|
36
|
+
const signIn = createSignInCallback({
|
|
37
|
+
authConfig,
|
|
38
|
+
plugins
|
|
39
|
+
});
|
|
40
|
+
if (signIn) callbacks.signIn = signIn;
|
|
41
|
+
return callbacks;
|
|
42
|
+
}
|
|
43
|
+
export default createCallbacks;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2022 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import createCallbackPlugins from './createCallbackPlugins.js';
|
|
16
|
+
function createJWTCallback({ authConfig , plugins }) {
|
|
17
|
+
const jwtCallbackPlugins = createCallbackPlugins({
|
|
18
|
+
authConfig,
|
|
19
|
+
plugins,
|
|
20
|
+
type: 'jwt'
|
|
21
|
+
});
|
|
22
|
+
async function jwtCallback({ token , user , account , profile , isNewUser }) {
|
|
23
|
+
if (profile) {
|
|
24
|
+
const { sub , name , given_name , family_name , middle_name , nickname , preferred_username , profile: profile_claim , picture , website , email , email_verified , gender , birthdate , zoneinfo , locale , phone_number , phone_number_verified , address , updated_at , } = profile;
|
|
25
|
+
token = {
|
|
26
|
+
sub,
|
|
27
|
+
name,
|
|
28
|
+
given_name,
|
|
29
|
+
family_name,
|
|
30
|
+
middle_name,
|
|
31
|
+
nickname,
|
|
32
|
+
preferred_username,
|
|
33
|
+
profile: profile_claim,
|
|
34
|
+
picture,
|
|
35
|
+
website,
|
|
36
|
+
email,
|
|
37
|
+
email_verified,
|
|
38
|
+
gender,
|
|
39
|
+
birthdate,
|
|
40
|
+
zoneinfo,
|
|
41
|
+
locale,
|
|
42
|
+
phone_number,
|
|
43
|
+
phone_number_verified,
|
|
44
|
+
address,
|
|
45
|
+
updated_at,
|
|
46
|
+
...token
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
for (const plugin of jwtCallbackPlugins){
|
|
50
|
+
token = await plugin.fn({
|
|
51
|
+
properties: plugin.properties ?? {},
|
|
52
|
+
account,
|
|
53
|
+
isNewUser,
|
|
54
|
+
profile,
|
|
55
|
+
token,
|
|
56
|
+
user
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return token;
|
|
60
|
+
}
|
|
61
|
+
return jwtCallback;
|
|
62
|
+
}
|
|
63
|
+
export default createJWTCallback;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2022 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import createCallbackPlugins from './createCallbackPlugins.js';
|
|
16
|
+
function createRedirectCallback({ authConfig , plugins }) {
|
|
17
|
+
const redirectCallbackPlugins = createCallbackPlugins({
|
|
18
|
+
authConfig,
|
|
19
|
+
plugins,
|
|
20
|
+
type: 'redirect'
|
|
21
|
+
});
|
|
22
|
+
if (redirectCallbackPlugins.length === 0) return undefined;
|
|
23
|
+
if (redirectCallbackPlugins.length !== 1) {
|
|
24
|
+
throw new Error('More than one auth redirect callbacks are configured. Only one is allowed.');
|
|
25
|
+
}
|
|
26
|
+
const [plugin] = redirectCallbackPlugins;
|
|
27
|
+
async function redirectCallback({ url , baseUrl }) {
|
|
28
|
+
return plugin.fn({
|
|
29
|
+
properties: plugin.properties ?? {},
|
|
30
|
+
baseUrl,
|
|
31
|
+
url
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return redirectCallback;
|
|
35
|
+
}
|
|
36
|
+
export default createRedirectCallback;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2022 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import createCallbackPlugins from './createCallbackPlugins.js';
|
|
16
|
+
function createSessionCallback({ authConfig , plugins }) {
|
|
17
|
+
const sessionCallbackPlugins = createCallbackPlugins({
|
|
18
|
+
authConfig,
|
|
19
|
+
plugins,
|
|
20
|
+
type: 'session'
|
|
21
|
+
});
|
|
22
|
+
async function sessionCallback({ session , token , user }) {
|
|
23
|
+
// console.log({ session, token, user });
|
|
24
|
+
if (token) {
|
|
25
|
+
const { sub , name , given_name , family_name , middle_name , nickname , preferred_username , profile , picture , website , email , email_verified , gender , birthdate , zoneinfo , locale , phone_number , phone_number_verified , address , updated_at , } = token;
|
|
26
|
+
session.user = {
|
|
27
|
+
sub,
|
|
28
|
+
name,
|
|
29
|
+
given_name,
|
|
30
|
+
family_name,
|
|
31
|
+
middle_name,
|
|
32
|
+
nickname,
|
|
33
|
+
preferred_username,
|
|
34
|
+
profile,
|
|
35
|
+
picture,
|
|
36
|
+
website,
|
|
37
|
+
email,
|
|
38
|
+
email_verified,
|
|
39
|
+
gender,
|
|
40
|
+
birthdate,
|
|
41
|
+
zoneinfo,
|
|
42
|
+
locale,
|
|
43
|
+
phone_number,
|
|
44
|
+
phone_number_verified,
|
|
45
|
+
address,
|
|
46
|
+
updated_at,
|
|
47
|
+
...session.user
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
for (const plugin of sessionCallbackPlugins){
|
|
51
|
+
session = await plugin.fn({
|
|
52
|
+
properties: plugin.properties ?? {},
|
|
53
|
+
session,
|
|
54
|
+
token,
|
|
55
|
+
user
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return session;
|
|
59
|
+
}
|
|
60
|
+
return sessionCallback;
|
|
61
|
+
}
|
|
62
|
+
export default createSessionCallback;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2022 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import createCallbackPlugins from './createCallbackPlugins.js';
|
|
16
|
+
function createSignInCallback({ authConfig , plugins }) {
|
|
17
|
+
const signInCallbackPlugins = createCallbackPlugins({
|
|
18
|
+
authConfig,
|
|
19
|
+
plugins,
|
|
20
|
+
type: 'signIn'
|
|
21
|
+
});
|
|
22
|
+
if (signInCallbackPlugins.length === 0) return undefined;
|
|
23
|
+
async function signInCallback({ account , credentials , email , profile , user }) {
|
|
24
|
+
let allowSignIn = true;
|
|
25
|
+
for (const plugin of signInCallbackPlugins){
|
|
26
|
+
allowSignIn = await plugin.fn({
|
|
27
|
+
properties: plugin.properties ?? {},
|
|
28
|
+
account,
|
|
29
|
+
credentials,
|
|
30
|
+
email,
|
|
31
|
+
profile,
|
|
32
|
+
user
|
|
33
|
+
});
|
|
34
|
+
if (allowSignIn === false) break;
|
|
35
|
+
}
|
|
36
|
+
return allowSignIn;
|
|
37
|
+
}
|
|
38
|
+
return signInCallback;
|
|
39
|
+
}
|
|
40
|
+
export default createSignInCallback;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2022 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ // TODO: docs:
|
|
16
|
+
// Callback url to configure with provider will be: {{ protocol }}{{ host }}/api/auth/callback/{{ providerId }}
|
|
17
|
+
// This depends on providerId, which might cause some issues if users copy an example and change the id.
|
|
18
|
+
// We need to allow users to configure ids, since they might have more than one of the same type.
|
|
19
|
+
function createProviders({ authConfig , plugins }) {
|
|
20
|
+
return authConfig.providers.map((providerConfig)=>plugins.providers[providerConfig.type]({
|
|
21
|
+
...providerConfig.properties,
|
|
22
|
+
id: providerConfig.id
|
|
23
|
+
}));
|
|
24
|
+
}
|
|
25
|
+
export default createProviders;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2022 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import createEventPlugins from './createEventPlugins.js';
|
|
16
|
+
function createCreateUserEvent({ authConfig , plugins }) {
|
|
17
|
+
const createUserPlugins = createEventPlugins({
|
|
18
|
+
authConfig,
|
|
19
|
+
plugins,
|
|
20
|
+
type: 'createUser'
|
|
21
|
+
});
|
|
22
|
+
if (createUserPlugins.length === 0) return undefined;
|
|
23
|
+
async function createUserEvent({ user }) {
|
|
24
|
+
for (const plugin of createUserPlugins){
|
|
25
|
+
await plugin.fn({
|
|
26
|
+
properties: plugin.properties ?? {},
|
|
27
|
+
user
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return createUserEvent;
|
|
32
|
+
}
|
|
33
|
+
export default createCreateUserEvent;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2022 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ function createEventPlugins({ authConfig , plugins , type }) {
|
|
16
|
+
return authConfig.events.map((eventConfig)=>({
|
|
17
|
+
fn: plugins.events[eventConfig.type],
|
|
18
|
+
properties: eventConfig.properties
|
|
19
|
+
})).filter((event)=>event.fn.meta.type === type);
|
|
20
|
+
}
|
|
21
|
+
export default createEventPlugins;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2022 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import createCreateUserEvent from './createCreateUserEvent.js';
|
|
16
|
+
import createLinkAccountEvent from './createLinkAccountEvent.js';
|
|
17
|
+
import createSessionEvent from './createSessionEvent.js';
|
|
18
|
+
import createSignInEvent from './createSignInEvent.js';
|
|
19
|
+
import createSignOutEvent from './createSignOutEvent.js';
|
|
20
|
+
import createUpdateUserEvent from './createUpdateUserEvent.js';
|
|
21
|
+
function createEvents({ authConfig , plugins }) {
|
|
22
|
+
const events = {};
|
|
23
|
+
const createUser = createCreateUserEvent({
|
|
24
|
+
authConfig,
|
|
25
|
+
plugins
|
|
26
|
+
});
|
|
27
|
+
if (createUser) events.createUser = createUser;
|
|
28
|
+
const linkAccount = createLinkAccountEvent({
|
|
29
|
+
authConfig,
|
|
30
|
+
plugins
|
|
31
|
+
});
|
|
32
|
+
if (linkAccount) events.linkAccount = linkAccount;
|
|
33
|
+
const session = createSessionEvent({
|
|
34
|
+
authConfig,
|
|
35
|
+
plugins
|
|
36
|
+
});
|
|
37
|
+
if (session) events.session = session;
|
|
38
|
+
const signIn = createSignInEvent({
|
|
39
|
+
authConfig,
|
|
40
|
+
plugins
|
|
41
|
+
});
|
|
42
|
+
if (signIn) events.signIn = signIn;
|
|
43
|
+
const signOut = createSignOutEvent({
|
|
44
|
+
authConfig,
|
|
45
|
+
plugins
|
|
46
|
+
});
|
|
47
|
+
if (signOut) events.signOut = signOut;
|
|
48
|
+
const updateUser = createUpdateUserEvent({
|
|
49
|
+
authConfig,
|
|
50
|
+
plugins
|
|
51
|
+
});
|
|
52
|
+
if (updateUser) events.updateUser = updateUser;
|
|
53
|
+
return events;
|
|
54
|
+
}
|
|
55
|
+
export default createEvents;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2022 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import createEventPlugins from './createEventPlugins.js';
|
|
16
|
+
function createLinkAccountEvent({ authConfig , plugins }) {
|
|
17
|
+
const linkAccountPlugins = createEventPlugins({
|
|
18
|
+
authConfig,
|
|
19
|
+
plugins,
|
|
20
|
+
type: 'linkAccount'
|
|
21
|
+
});
|
|
22
|
+
if (linkAccountPlugins.length === 0) return undefined;
|
|
23
|
+
async function linkAccountEvent({ account , user }) {
|
|
24
|
+
for (const plugin of linkAccountPlugins){
|
|
25
|
+
await plugin.fn({
|
|
26
|
+
properties: plugin.properties ?? {},
|
|
27
|
+
account,
|
|
28
|
+
user
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return linkAccountEvent;
|
|
33
|
+
}
|
|
34
|
+
export default createLinkAccountEvent;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2022 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import createEventPlugins from './createEventPlugins.js';
|
|
16
|
+
function createSessionEvent({ authConfig , plugins }) {
|
|
17
|
+
const sessionPlugins = createEventPlugins({
|
|
18
|
+
authConfig,
|
|
19
|
+
plugins,
|
|
20
|
+
type: 'session'
|
|
21
|
+
});
|
|
22
|
+
if (sessionPlugins.length === 0) return undefined;
|
|
23
|
+
async function sessionEvent({ session , token }) {
|
|
24
|
+
for (const plugin of sessionPlugins){
|
|
25
|
+
await plugin.fn({
|
|
26
|
+
properties: plugin.properties ?? {},
|
|
27
|
+
session,
|
|
28
|
+
token
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return sessionEvent;
|
|
33
|
+
}
|
|
34
|
+
export default createSessionEvent;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2022 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import createEventPlugins from './createEventPlugins.js';
|
|
16
|
+
function createSignInEvent({ authConfig , plugins }) {
|
|
17
|
+
const signInPlugins = createEventPlugins({
|
|
18
|
+
authConfig,
|
|
19
|
+
plugins,
|
|
20
|
+
type: 'signIn'
|
|
21
|
+
});
|
|
22
|
+
if (signInPlugins.length === 0) return undefined;
|
|
23
|
+
async function signInEvent({ account , isNewUser , profile , user }) {
|
|
24
|
+
for (const plugin of signInPlugins){
|
|
25
|
+
await plugin.fn({
|
|
26
|
+
properties: plugin.properties ?? {},
|
|
27
|
+
account,
|
|
28
|
+
isNewUser,
|
|
29
|
+
profile,
|
|
30
|
+
user
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return signInEvent;
|
|
35
|
+
}
|
|
36
|
+
export default createSignInEvent;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2022 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import createEventPlugins from './createEventPlugins.js';
|
|
16
|
+
function createSignOutEvent({ authConfig , plugins }) {
|
|
17
|
+
const signInPlugins = createEventPlugins({
|
|
18
|
+
authConfig,
|
|
19
|
+
plugins,
|
|
20
|
+
type: 'signOut'
|
|
21
|
+
});
|
|
22
|
+
if (signInPlugins.length === 0) return undefined;
|
|
23
|
+
async function signInEvent({ session , token }) {
|
|
24
|
+
for (const plugin of signInPlugins){
|
|
25
|
+
await plugin.fn({
|
|
26
|
+
properties: plugin.properties ?? {},
|
|
27
|
+
session,
|
|
28
|
+
token
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return signInEvent;
|
|
33
|
+
}
|
|
34
|
+
export default createSignOutEvent;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2022 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import createEventPlugins from './createEventPlugins.js';
|
|
16
|
+
function createUpdateUserEvent({ authConfig , plugins }) {
|
|
17
|
+
const updateUserPlugins = createEventPlugins({
|
|
18
|
+
authConfig,
|
|
19
|
+
plugins,
|
|
20
|
+
type: 'updateUser'
|
|
21
|
+
});
|
|
22
|
+
if (updateUserPlugins.length === 0) return undefined;
|
|
23
|
+
async function updateUserEvent({ user }) {
|
|
24
|
+
for (const plugin of updateUserPlugins){
|
|
25
|
+
await plugin.fn({
|
|
26
|
+
properties: plugin.properties ?? {},
|
|
27
|
+
user
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return updateUserEvent;
|
|
32
|
+
}
|
|
33
|
+
export default createUpdateUserEvent;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2022 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { NodeParser } from '@lowdefy/operators';
|
|
16
|
+
import { getSecretsFromEnv } from '@lowdefy/node-utils';
|
|
17
|
+
import { _secret } from '@lowdefy/operators-js/operators/server';
|
|
18
|
+
import createCallbacks from './callbacks/createCallbacks.js';
|
|
19
|
+
import createEvents from './events/createEvents.js';
|
|
20
|
+
import createProviders from './createProviders.js';
|
|
21
|
+
const nextAuthConfig = {};
|
|
22
|
+
let initialized = false;
|
|
23
|
+
function getNextAuthConfig({ authJson , plugins }) {
|
|
24
|
+
if (initialized) return nextAuthConfig;
|
|
25
|
+
const secrets = getSecretsFromEnv();
|
|
26
|
+
const operatorsParser = new NodeParser({
|
|
27
|
+
operators: {
|
|
28
|
+
_secret
|
|
29
|
+
},
|
|
30
|
+
payload: {},
|
|
31
|
+
secrets,
|
|
32
|
+
user: {}
|
|
33
|
+
});
|
|
34
|
+
const { output: authConfig , errors: operatorErrors } = operatorsParser.parse({
|
|
35
|
+
input: authJson,
|
|
36
|
+
location: 'auth'
|
|
37
|
+
});
|
|
38
|
+
if (operatorErrors.length > 0) {
|
|
39
|
+
throw new Error(operatorErrors[0]);
|
|
40
|
+
}
|
|
41
|
+
nextAuthConfig.callbacks = createCallbacks({
|
|
42
|
+
authConfig,
|
|
43
|
+
plugins
|
|
44
|
+
});
|
|
45
|
+
nextAuthConfig.events = createEvents({
|
|
46
|
+
authConfig,
|
|
47
|
+
plugins
|
|
48
|
+
});
|
|
49
|
+
nextAuthConfig.providers = createProviders({
|
|
50
|
+
authConfig,
|
|
51
|
+
plugins
|
|
52
|
+
});
|
|
53
|
+
nextAuthConfig.session = authConfig.session;
|
|
54
|
+
nextAuthConfig.theme = authConfig.theme;
|
|
55
|
+
initialized = true;
|
|
56
|
+
return nextAuthConfig;
|
|
57
|
+
}
|
|
58
|
+
export default getNextAuthConfig;
|
|
@@ -23,11 +23,12 @@ import getConnectionConfig from './getConnectionConfig.js';
|
|
|
23
23
|
import getRequestConfig from './getRequestConfig.js';
|
|
24
24
|
import getRequestResolver from './getRequestResolver.js';
|
|
25
25
|
import validateSchemas from './validateSchemas.js';
|
|
26
|
-
async function callRequest(context, { pageId , payload , requestId }) {
|
|
26
|
+
async function callRequest(context, { blockId , pageId , payload , requestId }) {
|
|
27
27
|
const { logger } = context;
|
|
28
28
|
logger.debug({
|
|
29
29
|
route: 'request',
|
|
30
30
|
params: {
|
|
31
|
+
blockId,
|
|
31
32
|
pageId,
|
|
32
33
|
payload,
|
|
33
34
|
requestId
|
|
@@ -26,8 +26,7 @@ async function getHomeAndMenus(context) {
|
|
|
26
26
|
menus
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
|
-
let defaultMenu = menus.find((menu)=>menu.menuId === 'default'
|
|
30
|
-
);
|
|
29
|
+
let defaultMenu = menus.find((menu)=>menu.menuId === 'default');
|
|
31
30
|
if (!defaultMenu) {
|
|
32
31
|
// eslint-disable-next-line prefer-destructuring
|
|
33
32
|
defaultMenu = menus[0];
|
package/dist/test/testContext.js
CHANGED
|
@@ -20,13 +20,10 @@ function testContext({ config ={} , connections ={} , headers ={} , host ='host'
|
|
|
20
20
|
warn: ()=>{}
|
|
21
21
|
} , operators ={
|
|
22
22
|
_test: ()=>'test'
|
|
23
|
-
} , readConfigFile ,
|
|
24
|
-
const authenticated = user && !!user.sub;
|
|
23
|
+
} , readConfigFile , secrets ={} , setHeader , session , protocol ='https' , } = {}) {
|
|
25
24
|
return {
|
|
26
|
-
authenticated,
|
|
27
25
|
authorize: createAuthorize({
|
|
28
|
-
|
|
29
|
-
roles
|
|
26
|
+
session
|
|
30
27
|
}),
|
|
31
28
|
config,
|
|
32
29
|
connections,
|
|
@@ -38,7 +35,7 @@ function testContext({ config ={} , connections ={} , headers ={} , host ='host'
|
|
|
38
35
|
readConfigFile,
|
|
39
36
|
secrets,
|
|
40
37
|
setHeader,
|
|
41
|
-
user
|
|
38
|
+
user: session?.user
|
|
42
39
|
};
|
|
43
40
|
}
|
|
44
41
|
export default testContext;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lowdefy/api",
|
|
3
|
-
"version": "4.0.0-alpha.
|
|
4
|
-
"
|
|
3
|
+
"version": "4.0.0-alpha.13",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
5
|
"description": "",
|
|
6
6
|
"homepage": "https://lowdefy.com",
|
|
7
7
|
"keywords": [
|
|
@@ -41,22 +41,22 @@
|
|
|
41
41
|
"test": "yarn node --experimental-vm-modules $(yarn bin jest)"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@lowdefy/ajv": "4.0.0-alpha.
|
|
45
|
-
"@lowdefy/helpers": "4.0.0-alpha.
|
|
46
|
-
"@lowdefy/node-utils": "4.0.0-alpha.
|
|
47
|
-
"@lowdefy/nunjucks": "4.0.0-alpha.
|
|
48
|
-
"@lowdefy/operators": "4.0.0-alpha.
|
|
44
|
+
"@lowdefy/ajv": "4.0.0-alpha.13",
|
|
45
|
+
"@lowdefy/helpers": "4.0.0-alpha.13",
|
|
46
|
+
"@lowdefy/node-utils": "4.0.0-alpha.13",
|
|
47
|
+
"@lowdefy/nunjucks": "4.0.0-alpha.13",
|
|
48
|
+
"@lowdefy/operators": "4.0.0-alpha.13"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
|
-
"@jest/globals": "
|
|
52
|
-
"@lowdefy/operators-js": "4.0.0-alpha.
|
|
53
|
-
"@swc/cli": "0.1.
|
|
54
|
-
"@swc/core": "1.2.
|
|
55
|
-
"@swc/jest": "0.2.
|
|
56
|
-
"jest": "
|
|
51
|
+
"@jest/globals": "28.1.0",
|
|
52
|
+
"@lowdefy/operators-js": "4.0.0-alpha.13",
|
|
53
|
+
"@swc/cli": "0.1.57",
|
|
54
|
+
"@swc/core": "1.2.194",
|
|
55
|
+
"@swc/jest": "0.2.21",
|
|
56
|
+
"jest": "28.1.0"
|
|
57
57
|
},
|
|
58
58
|
"publishConfig": {
|
|
59
59
|
"access": "public"
|
|
60
60
|
},
|
|
61
|
-
"gitHead": "
|
|
61
|
+
"gitHead": "e99b4b6c1f59804982fc148c0fe39dcf13b35d77"
|
|
62
62
|
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2020-2022 Lowdefy, Inc
|
|
3
|
-
|
|
4
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
you may not use this file except in compliance with the License.
|
|
6
|
-
You may obtain a copy of the License at
|
|
7
|
-
|
|
8
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
|
|
10
|
-
Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
See the License for the specific language governing permissions and
|
|
14
|
-
limitations under the License.
|
|
15
|
-
*/ import createAuthorize from './createAuthorize.js';
|
|
16
|
-
import createReadConfigFile from './readConfigFile.js';
|
|
17
|
-
import verifyAuthorizationHeader from './verifyAuthorizationHeader.js';
|
|
18
|
-
async function createContext({ buildDirectory , connections , secrets }) {
|
|
19
|
-
const readConfigFile = createReadConfigFile({
|
|
20
|
-
buildDirectory
|
|
21
|
-
});
|
|
22
|
-
const config = await readConfigFile('config.json');
|
|
23
|
-
function contextFn({ headers , host , logger , protocol , setHeader }) {
|
|
24
|
-
const context = {
|
|
25
|
-
config,
|
|
26
|
-
connections,
|
|
27
|
-
headers,
|
|
28
|
-
host,
|
|
29
|
-
logger,
|
|
30
|
-
protocol,
|
|
31
|
-
readConfigFile,
|
|
32
|
-
secrets,
|
|
33
|
-
setHeader
|
|
34
|
-
};
|
|
35
|
-
const { authenticated , user , roles } = verifyAuthorizationHeader(context);
|
|
36
|
-
context.authorize = createAuthorize({
|
|
37
|
-
authenticated,
|
|
38
|
-
roles
|
|
39
|
-
});
|
|
40
|
-
context.authenticated = authenticated;
|
|
41
|
-
context.user = user;
|
|
42
|
-
return context;
|
|
43
|
-
}
|
|
44
|
-
return contextFn;
|
|
45
|
-
}
|
|
46
|
-
export default createContext;
|