@lowdefy/api 4.0.0-rc.1 → 4.0.0-rc.10

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 (41) hide show
  1. package/dist/context/createApiContext.js +4 -17
  2. package/dist/context/createAuthorize.js +2 -2
  3. package/dist/context/createReadConfigFile.js +1 -1
  4. package/dist/routes/auth/callbacks/addUserFieldsToSession.js +4 -3
  5. package/dist/routes/auth/callbacks/addUserFieldsToToken.js +6 -12
  6. package/dist/routes/auth/callbacks/createCallbackPlugins.js +1 -1
  7. package/dist/routes/auth/callbacks/createCallbacks.js +6 -5
  8. package/dist/routes/auth/callbacks/createJWTCallback.js +5 -4
  9. package/dist/routes/auth/callbacks/createRedirectCallback.js +2 -2
  10. package/dist/routes/auth/callbacks/createSessionCallback.js +4 -4
  11. package/dist/routes/auth/callbacks/createSignInCallback.js +2 -2
  12. package/dist/routes/auth/createAdapter.js +1 -1
  13. package/dist/routes/auth/createLogger.js +34 -0
  14. package/dist/routes/auth/createProviders.js +1 -1
  15. package/dist/routes/auth/events/createCreateUserEvent.js +11 -3
  16. package/dist/routes/auth/events/createEventPlugins.js +1 -1
  17. package/dist/routes/auth/events/createEvents.js +29 -28
  18. package/dist/routes/auth/events/createLinkAccountEvent.js +11 -3
  19. package/dist/routes/auth/events/createSessionEvent.js +2 -2
  20. package/dist/routes/auth/events/createSignInEvent.js +13 -3
  21. package/dist/routes/auth/events/createSignOutEvent.js +15 -6
  22. package/dist/routes/auth/events/createUpdateUserEvent.js +11 -3
  23. package/dist/routes/auth/getNextAuthConfig.js +16 -12
  24. package/dist/routes/page/getPageConfig.js +2 -2
  25. package/dist/routes/request/authorizeRequest.js +10 -6
  26. package/dist/routes/request/callRequest.js +9 -11
  27. package/dist/routes/request/callRequestResolver.js +1 -1
  28. package/dist/routes/request/checkConnectionRead.js +1 -1
  29. package/dist/routes/request/checkConnectionWrite.js +1 -1
  30. package/dist/routes/request/evaluateOperators.js +6 -6
  31. package/dist/routes/request/getConnection.js +1 -1
  32. package/dist/routes/request/getConnectionConfig.js +2 -2
  33. package/dist/routes/request/getRequestConfig.js +1 -1
  34. package/dist/routes/request/getRequestResolver.js +1 -1
  35. package/dist/routes/request/validateSchemas.js +1 -1
  36. package/dist/routes/rootConfig/getLowdefyGlobal.js +1 -1
  37. package/dist/routes/rootConfig/getRootConfig.js +1 -1
  38. package/dist/routes/rootConfig/menus/filterMenuList.js +2 -2
  39. package/dist/routes/rootConfig/menus/filterMenus.js +1 -1
  40. package/dist/test/testContext.js +4 -7
  41. package/package.json +13 -13
@@ -14,22 +14,9 @@
14
14
  limitations under the License.
15
15
  */ import createAuthorize from './createAuthorize.js';
16
16
  import createReadConfigFile from './createReadConfigFile.js';
