@lowdefy/api 4.0.0-alpha.9 → 4.0.0-rc.0

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.
Files changed (33) hide show
  1. package/dist/context/createApiContext.js +10 -8
  2. package/dist/context/createAuthorize.js +6 -3
  3. package/dist/context/{readConfigFile.js → createReadConfigFile.js} +5 -2
  4. package/dist/context/errors.js +2 -14
  5. package/dist/index.js +3 -2
  6. package/dist/routes/auth/callbacks/addUserFieldsToSession.js +27 -0
  7. package/dist/routes/auth/callbacks/addUserFieldsToToken.js +36 -0
  8. package/dist/routes/auth/callbacks/createCallbackPlugins.js +21 -0
  9. package/dist/routes/auth/callbacks/createCallbacks.js +43 -0
  10. package/dist/routes/auth/callbacks/createJWTCallback.js +75 -0
  11. package/dist/routes/auth/callbacks/createRedirectCallback.js +36 -0
  12. package/dist/routes/auth/callbacks/createSessionCallback.js +71 -0
  13. package/dist/routes/auth/callbacks/createSignInCallback.js +40 -0
  14. package/dist/routes/auth/createAdapter.js +24 -0
  15. package/dist/routes/auth/createProviders.js +25 -0
  16. package/dist/routes/auth/events/createCreateUserEvent.js +33 -0
  17. package/dist/routes/auth/events/createEventPlugins.js +21 -0
  18. package/dist/routes/auth/events/createEvents.js +55 -0
  19. package/dist/routes/auth/events/createLinkAccountEvent.js +35 -0
  20. package/dist/routes/auth/events/createSessionEvent.js +34 -0
  21. package/dist/routes/auth/events/createSignInEvent.js +36 -0
  22. package/dist/routes/auth/events/createSignOutEvent.js +34 -0
  23. package/dist/routes/auth/events/createUpdateUserEvent.js +33 -0
  24. package/dist/routes/auth/getNextAuthConfig.js +66 -0
  25. package/dist/routes/page/getPageConfig.js +5 -2
  26. package/dist/routes/request/callRequest.js +8 -4
  27. package/dist/routes/request/callRequestResolver.js +6 -2
  28. package/dist/routes/rootConfig/getHomeAndMenus.js +1 -2
  29. package/dist/routes/rootConfig/getRootConfig.js +0 -1
  30. package/dist/routes/rootConfig/menus/filterMenuList.js +1 -2
  31. package/dist/test/testContext.js +3 -6
  32. package/package.json +17 -18
  33. 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 createReadConfigFile from './readConfigFile.js';
16
- async function createApiContext({ buildDirectory , connections , logger , operators , secrets }) {
15
+ */ import createAuthorize from './createAuthorize.js';
16
+ import createReadConfigFile from './createReadConfigFile.js';
17
+ function createApiContext({ buildDirectory , config , connections , fileCache , logger , operators , secrets , session , }) {
17
18
  const readConfigFile = createReadConfigFile({
18
- buildDirectory
19
+ buildDirectory,
20
+ fileCache
19
21
  });
20
- const config = await readConfigFile('config.json');
21
22
  return {
22
- authenticated: false,
23
- authorize: ()=>true
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({ authenticated =false , roles =[] }) {
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
  }
@@ -15,7 +15,7 @@
15
15
  */ import path from 'path';
16
16
  import { cachedPromises } from '@lowdefy/helpers';
17
17
  import { getFileExtension, readFile } from '@lowdefy/node-utils';
18
- function createReadConfigFile({ buildDirectory }) {
18
+ function createReadConfigFile({ buildDirectory , fileCache }) {
19
19
  async function readConfigFile(filePath) {
20
20
  const fileContent = await readFile(path.resolve(buildDirectory, filePath));
21
21
  if (getFileExtension(filePath) === 'json') {
@@ -23,6 +23,9 @@ function createReadConfigFile({ buildDirectory }) {
23
23
  }
24
24
  return fileContent;
25
25
  }
26
- return cachedPromises(readConfigFile);
26
+ return cachedPromises({
27
+ cache: fileCache,
28
+ getter: readConfigFile
29
+ });
27
30
  }
28
31
  export default createReadConfigFile;
@@ -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 AuthenticationError = class AuthenticationError extends Error {
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
- let TokenExpiredError = class TokenExpiredError extends Error {
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 { AuthenticationError, ConfigurationError, RequestError, ServerError, TokenExpiredError } from './context/errors.js';
21
- export { AuthenticationError, callRequest, ConfigurationError, createApiContext, getHomeAndMenus, getPageConfig, getRootConfig, RequestError, ServerError, TokenExpiredError };
21
+ import { ConfigurationError, RequestError, ServerError } from './context/errors.js';
22
+ export { callRequest, ConfigurationError, createApiContext, getHomeAndMenus, getNextAuthConfig, getPageConfig, getRootConfig, RequestError, ServerError };
@@ -0,0 +1,27 @@
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 addUserFieldsToSession(context, { session , token , authConfig , user }) {
16
+ if (token) {
17
+ Object.keys(authConfig.userFields).forEach((fieldName)=>{
18
+ session.user[fieldName] = token[fieldName];
19
+ });
20
+ }
21
+ if (user) {
22
+ Object.keys(authConfig.userFields).forEach((fieldName)=>{
23
+ session.user[fieldName] = user[fieldName];
24
+ });
25
+ }
26
+ }
27
+ export default addUserFieldsToSession;
@@ -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 { get } from '@lowdefy/helpers';
16
+ function addUserFieldsToToken(context, { account , authConfig , profile , token , user }) {
17
+ // const { debug } = context.logger;
18
+ const objects = {
19
+ account,
20
+ profile,
21
+ user
22
+ };
23
+ // TODO: Add when debug is fixed.
24
+ // debug('Adding userFields to user. Available provider data is:');
25
+ // debug(objects);
26
+ Object.entries(authConfig.userFields).forEach(([lowdefyFieldName, providerFieldName])=>{
27
+ const value = get(objects, providerFieldName);
28
+ // debug(
29
+ // `Adding provider field "${providerFieldName}" with value ${JSON.stringify(
30
+ // value
31
+ // )} as "${lowdefyFieldName}"`
32
+ // );
33
+ token[lowdefyFieldName] = value;
34
+ });
35
+ }
36
+ export default addUserFieldsToToken;
@@ -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(context, { authConfig , plugins }) {
20
+ const callbacks = {
21
+ session: createSessionCallback(context, {
22
+ authConfig,
23
+ plugins
24
+ })
25
+ };
26
+ const jwt = createJWTCallback(context, {
27
+ authConfig,
28
+ plugins
29
+ });
30
+ if (jwt) callbacks.jwt = jwt;
31
+ const redirect = createRedirectCallback(context, {
32
+ authConfig,
33
+ plugins
34
+ });
35
+ if (redirect) callbacks.redirect = redirect;
36
+ const signIn = createSignInCallback(context, {
37
+ authConfig,
38
+ plugins
39
+ });
40
+ if (signIn) callbacks.signIn = signIn;
41
+ return callbacks;
42
+ }
43
+ export default createCallbacks;
@@ -0,0 +1,75 @@
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 addUserFieldsToToken from './addUserFieldsToToken.js';
16
+ import createCallbackPlugins from './createCallbackPlugins.js';
17
+ function createJWTCallback(context, { authConfig , plugins }) {
18
+ const jwtCallbackPlugins = createCallbackPlugins({
19
+ authConfig,
20
+ plugins,
21
+ type: 'jwt'
22
+ });
23
+ async function jwtCallback({ token , user , account , profile , isNewUser }) {
24
+ if (profile) {
25
+ 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;
26
+ token = {
27
+ sub,
28
+ name,
29
+ given_name,
30
+ family_name,
31
+ middle_name,
32
+ nickname,
33
+ preferred_username,
34
+ profile: profile_claim,
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
+ ...token
48
+ };
49
+ }
50
+ if (profile || user) {
51
+ if (authConfig.userFields) {
52
+ addUserFieldsToToken(context, {
53
+ authConfig,
54
+ account,
55
+ profile,
56
+ token,
57
+ user
58
+ });
59
+ }
60
+ }
61
+ for (const plugin of jwtCallbackPlugins){
62
+ token = await plugin.fn({
63
+ properties: plugin.properties ?? {},
64
+ account,
65
+ isNewUser,
66
+ profile,
67
+ token,
68
+ user
69
+ });
70
+ }
71
+ return token;
72
+ }
73
+ return jwtCallback;
74
+ }
75
+ 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(context, { 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,71 @@
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 addUserFieldsToSession from './addUserFieldsToSession.js';
16
+ import createCallbackPlugins from './createCallbackPlugins.js';
17
+ function createSessionCallback(context, { authConfig , plugins }) {
18
+ const sessionCallbackPlugins = createCallbackPlugins({
19
+ authConfig,
20
+ plugins,
21
+ type: 'session'
22
+ });
23
+ async function sessionCallback({ 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
+ if (authConfig.userFields) {
51
+ addUserFieldsToSession(context, {
52
+ authConfig,
53
+ session,
54
+ token,
55
+ user
56
+ });
57
+ }
58
+ for (const plugin of sessionCallbackPlugins){
59
+ // eslint-disable-next-line no-param-reassign
60
+ session = await plugin.fn({
61
+ properties: plugin.properties ?? {},
62
+ session,
63
+ token,
64
+ user
65
+ });
66
+ }
67
+ return session;
68
+ }
69
+ return sessionCallback;
70
+ }
71
+ 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(context, { 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,24 @@
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 createAdapter(context, { authConfig , plugins }) {
16
+ const adapterConfig = authConfig.adapter;
17
+ if (!adapterConfig) {
18
+ return undefined;
19
+ }
20
+ return plugins.adapters[adapterConfig.type]({
21
+ properties: adapterConfig.properties
22
+ });
23
+ }
24
+ export default createAdapter;
@@ -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(context, { 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(context, { 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(context, { authConfig , plugins }) {
22
+ const events = {};
23
+ const createUser = createCreateUserEvent(context, {
24
+ authConfig,
25
+ plugins
26
+ });
27
+ if (createUser) events.createUser = createUser;
28
+ const linkAccount = createLinkAccountEvent(context, {
29
+ authConfig,
30
+ plugins
31
+ });
32
+ if (linkAccount) events.linkAccount = linkAccount;
33
+ const session = createSessionEvent(context, {
34
+ authConfig,
35
+ plugins
36
+ });
37
+ if (session) events.session = session;
38
+ const signIn = createSignInEvent(context, {
39
+ authConfig,
40
+ plugins
41
+ });
42
+ if (signIn) events.signIn = signIn;
43
+ const signOut = createSignOutEvent(context, {
44
+ authConfig,
45
+ plugins
46
+ });
47
+ if (signOut) events.signOut = signOut;
48
+ const updateUser = createUpdateUserEvent(context, {
49
+ authConfig,
50
+ plugins
51
+ });
52
+ if (updateUser) events.updateUser = updateUser;
53
+ return events;
54
+ }
55
+ export default createEvents;
@@ -0,0 +1,35 @@
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(context, { 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 , profile , user }) {
24
+ for (const plugin of linkAccountPlugins){
25
+ await plugin.fn({
26
+ properties: plugin.properties ?? {},
27
+ account,
28
+ profile,
29
+ user
30
+ });
31
+ }
32
+ }
33
+ return linkAccountEvent;
34
+ }
35
+ 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(context, { 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(context, { 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(context, { 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(context, { 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,66 @@
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 createAdapter from './createAdapter.js';
19
+ import createCallbacks from './callbacks/createCallbacks.js';
20
+ import createEvents from './events/createEvents.js';
21
+ import createProviders from './createProviders.js';
22
+ const nextAuthConfig = {};
23
+ let initialized = false;
24
+ function getNextAuthConfig(context, { authJson , plugins }) {
25
+ if (initialized) return nextAuthConfig;
26
+ const secrets = getSecretsFromEnv();
27
+ // TODO: Add logger
28
+ const operatorsParser = new NodeParser({
29
+ operators: {
30
+ _secret
31
+ },
32
+ payload: {},
33
+ secrets,
34
+ user: {}
35
+ });
36
+ const { output: authConfig , errors: operatorErrors } = operatorsParser.parse({
37
+ input: authJson,
38
+ location: 'auth'
39
+ });
40
+ if (operatorErrors.length > 0) {
41
+ throw new Error(operatorErrors[0]);
42
+ }
43
+ nextAuthConfig.adapter = createAdapter(context, {
44
+ authConfig,
45
+ plugins
46
+ });
47
+ nextAuthConfig.callbacks = createCallbacks(context, {
48
+ authConfig,
49
+ plugins
50
+ });
51
+ nextAuthConfig.events = createEvents(context, {
52
+ authConfig,
53
+ plugins
54
+ });
55
+ nextAuthConfig.providers = createProviders(context, {
56
+ authConfig,
57
+ plugins
58
+ });
59
+ nextAuthConfig.session = authConfig.session;
60
+ nextAuthConfig.theme = authConfig.theme;
61
+ nextAuthConfig.pages = authConfig.authPages;
62
+ nextAuthConfig.cookies = authConfig?.advanced?.cookies;
63
+ initialized = true;
64
+ return nextAuthConfig;
65
+ }
66
+ export default getNextAuthConfig;
@@ -15,8 +15,11 @@
15
15
  */ async function getPageConfig({ authorize , readConfigFile }, { pageId }) {
16
16
  const pageConfig = await readConfigFile(`pages/${pageId}/${pageId}.json`);
17
17
  if (pageConfig && authorize(pageConfig)) {
18
- delete pageConfig.auth;
19
- return pageConfig;
18
+ // eslint-disable-next-line no-unused-vars
19
+ const { auth , ...rest } = pageConfig;
20
+ return {
21
+ ...rest
22
+ };
20
23
  }
21
24
  return null;
22
25
  }
@@ -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
@@ -50,9 +51,10 @@ async function callRequest(context, { pageId , payload , requestId }) {
50
51
  connection,
51
52
  requestConfig
52
53
  });
54
+ const deserializedPayload = serializer.deserialize(payload);
53
55
  const { connectionProperties , requestProperties } = evaluateOperators(context, {
54
56
  connectionConfig,
55
- payload: serializer.deserialize(payload),
57
+ payload: deserializedPayload,
56
58
  requestConfig
57
59
  });
58
60
  checkConnectionRead(context, {
@@ -75,10 +77,12 @@ async function callRequest(context, { pageId , payload , requestId }) {
75
77
  requestProperties
76
78
  });
77
79
  const response = await callRequestResolver(context, {
80
+ blockId,
78
81
  connectionProperties,
82
+ payload: deserializedPayload,
79
83
  requestConfig,
80
- requestResolver,
81
- requestProperties
84
+ requestProperties,
85
+ requestResolver
82
86
  });
83
87
  return {
84
88
  id: requestConfig.id,
@@ -13,11 +13,15 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { RequestError } from '../../context/errors.js';
16
- async function callRequestResolver({ logger }, { connectionProperties , requestConfig , requestProperties , requestResolver }) {
16
+ async function callRequestResolver({ logger }, { blockId , connectionProperties , payload , requestConfig , requestProperties , requestResolver }) {
17
17
  try {
18
18
  const response = await requestResolver({
19
+ blockId,
20
+ connection: connectionProperties,
21
+ pageId: requestConfig.pageId,
22
+ payload,
19
23
  request: requestProperties,
20
- connection: connectionProperties
24
+ requestId: requestConfig.requestId
21
25
  });
22
26
  return response;
23
27
  } catch (error) {
@@ -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];
@@ -20,7 +20,6 @@ async function getRootConfig(context) {
20
20
  getHomeAndMenus(context),
21
21
  ]);
22
22
  return {
23
- authenticated: context.authenticated,
24
23
  home,
25
24
  lowdefyGlobal,
26
25
  menus
@@ -36,7 +36,6 @@ function filterMenuList(context, { menuList }) {
36
36
  }
37
37
  }
38
38
  return null;
39
- }).filter((item)=>item !== null
40
- );
39
+ }).filter((item)=>item !== null);
41
40
  }
42
41
  export default filterMenuList;
@@ -20,13 +20,10 @@ function testContext({ config ={} , connections ={} , headers ={} , host ='host'
20
20
  warn: ()=>{}
21
21
  } , operators ={
22
22
  _test: ()=>'test'
23
- } , readConfigFile , roles , secrets ={} , setHeader , user , protocol ='https' , } = {}) {
24
- const authenticated = user && !!user.sub;
23
+ } , readConfigFile , secrets ={} , setHeader , session , protocol ='https' , } = {}) {
25
24
  return {
26
- authenticated,
27
25
  authorize: createAuthorize({
28
- authenticated,
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.9",
4
- "licence": "Apache-2.0",
3
+ "version": "4.0.0-rc.0",
4
+ "license": "Apache-2.0",
5
5
  "description": "",
6
6
  "homepage": "https://lowdefy.com",
7
7
  "keywords": [
@@ -34,29 +34,28 @@
34
34
  "dist/*"
35
35
  ],
36
36
  "scripts": {
37
- "build": "yarn swc",
37
+ "build": "swc src --out-dir dist --config-file ../../.swcrc --delete-dir-on-start",
38
38
  "clean": "rm -rf dist",
39
- "prepare": "yarn build",
40
- "swc": "swc src --out-dir dist --config-file ../../.swcrc --delete-dir-on-start",
41
- "test": "yarn node --experimental-vm-modules $(yarn bin jest)"
39
+ "prepublishOnly": "pnpm build",
40
+ "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
42
41
  },
43
42
  "dependencies": {
44
- "@lowdefy/ajv": "4.0.0-alpha.9",
45
- "@lowdefy/helpers": "4.0.0-alpha.9",
46
- "@lowdefy/node-utils": "4.0.0-alpha.9",
47
- "@lowdefy/nunjucks": "4.0.0-alpha.9",
48
- "@lowdefy/operators": "4.0.0-alpha.9"
43
+ "@lowdefy/ajv": "4.0.0-rc.0",
44
+ "@lowdefy/helpers": "4.0.0-rc.0",
45
+ "@lowdefy/node-utils": "4.0.0-rc.0",
46
+ "@lowdefy/nunjucks": "4.0.0-rc.0",
47
+ "@lowdefy/operators": "4.0.0-rc.0",
48
+ "@lowdefy/operators-js": "4.0.0-rc.0"
49
49
  },
50
50
  "devDependencies": {
51
- "@jest/globals": "27.5.1",
52
- "@lowdefy/operators-js": "4.0.0-alpha.9",
53
- "@swc/cli": "0.1.55",
54
- "@swc/core": "1.2.135",
55
- "@swc/jest": "0.2.17",
56
- "jest": "27.5.1"
51
+ "@jest/globals": "28.1.0",
52
+ "@swc/cli": "0.1.57",
53
+ "@swc/core": "1.2.194",
54
+ "@swc/jest": "0.2.21",
55
+ "jest": "28.1.0"
57
56
  },
58
57
  "publishConfig": {
59
58
  "access": "public"
60
59
  },
61
- "gitHead": "98b544eca231bdcfca6c3a8601a891835d5ce571"
60
+ "gitHead": "f6872d7ff6da421710096536fce7b2016ef8f35c"
62
61
  }
@@ -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;