17
- function createApiContext({ buildDirectory , config , connections , fileCache , logger , operators , secrets , session }) {
18
- const readConfigFile = createReadConfigFile({
19
- buildDirectory,
20
- fileCache
21
- });
22
- return {
23
- authorize: createAuthorize({
24
- session
25
- }),
26
- config,
27
- connections,
28
- logger,
29
- operators,
30
- readConfigFile,
31
- secrets,
32
- user: session?.user
33
- };
17
+ function createApiContext(context) {
18
+ context.readConfigFile = createReadConfigFile(context);
19
+ context.authorize = createAuthorize(context);
20
+ context.user = context?.session?.user;
34
21
  }
35
22
  export default createApiContext;
@@ -13,12 +13,12 @@
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({ session }) {
16
+ function createAuthorize({ session }) {
17
17
  // Next-auth getSession provides a session object if the user is authenticated
18
18
  // else session will be null
19
19
  const authenticated = !!session;
20
20
  const roles = session?.user?.roles ?? [];
21
- function authorize({ auth }) {
21
+ function authorize({ auth }) {
22
22
  if (auth.public === true) return true;
23
23
  if (auth.public === false) {
24
24
  if (auth.roles) {
@@ -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 , fileCache }) {
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') {
@@ -12,15 +12,16 @@
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
- */ function addUserFieldsToSession(context, { session , token , authConfig , user }) {
15
+ */ import { get, set } from '@lowdefy/helpers';
16
+ function addUserFieldsToSession({ session, token, authConfig, user }) {
16
17
  if (token) {
17
18
  Object.keys(authConfig.userFields).forEach((fieldName)=>{
18
- session.user[fieldName] = token[fieldName];
19
+ set(session.user, fieldName, get(token, fieldName));
19
20
  });
20
21
  }
21
22
  if (user) {
22
23
  Object.keys(authConfig.userFields).forEach((fieldName)=>{
23
- session.user[fieldName] = user[fieldName];
24
+ set(session.user, fieldName, get(user, fieldName));
24
25
  });
25
26
  }
26
27
  }
@@ -12,25 +12,19 @@
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 { get } from '@lowdefy/helpers';
16
- function addUserFieldsToToken(context, { account , authConfig , profile , token , user }) {
17
- // const { debug } = context.logger;
15
+ */ import { get, set } from '@lowdefy/helpers';
16
+ function addUserFieldsToToken({ account, authConfig, logger, profile, token, user }) {
18
17
  const objects = {
19
18
  account,
20
19
  profile,
21
20
  user
22
21
  };
23
- // TODO: Add when debug is fixed.
24
- // debug('Adding userFields to user. Available provider data is:');
25
- // debug(objects);
22
+ logger.debug('Adding userFields to user. Available provider data is:');
23
+ logger.debug(objects);
26
24
  Object.entries(authConfig.userFields).forEach(([lowdefyFieldName, providerFieldName])=>{
27
25
  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;
26
+ logger.debug(`Adding provider field "${providerFieldName}" with value ${JSON.stringify(value)} as "${lowdefyFieldName}"`);
27
+ set(token, lowdefyFieldName, value);
34
28
  });
35
29
  }
36
30
  export default addUserFieldsToToken;
@@ -12,7 +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
- */ function createCallbackPlugins({ authConfig , plugins , type }) {
15
+ */ function createCallbackPlugins({ authConfig, plugins, type }) {
16
16
  return authConfig.callbacks.map((callbackConfig)=>({
17
17
  fn: plugins.callbacks[callbackConfig.type],
18
18
  properties: callbackConfig.properties
@@ -16,24 +16,25 @@
16
16
  import createRedirectCallback from './createRedirectCallback.js';
17
17
  import createSessionCallback from './createSessionCallback.js';
18
18
  import createSignInCallback from './createSignInCallback.js';
19
- function createCallbacks(context, { authConfig , plugins }) {
19
+ function createCallbacks({ authConfig, logger, plugins }) {
20
20
  const callbacks = {
21
- session: createSessionCallback(context, {
21
+ session: createSessionCallback({
22
22
  authConfig,
23
23
  plugins
24
24
  })
25
25
  };
26
- const jwt = createJWTCallback(context, {
26
+ const jwt = createJWTCallback({
27
27
  authConfig,
28
+ logger,
28
29
  plugins
29
30
  });
30
31
  if (jwt) callbacks.jwt = jwt;
31
- const redirect = createRedirectCallback(context, {
32
+ const redirect = createRedirectCallback({
32
33
  authConfig,
33
34
  plugins
34
35
  });
35
36
  if (redirect) callbacks.redirect = redirect;
36
- const signIn = createSignInCallback(context, {
37
+ const signIn = createSignInCallback({
37
38
  authConfig,
38
39
  plugins
39
40
  });
@@ -14,15 +14,15 @@
14
14
  limitations under the License.
15
15
  */ import addUserFieldsToToken from './addUserFieldsToToken.js';
16
16
  import createCallbackPlugins from './createCallbackPlugins.js';
17
- function createJWTCallback(context, { authConfig , plugins }) {
17
+ function createJWTCallback({ authConfig, logger, plugins }) {
18
18
  const jwtCallbackPlugins = createCallbackPlugins({
19
19
  authConfig,
20
20
  plugins,
21
21
  type: 'jwt'
22
22
  });
23
- async function jwtCallback({ token , user , account , profile , isNewUser }) {
23
+ async function jwtCallback({ token, user, account, profile, isNewUser }) {
24
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;
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
26
  token = {
27
27
  sub,
28
28
  name,
@@ -49,9 +49,10 @@ function createJWTCallback(context, { authConfig , plugins }) {
49
49
  }
50
50
  if (profile || user) {
51
51
  if (authConfig.userFields) {
52
- addUserFieldsToToken(context, {
52
+ addUserFieldsToToken({
53
53
  authConfig,
54
54
  account,
55
+ logger,
55
56
  profile,
56
57
  token,
57
58
  user
@@ -13,7 +13,7 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import createCallbackPlugins from './createCallbackPlugins.js';
16
- function createRedirectCallback(context, { authConfig , plugins }) {
16
+ function createRedirectCallback({ authConfig, plugins }) {
17
17
  const redirectCallbackPlugins = createCallbackPlugins({
18
18
  authConfig,
19
19
  plugins,
@@ -24,7 +24,7 @@ function createRedirectCallback(context, { authConfig , plugins }) {
24
24
  throw new Error('More than one auth redirect callbacks are configured. Only one is allowed.');
25
25
  }
26
26
  const [plugin] = redirectCallbackPlugins;
27
- async function redirectCallback({ url , baseUrl }) {
27
+ async function redirectCallback({ url, baseUrl }) {
28
28
  return plugin.fn({
29
29
  properties: plugin.properties ?? {},
30
30
  baseUrl,
@@ -14,15 +14,15 @@
14
14
  limitations under the License.
15
15
  */ import addUserFieldsToSession from './addUserFieldsToSession.js';
16
16
  import createCallbackPlugins from './createCallbackPlugins.js';
17
- function createSessionCallback(context, { authConfig , plugins }) {
17
+ function createSessionCallback({ authConfig, plugins }) {
18
18
  const sessionCallbackPlugins = createCallbackPlugins({
19
19
  authConfig,
20
20
  plugins,
21
21
  type: 'session'
22
22
  });
23
- async function sessionCallback({ session , token , user }) {
23
+ async function sessionCallback({ session, token, user }) {
24
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;
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
26
  session.user = {
27
27
  sub,
28
28
  name,
@@ -48,7 +48,7 @@ function createSessionCallback(context, { authConfig , plugins }) {
48
48
  };
49
49
  }
50
50
  if (authConfig.userFields) {
51
- addUserFieldsToSession(context, {
51
+ addUserFieldsToSession({
52
52
  authConfig,
53
53
  session,
54
54
  token,
@@ -13,14 +13,14 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import createCallbackPlugins from './createCallbackPlugins.js';
16
- function createSignInCallback(context, { authConfig , plugins }) {
16
+ function createSignInCallback({ authConfig, plugins }) {
17
17
  const signInCallbackPlugins = createCallbackPlugins({
18
18
  authConfig,
19
19
  plugins,
20
20
  type: 'signIn'
21
21
  });
22
22
  if (signInCallbackPlugins.length === 0) return undefined;
23
- async function signInCallback({ account , credentials , email , profile , user }) {
23
+ async function signInCallback({ account, credentials, email, profile, user }) {
24
24
  let allowSignIn = true;
25
25
  for (const plugin of signInCallbackPlugins){
26
26
  allowSignIn = await plugin.fn({
@@ -12,7 +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
- */ function createAdapter(context, { authConfig , plugins }) {
15
+ */ function createAdapter({ authConfig, plugins }) {
16
16
  const adapterConfig = authConfig.adapter;
17
17
  if (!adapterConfig) {
18
18
  return undefined;
@@ -0,0 +1,34 @@
1
+ /*
2
+ Copyright 2020-2023 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 createLogger({ logger }) {
16
+ return {
17
+ error: (code, metadata)=>logger.error({
18
+ code,
19
+ metadata,
20
+ event: 'auth_error'
21
+ }),
22
+ warn: (code, metadata)=>logger.warn({
23
+ code,
24
+ metadata,
25
+ event: 'auth_warning'
26
+ }),
27
+ debug: (code, metadata)=>logger.debug({
28
+ code,
29
+ metadata,
30
+ event: 'auth_debug'
31
+ })
32
+ };
33
+ }
34
+ export default createLogger;
@@ -16,7 +16,7 @@
16
16
  // Callback url to configure with provider will be: {{ protocol }}{{ host }}/api/auth/callback/{{ providerId }}
17
17
  // This depends on providerId, which might cause some issues if users copy an example and change the id.
18
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 }) {
19
+ function createProviders({ authConfig, plugins }) {
20
20
  return authConfig.providers.map((providerConfig)=>plugins.providers[providerConfig.type]({
21
21
  ...providerConfig.properties,
22
22
  id: providerConfig.id
@@ -13,14 +13,22 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import createEventPlugins from './createEventPlugins.js';
16
- function createCreateUserEvent(context, { authConfig , plugins }) {
16
+ function createCreateUserEvent({ authConfig, logger, plugins }) {
17
17
  const createUserPlugins = createEventPlugins({
18
18
  authConfig,
19
19
  plugins,
20
20
  type: 'createUser'
21
21
  });
22
- if (createUserPlugins.length === 0) return undefined;
23
- async function createUserEvent({ user }) {
22
+ async function createUserEvent({ user }) {
23
+ logger.info({
24
+ event: 'auth_create_user',
25
+ user: {
26
+ id: user.id,
27
+ roles: user.roles,
28
+ sub: user.sub,
29
+ session_id: user.session_id
30
+ }
31
+ });
24
32
  for (const plugin of createUserPlugins){
25
33
  await plugin.fn({
26
34
  properties: plugin.properties ?? {},
@@ -12,7 +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
- */ function createEventPlugins({ authConfig , plugins , type }) {
15
+ */ function createEventPlugins({ authConfig, plugins, type }) {
16
16
  return authConfig.events.map((eventConfig)=>({
17
17
  fn: plugins.events[eventConfig.type],
18
18
  properties: eventConfig.properties
@@ -18,38 +18,39 @@ import createSessionEvent from './createSessionEvent.js';
18
18
  import createSignInEvent from './createSignInEvent.js';
19
19
  import createSignOutEvent from './createSignOutEvent.js';
20
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, {
21
+ function createEvents({ authConfig, logger, plugins }) {
22
+ const events = {
23
+ createUser: createCreateUserEvent({
24
+ authConfig,
25
+ logger,
26
+ plugins
27
+ }),
28
+ linkAccount: createLinkAccountEvent({
29
+ authConfig,
30
+ logger,
31
+ plugins
32
+ }),
33
+ signIn: createSignInEvent({
34
+ authConfig,
35
+ logger,
36
+ plugins
37
+ }),
38
+ signOut: createSignOutEvent({
39
+ authConfig,
40
+ logger,
41
+ plugins
42
+ }),
43
+ updateUser: createUpdateUserEvent({
44
+ authConfig,
45
+ logger,
46
+ plugins
47
+ })
48
+ };
49
+ const session = createSessionEvent({
34
50
  authConfig,
35
51
  plugins
36
52
  });
37
53
  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
54
  return events;
54
55
  }
55
56
  export default createEvents;
@@ -13,14 +13,22 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import createEventPlugins from './createEventPlugins.js';
16
- function createLinkAccountEvent(context, { authConfig , plugins }) {
16
+ function createLinkAccountEvent({ authConfig, logger, plugins }) {
17
17
  const linkAccountPlugins = createEventPlugins({
18
18
  authConfig,
19
19
  plugins,
20
20
  type: 'linkAccount'
21
21
  });
22
- if (linkAccountPlugins.length === 0) return undefined;
23
- async function linkAccountEvent({ account , profile , user }) {
22
+ async function linkAccountEvent({ account, profile, user }) {
23
+ logger.info({
24
+ event: 'auth_link_account',
25
+ user: {
26
+ id: user.id,
27
+ roles: user.roles,
28
+ sub: user.sub,
29
+ session_id: user.session_id
30
+ }
31
+ });
24
32
  for (const plugin of linkAccountPlugins){
25
33
  await plugin.fn({
26
34
  properties: plugin.properties ?? {},
@@ -13,14 +13,14 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import createEventPlugins from './createEventPlugins.js';
16
- function createSessionEvent(context, { authConfig , plugins }) {
16
+ function createSessionEvent({ authConfig, plugins }) {
17
17
  const sessionPlugins = createEventPlugins({
18
18
  authConfig,
19
19
  plugins,
20
20
  type: 'session'
21
21
  });
22
22
  if (sessionPlugins.length === 0) return undefined;
23
- async function sessionEvent({ session , token }) {
23
+ async function sessionEvent({ session, token }) {
24
24
  for (const plugin of sessionPlugins){
25
25
  await plugin.fn({
26
26
  properties: plugin.properties ?? {},
@@ -13,14 +13,24 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import createEventPlugins from './createEventPlugins.js';
16
- function createSignInEvent(context, { authConfig , plugins }) {
16
+ function createSignInEvent({ authConfig, logger, plugins }) {
17
17
  const signInPlugins = createEventPlugins({
18
18
  authConfig,
19
19
  plugins,
20
20
  type: 'signIn'
21
21
  });
22
- if (signInPlugins.length === 0) return undefined;
23
- async function signInEvent({ account , isNewUser , profile , user }) {
22
+ async function signInEvent({ account, isNewUser, profile, user }) {
23
+ logger.info({
24
+ event: 'auth_sign_in',
25
+ isNewUser,
26
+ provider: account?.provider,
27
+ user: {
28
+ id: user.id,
29
+ roles: user.roles,
30
+ sub: user.sub,
31
+ session_id: user.session_id
32
+ }
33
+ });
24
34
  for (const plugin of signInPlugins){
25
35
  await plugin.fn({
26
36
  properties: plugin.properties ?? {},
@@ -13,15 +13,24 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import createEventPlugins from './createEventPlugins.js';
16
- function createSignOutEvent(context, { authConfig , plugins }) {
17
- const signInPlugins = createEventPlugins({
16
+ function createSignOutEvent({ authConfig, logger, plugins }) {
17
+ const signOutPlugins = createEventPlugins({
18
18
  authConfig,
19
19
  plugins,
20
20
  type: 'signOut'
21
21
  });
22
- if (signInPlugins.length === 0) return undefined;
23
- async function signInEvent({ session , token }) {
24
- for (const plugin of signInPlugins){
22
+ async function signOutEvent({ session, token }) {
23
+ const user = token?.user ?? session?.user;
24
+ logger.info({
25
+ event: 'auth_sign_out',
26
+ user: {
27
+ id: user.id,
28
+ roles: user.roles,
29
+ sub: user.sub,
30
+ session_id: user.session_id
31
+ }
32
+ });
33
+ for (const plugin of signOutPlugins){
25
34
  await plugin.fn({
26
35
  properties: plugin.properties ?? {},
27
36
  session,
@@ -29,6 +38,6 @@ function createSignOutEvent(context, { authConfig , plugins }) {
29
38
  });
30
39
  }
31
40
  }
32
- return signInEvent;
41
+ return signOutEvent;
33
42
  }
34
43
  export default createSignOutEvent;
@@ -13,14 +13,22 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import createEventPlugins from './createEventPlugins.js';
16
- function createUpdateUserEvent(context, { authConfig , plugins }) {
16
+ function createUpdateUserEvent({ authConfig, logger, plugins }) {
17
17
  const updateUserPlugins = createEventPlugins({
18
18
  authConfig,
19
19
  plugins,
20
20
  type: 'updateUser'
21
21
  });
22
- if (updateUserPlugins.length === 0) return undefined;
23
- async function updateUserEvent({ user }) {
22
+ async function updateUserEvent({ user }) {
23
+ logger.info({
24
+ event: 'auth_update_user',
25
+ user: {
26
+ id: user.id,
27
+ roles: user.roles,
28
+ sub: user.sub,
29
+ session_id: user.session_id
30
+ }
31
+ });
24
32
  for (const plugin of updateUserPlugins){
25
33
  await plugin.fn({
26
34
  properties: plugin.properties ?? {},
@@ -12,20 +12,18 @@
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 { NodeParser } from '@lowdefy/operators';
16
- import { getSecretsFromEnv } from '@lowdefy/node-utils';
15
+ */ import { ServerParser } from '@lowdefy/operators';
17
16
  import { _secret } from '@lowdefy/operators-js/operators/server';
18
17
  import createAdapter from './createAdapter.js';
19
18
  import createCallbacks from './callbacks/createCallbacks.js';
20
19
  import createEvents from './events/createEvents.js';
20
+ import createLogger from './createLogger.js';
21
21
  import createProviders from './createProviders.js';
22
22
  const nextAuthConfig = {};
23
23
  let initialized = false;
24
- function getNextAuthConfig(context, { authJson , plugins }) {
24
+ function getNextAuthConfig({ authJson, logger, plugins, secrets }) {
25
25
  if (initialized) return nextAuthConfig;
26
- const secrets = getSecretsFromEnv();
27
- // TODO: Add logger
28
- const operatorsParser = new NodeParser({
26
+ const operatorsParser = new ServerParser({
29
27
  operators: {
30
28
  _secret
31
29
  },
@@ -33,32 +31,38 @@ function getNextAuthConfig(context, { authJson , plugins }) {
33
31
  secrets,
34
32
  user: {}
35
33
  });
36
- const { output: authConfig , errors: operatorErrors } = operatorsParser.parse({
34
+ const { output: authConfig, errors: operatorErrors } = operatorsParser.parse({
37
35
  input: authJson,
38
36
  location: 'auth'
39
37
  });
40
38
  if (operatorErrors.length > 0) {
41
39
  throw new Error(operatorErrors[0]);
42
40
  }
43
- nextAuthConfig.adapter = createAdapter(context, {
41
+ nextAuthConfig.adapter = createAdapter({
44
42
  authConfig,
45
43
  plugins
46
44
  });
47
- nextAuthConfig.callbacks = createCallbacks(context, {
45
+ nextAuthConfig.callbacks = createCallbacks({
48
46
  authConfig,
47
+ logger,
49
48
  plugins
50
49
  });
51
- nextAuthConfig.events = createEvents(context, {
50
+ nextAuthConfig.events = createEvents({
52
51
  authConfig,
52
+ logger,
53
53
  plugins
54
54
  });
55
- nextAuthConfig.providers = createProviders(context, {
55
+ nextAuthConfig.logger = createLogger({
56
+ logger
57
+ });
58
+ nextAuthConfig.providers = createProviders({
56
59
  authConfig,
57
60
  plugins
58
61
  });
62
+ nextAuthConfig.debug = authConfig.debug ?? logger?.isLevelEnabled('debug') === true;
63
+ nextAuthConfig.pages = authConfig.authPages;
59
64
  nextAuthConfig.session = authConfig.session;
60
65
  nextAuthConfig.theme = authConfig.theme;
61
- nextAuthConfig.pages = authConfig.authPages;
62
66
  nextAuthConfig.cookies = authConfig?.advanced?.cookies;
63
67
  initialized = true;
64
68
  return nextAuthConfig;
@@ -12,11 +12,11 @@
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
- */ async function getPageConfig({ authorize , readConfigFile }, { pageId }) {
15
+ */ async function getPageConfig({ authorize, readConfigFile }, { pageId }) {
16
16
  const pageConfig = await readConfigFile(`pages/${pageId}/${pageId}.json`);
17
17
  if (pageConfig && authorize(pageConfig)) {
18
18
  // eslint-disable-next-line no-unused-vars
19
- const { auth , ...rest } = pageConfig;
19
+ const { auth, ...rest } = pageConfig;
20
20
  return {
21
21
  ...rest
22
22
  };
@@ -13,16 +13,20 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { ConfigurationError } from '../../context/errors.js';
16
- function authorizeRequest({ authorize , logger }, { requestConfig }) {
16
+ function authorizeRequest({ authorize, logger }, { requestConfig }) {
17
17
  if (!authorize(requestConfig)) {
18
- logger.warn({
19
- authorized: false
20
- }, 'Unauthorized Request');
18
+ logger.debug({
19
+ event: 'debug_request_authorize',
20
+ authorized: false,
21
+ auth_config: requestConfig.auth
22
+ });
21
23
  // Throw does not exist error to avoid leaking information that request exists to unauthorized users
22
24
  throw new ConfigurationError(`Request "${requestConfig.requestId}" does not exist.`);
23
25
  }
24
26
  logger.debug({
25
- authorized: true
26
- }, 'Authorize Request');
27
+ event: 'debug_request_authorize',
28
+ authorized: true,
29
+ auth_config: requestConfig.auth
30
+ });
27
31
  }
28
32
  export default authorizeRequest;
@@ -23,17 +23,15 @@ 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, { blockId , pageId , payload , requestId }) {
27
- const { logger } = context;
26
+ async function callRequest(context, { blockId, pageId, payload, requestId }) {
27
+ const { logger } = context;
28
28
  logger.debug({
29
- route: 'request',
30
- params: {
31
- blockId,
32
- pageId,
33
- payload,
34
- requestId
35
- }
36
- }, 'Started request');
29
+ event: 'debug_request',
30
+ blockId,
31
+ pageId,
32
+ payload,
33
+ requestId
34
+ });
37
35
  const requestConfig = await getRequestConfig(context, {
38
36
  pageId,
39
37
  requestId
@@ -52,7 +50,7 @@ async function callRequest(context, { blockId , pageId , payload , requestId })
52
50
  requestConfig
53
51
  });
54
52
  const deserializedPayload = serializer.deserialize(payload);
55
- const { connectionProperties , requestProperties } = evaluateOperators(context, {
53
+ const { connectionProperties, requestProperties } = evaluateOperators(context, {
56
54
  connectionConfig,
57
55
  payload: deserializedPayload,
58
56
  requestConfig
@@ -13,7 +13,7 @@
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 }, { blockId , connectionProperties , payload , requestConfig , requestProperties , requestResolver }) {
16
+ async function callRequestResolver({ logger }, { blockId, connectionProperties, payload, requestConfig, requestProperties, requestResolver }) {
17
17
  try {
18
18
  const response = await requestResolver({
19
19
  blockId,
@@ -13,7 +13,7 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { ConfigurationError } from '../../context/errors.js';
16
- function checkConnectionRead({ logger }, { connectionConfig , connectionProperties , requestConfig , requestResolver }) {
16
+ function checkConnectionRead({ logger }, { connectionConfig, connectionProperties, requestConfig, requestResolver }) {
17
17
  if (requestResolver.meta.checkRead && connectionProperties.read === false) {
18
18
  const err = new ConfigurationError(`Connection "${connectionConfig.connectionId}" does not allow reads.`);
19
19
  logger.debug({
@@ -13,7 +13,7 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { ConfigurationError } from '../../context/errors.js';
16
- function checkConnectionWrite({ logger }, { connectionConfig , connectionProperties , requestConfig , requestResolver }) {
16
+ function checkConnectionWrite({ logger }, { connectionConfig, connectionProperties, requestConfig, requestResolver }) {
17
17
  if (requestResolver.meta.checkWrite && connectionProperties.write !== true) {
18
18
  const err = new ConfigurationError(`Connection "${connectionConfig.connectionId}" does not allow writes.`);
19
19
  logger.debug({
@@ -12,23 +12,23 @@
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 { NodeParser } from '@lowdefy/operators';
15
+ */ import { ServerParser } from '@lowdefy/operators';
16
16
  import { RequestError } from '../../context/errors.js';
17
- function evaluateOperators({ operators , secrets , user }, { connectionConfig , payload , requestConfig }) {
18
- const operatorsParser = new NodeParser({
17
+ function evaluateOperators({ operators, secrets, session }, { connectionConfig, payload, requestConfig }) {
18
+ const operatorsParser = new ServerParser({
19
19
  operators,
20
20
  payload,
21
21
  secrets,
22
- user
22
+ user: session?.user
23
23
  });
24
- const { output: connectionProperties , errors: connectionErrors } = operatorsParser.parse({
24
+ const { output: connectionProperties, errors: connectionErrors } = operatorsParser.parse({
25
25
  input: connectionConfig.properties || {},
26
26
  location: connectionConfig.connectionId
27
27
  });
28
28
  if (connectionErrors.length > 0) {
29
29
  throw new RequestError(connectionErrors[0]);
30
30
  }
31
- const { output: requestProperties , errors: requestErrors } = operatorsParser.parse({
31
+ const { output: requestProperties, errors: requestErrors } = operatorsParser.parse({
32
32
  input: requestConfig.properties || {},
33
33
  location: requestConfig.requestId
34
34
  });
@@ -13,7 +13,7 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { ConfigurationError } from '../../context/errors.js';
16
- function getConnection({ connections , logger }, { connectionConfig }) {
16
+ function getConnection({ connections, logger }, { connectionConfig }) {
17
17
  const connection = connections[connectionConfig.type];
18
18
  if (!connection) {
19
19
  const err = new ConfigurationError(`Connection type "${connectionConfig.type}" can not be found.`);
@@ -13,8 +13,8 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { ConfigurationError } from '../../context/errors.js';
16
- async function getConnectionConfig({ logger , readConfigFile }, { requestConfig }) {
17
- const { connectionId , requestId } = requestConfig;
16
+ async function getConnectionConfig({ logger, readConfigFile }, { requestConfig }) {
17
+ const { connectionId, requestId } = requestConfig;
18
18
  let err;
19
19
  if (!connectionId) {
20
20
  err = new ConfigurationError(`Request "${requestId}" does not specify a connection.`);
@@ -13,7 +13,7 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { ConfigurationError } from '../../context/errors.js';
16
- async function getRequestConfig({ logger , readConfigFile }, { pageId , requestId }) {
16
+ async function getRequestConfig({ logger, readConfigFile }, { pageId, requestId }) {
17
17
  const request = await readConfigFile(`pages/${pageId}/requests/${requestId}.json`);
18
18
  if (!request) {
19
19
  const err = new ConfigurationError(`Request "${requestId}" does not exist.`);
@@ -13,7 +13,7 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { ConfigurationError } from '../../context/errors.js';
16
- function getRequestResolver({ logger }, { connection , requestConfig }) {
16
+ function getRequestResolver({ logger }, { connection, requestConfig }) {
17
17
  const requestResolver = connection.requests[requestConfig.type];
18
18
  if (!requestResolver) {
19
19
  const err = new ConfigurationError(`Request type "${requestConfig.type}" can not be found.`);
@@ -14,7 +14,7 @@
14
14
  limitations under the License.
15
15
  */ import { validate } from '@lowdefy/ajv';
16
16
  import { ConfigurationError } from '../../context/errors.js';
17
- function validateSchemas({ logger }, { connection , connectionProperties , requestConfig , requestResolver , requestProperties }) {
17
+ function validateSchemas({ logger }, { connection, connectionProperties, requestConfig, requestResolver, requestProperties }) {
18
18
  try {
19
19
  validate({
20
20
  schema: connection.schema,
@@ -12,7 +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
- */ async function getLowdefyGlobal({ readConfigFile }) {
15
+ */ async function getLowdefyGlobal({ readConfigFile }) {
16
16
  const lowdefyGlobal = await readConfigFile('global.json');
17
17
  return lowdefyGlobal || {};
18
18
  }
@@ -15,7 +15,7 @@
15
15
  */ import getHomeAndMenus from './getHomeAndMenus.js';
16
16
  import getLowdefyGlobal from './getLowdefyGlobal.js';
17
17
  async function getRootConfig(context) {
18
- const [lowdefyGlobal, { home , menus }] = await Promise.all([
18
+ const [lowdefyGlobal, { home, menus }] = await Promise.all([
19
19
  getLowdefyGlobal(context),
20
20
  getHomeAndMenus(context)
21
21
  ]);
@@ -13,8 +13,8 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { get } from '@lowdefy/helpers';
16
- function filterMenuList(context, { menuList }) {
17
- const { authorize } = context;
16
+ function filterMenuList(context, { menuList }) {
17
+ const { authorize } = context;
18
18
  return menuList.map((item)=>{
19
19
  if (item.type === 'MenuLink') {
20
20
  if (authorize(item)) {
@@ -14,7 +14,7 @@
14
14
  limitations under the License.
15
15
  */ import { get } from '@lowdefy/helpers';
16
16
  import filterMenuList from './filterMenuList.js';
17
- function filterMenus(context, { menus }) {
17
+ function filterMenus(context, { menus }) {
18
18
  return menus.map((menu)=>{
19
19
  return {
20
20
  ...menu,
@@ -13,14 +13,14 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import createAuthorize from '../context/createAuthorize.js';
16
- function testContext({ config ={} , connections ={} , headers ={} , host ='host' , logger ={
16
+ function testContext({ config = {}, connections = {}, headers = {}, logger = {
17
17
  debug: ()=>{},
18
18
  error: ()=>{},
19
19
  info: ()=>{},
20
20
  warn: ()=>{}
21
- } , operators ={
21
+ }, operators = {
22
22
  _test: ()=>'test'
23
- } , readConfigFile , secrets ={} , setHeader , session , protocol ='https' } = {}) {
23
+ }, readConfigFile, secrets = {}, session } = {}) {
24
24
  return {
25
25
  authorize: createAuthorize({
26
26
  session
@@ -28,14 +28,11 @@ function testContext({ config ={} , connections ={} , headers ={} , host ='host'
28
28
  config,
29
29
  connections,
30
30
  headers,
31
- host,
32
31
  logger,
33
32
  operators,
34
- protocol,
35
33
  readConfigFile,
36
34
  secrets,
37
- setHeader,
38
- user: session?.user
35
+ session
39
36
  };
40
37
  }
41
38
  export default testContext;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowdefy/api",
3
- "version": "4.0.0-rc.1",
3
+ "version": "4.0.0-rc.10",
4
4
  "license": "Apache-2.0",
5
5
  "description": "",
6
6
  "homepage": "https://lowdefy.com",
@@ -40,22 +40,22 @@
40
40
  "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
41
41
  },
42
42
  "dependencies": {
43
- "@lowdefy/ajv": "4.0.0-rc.1",
44
- "@lowdefy/helpers": "4.0.0-rc.1",
45
- "@lowdefy/node-utils": "4.0.0-rc.1",
46
- "@lowdefy/nunjucks": "4.0.0-rc.1",
47
- "@lowdefy/operators": "4.0.0-rc.1",
48
- "@lowdefy/operators-js": "4.0.0-rc.1"
43
+ "@lowdefy/ajv": "4.0.0-rc.10",
44
+ "@lowdefy/helpers": "4.0.0-rc.10",
45
+ "@lowdefy/node-utils": "4.0.0-rc.10",
46
+ "@lowdefy/nunjucks": "4.0.0-rc.10",
47
+ "@lowdefy/operators": "4.0.0-rc.10",
48
+ "@lowdefy/operators-js": "4.0.0-rc.10"
49
49
  },
50
50
  "devDependencies": {
51
- "@jest/globals": "28.1.0",
52
- "@swc/cli": "0.1.59",
53
- "@swc/core": "1.3.24",
54
- "@swc/jest": "0.2.24",
55
- "jest": "28.1.0"
51
+ "@jest/globals": "28.1.3",
52
+ "@swc/cli": "0.1.62",
53
+ "@swc/core": "1.3.70",
54
+ "@swc/jest": "0.2.27",
55
+ "jest": "28.1.3"
56
56
  },
57
57
  "publishConfig": {
58
58
  "access": "public"
59
59
  },
60
- "gitHead": "ecc4f16c19eede929eda177db524cf13a8053379"
60
+ "gitHead": "537af074f27770e32da9da8d48490f2eda94b406"
61
61
  }