soul-cli 0.7.5 → 0.7.7

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/.env.sample CHANGED
@@ -6,8 +6,8 @@ CORS_ORIGIN_WHITELIST=http://localhost:3000,http://127.0.0.1:3000
6
6
  AUTH=false
7
7
 
8
8
  RATE_LIMIT_ENABLED=false
9
- RATE_LIMIT_WINDOW_MS=1000
10
- RATE_LIMIT_MAX_REQUESTS=10
9
+ RATE_LIMIT_WINDOW=1000
10
+ RATE_LIMIT_MAX=10
11
11
 
12
12
  DB=foobar.db
13
13
 
package/README.md CHANGED
@@ -36,12 +36,12 @@ Options:
36
36
  -c, --cors CORS whitelist origins [string]
37
37
  -a, --auth Enable authentication and authorization [boolean]
38
38
 
39
- -iuu, --initialuserusername Initial user username [string]
40
- -iup, --initialuserpassword Initial user password [string]
39
+ --iuu, --initialuserusername Initial user username [string]
40
+ --iup, --initialuserpassword Initial user password [string]
41
41
 
42
- -ts, --tokensecret Token Secret [string]
43
- -atet, --accesstokenexpirationtime Access Token Expiration Time (Default: 5H) [string]
44
- -rtet, --refreshtokenexpirationtime Refresh Token Expiration Time (Default: 1D) [string]
42
+ --ts, --tokensecret Token Secret [string]
43
+ --atet, --accesstokenexpirationtime Access Token Expiration Time (Default: 5H) [string]
44
+ --rtet, --refreshtokenexpirationtime Refresh Token Expiration Time (Default: 1D) [string]
45
45
  -S, --studio Start Soul Studio in parallel
46
46
  --help Show help
47
47
 
@@ -63,7 +63,7 @@ Run the Soul command with the necessary parameters:
63
63
 
64
64
  ```
65
65
 
66
- soul --d foobar.db -a -ts <your_jwt_secret_value> -atet=4H -rtet=3D -iuu=john -iup=<your_password>
66
+ soul -d foobar.db -a --ts=<your_jwt_secret_value> --atet=4H --rtet=3D --iuu=john --iup=<your_password>
67
67
 
68
68
  ```
69
69
 
@@ -72,30 +72,30 @@ Note: When configuring your JWT Secret, it is recommended to use a long string v
72
72
  In this example:
73
73
 
74
74
  The `-a` flag instructs Soul to run in auth mode.
75
- The `-ts` flag allows you to pass a JWT secret value for the `access and refresh tokens` generation and verification. Replace <your_jwt_secret_value> with your desired secret value.
76
- The `-atet` flag sets the JWT expiration time for the access token. In this case, it is set to four hours (4H), meaning the token will expire after 4 hours.
77
- The `-rtet` flag sets the JWT expiration time for the refresh token. In this case, it is set to three days (3D), meaning the token will expire after 3 days.
78
- The `-iuu` flag is used to pass a username for the initial user
79
- The `-iup` flag is used to pass a password for the initial user
75
+ The `--ts` flag allows you to pass a JWT secret value for the `access and refresh tokens` generation and verification. Replace <your_jwt_secret_value> with your desired secret value.
76
+ The `--atet` flag sets the JWT expiration time for the access token. In this case, it is set to four hours (4H), meaning the token will expire after 4 hours.
77
+ The `--rtet` flag sets the JWT expiration time for the refresh token. In this case, it is set to three days (3D), meaning the token will expire after 3 days.
78
+ The `--iuu` flag is used to pass a username for the initial user
79
+ The `--iup` flag is used to pass a password for the initial user
80
80
 
81
- Here are some example values for the `-atet` and `rtet` flags
81
+ Here are some example values for the `atet` and `rtet` flags
82
82
 
83
83
  - 60M: Represents a duration of 60 minutes.
84
84
  - 5H: Represents a duration of 5 hours.
85
85
  - 1D: Represents a duration of 1 day.
86
86
 
87
- NOTE: It is crucial to securely store a copy of the `-ts`(`Token Secret`) value used in Soul. Once you pass this values, make sure to keep a backup because you will need it every time you restart Soul. Losing this secret values can result in a situation where all of your users are blocked from accessing Soul.
87
+ NOTE: It is crucial to securely store a copy of the `--ts`(`Token Secret`) value used in Soul. Once you pass this values, make sure to keep a backup because you will need it every time you restart Soul. Losing this secret values can result in a situation where all of your users are blocked from accessing Soul.
88
88
 
89
89
  ### 3. Updating Super Users
90
90
 
91
91
  To modify a superuser information in a database, you can utilize the `updatesuperuser` command. This command allows you to change a superuser's `password` or upgrade/downgrade a normal user to a `superuser`. Below is an example of how to use it:
92
92
 
93
93
  ```
94
- soul --d foobar.db updatesuperuser --id=1 password=<new_password_for_the_user> // Update the password for the superuser with ID 1
94
+ soul -d foobar.db updatesuperuser --id=1 password=<new_password_for_the_user> // Update the password for the superuser with ID 1
95
95
 
96
- soul --d foobar.db updatesuperuser --id=1 --is_superuser=true // Upgrade the user with ID 1 to a superuser
96
+ soul -d foobar.db updatesuperuser --id=1 --is_superuser=true // Upgrade the user with ID 1 to a superuser
97
97
 
98
- soul --d foobar.db updatesuperuser --id=1 --is_superuser=false // Revoke the superuser role from the superuser with ID 1
98
+ soul -d foobar.db updatesuperuser --id=1 --is_superuser=false // Revoke the superuser role from the superuser with ID 1
99
99
  ```
100
100
 
101
101
  ## Documentation
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "soul-cli",
3
- "version": "0.7.5",
3
+ "version": "0.7.7",
4
4
  "description": "A SQLite REST and Realtime server",
5
5
  "main": "src/server.js",
6
6
  "bin": {
package/src/cli.js CHANGED
@@ -50,28 +50,24 @@ if (process.env.NO_CLI !== 'true') {
50
50
  alias: 'auth',
51
51
  describe: 'Enable authentication and authorization',
52
52
  type: 'boolean',
53
- default: false,
54
53
  demandOption: false,
55
54
  })
56
55
  .options('ts', {
57
56
  alias: 'tokensecret',
58
57
  describe: 'JWT secret for the access and refresh tokens',
59
58
  type: 'string',
60
- default: null,
61
59
  demandOption: false,
62
60
  })
63
61
  .options('atet', {
64
62
  alias: 'accesstokenexpirationtime',
65
63
  describe: 'JWT expiration time for access token',
66
64
  type: 'string',
67
- default: '5H',
68
65
  demandOption: false,
69
66
  })
70
67
  .options('rtet', {
71
68
  alias: 'refreshtokenexpirationtime',
72
69
  describe: 'JWT expiration time for refresh token',
73
70
  type: 'string',
74
- default: '3D',
75
71
  demandOption: false,
76
72
  })
77
73
  .options('iuu', {
@@ -20,7 +20,7 @@ const envVarsSchema = Joi.object()
20
20
  VERBOSE: Joi.string().valid('console', null).default(null),
21
21
 
22
22
  CORS_ORIGIN_WHITELIST: Joi.string().default('*'),
23
- AUTH: Joi.boolean().default(false),
23
+ AUTH: Joi.boolean(),
24
24
 
25
25
  RATE_LIMIT_ENABLED: Joi.boolean().default(false),
26
26
  RATE_LIMIT_WINDOW_MS: Joi.number().positive().default(1000),
@@ -33,9 +33,9 @@ const envVarsSchema = Joi.object()
33
33
  INITIAL_USER_USERNAME: Joi.string(),
34
34
  INITIAL_USER_PASSWORD: Joi.string(),
35
35
 
36
- TOKEN_SECRET: Joi.string().default(null),
37
- ACCESS_TOKEN_EXPIRATION_TIME: Joi.string().default('5H'),
38
- REFRESH_TOKEN_EXPIRATION_TIME: Joi.string().default('3D'),
36
+ TOKEN_SECRET: Joi.string(),
37
+ ACCESS_TOKEN_EXPIRATION_TIME: Joi.string(),
38
+ REFRESH_TOKEN_EXPIRATION_TIME: Joi.string(),
39
39
  })
40
40
  .unknown();
41
41
 
@@ -113,12 +113,16 @@ module.exports = {
113
113
  envVars.CORS_ORIGIN_WHITELIST?.split(',') || ['*'],
114
114
  },
115
115
 
116
- auth: argv.auth || envVars.AUTH,
117
- tokenSecret: argv.tokensecret || envVars.TOKEN_SECRET,
116
+ auth: argv.auth || envVars.AUTH || false,
117
+ tokenSecret: argv.tokensecret || envVars.TOKEN_SECRET || null,
118
118
  accessTokenExpirationTime:
119
- argv.accesstokenexpirationtime || envVars.ACCESS_TOKEN_EXPIRATION_TIME,
119
+ argv.accesstokenexpirationtime ||
120
+ envVars.ACCESS_TOKEN_EXPIRATION_TIME ||
121
+ '5H',
120
122
  refreshTokenExpirationTime:
121
- argv.refreshtokenexpirationtime || envVars.REFRESH_TOKEN_EXPIRATION_TIME,
123
+ argv.refreshtokenexpirationtime ||
124
+ envVars.REFRESH_TOKEN_EXPIRATION_TIME ||
125
+ '3D',
122
126
 
123
127
  initialUserUsername:
124
128
  argv.initialuserusername || envVars.INITIAL_USER_USERNAME,
@@ -13,4 +13,12 @@ const isUsernameTaken = (username) => {
13
13
  return users.length > 0;
14
14
  };
15
15
 
16
- module.exports = { isUsernameTaken };
16
+ const checkAuthConfigs = ({ auth, tokenSecret }) => {
17
+ if (auth && !tokenSecret) {
18
+ throw new Error(
19
+ 'You need to provide a token secret either from the CLI or from your environment variables',
20
+ );
21
+ }
22
+ };
23
+
24
+ module.exports = { isUsernameTaken, checkAuthConfigs };
@@ -1,5 +1,6 @@
1
1
  const users = require('./user');
2
2
  const token = require('./token');
3
3
  const tables = require('./tables');
4
+ const { checkAuthConfigs } = require('./common');
4
5
 
5
- module.exports = { ...users, ...token, ...tables };
6
+ module.exports = { ...users, ...token, ...tables, checkAuthConfigs };
@@ -59,12 +59,18 @@ const obtainAccessToken = async (req, res) => {
59
59
 
60
60
  // if the user is not a superuser get the role and its permission from the DB
61
61
  if (!toBoolean(user.is_superuser)) {
62
- const roleData = getUsersRoleAndPermission({
63
- userId: user.id,
64
- res,
65
- });
66
-
67
- roleIds = roleData.roleIds;
62
+ try {
63
+ const roleData = getUsersRoleAndPermission({
64
+ userId: user.id,
65
+ res,
66
+ });
67
+
68
+ roleIds = roleData.roleIds;
69
+ } catch (err) {
70
+ return res
71
+ .status(401)
72
+ .send({ message: errorMessage.ROLE_NOT_FOUND_ERROR });
73
+ }
68
74
  }
69
75
 
70
76
  const payload = {
@@ -162,12 +168,17 @@ const refreshAccessToken = async (req, res) => {
162
168
 
163
169
  // if the user is not a superuser get the role and its permission from the DB
164
170
  if (!toBoolean(user.is_superuser)) {
165
- const roleData = getUsersRoleAndPermission({
166
- userId: user.id,
167
- res,
168
- });
169
-
170
- roleIds = roleData.roleIds;
171
+ try {
172
+ const roleData = getUsersRoleAndPermission({
173
+ userId: user.id,
174
+ });
175
+
176
+ roleIds = roleData.roleIds;
177
+ } catch (err) {
178
+ return res
179
+ .status(401)
180
+ .send({ message: errorMessage.ROLE_NOT_FOUND_ERROR });
181
+ }
171
182
  }
172
183
 
173
184
  const newPayload = {
@@ -271,11 +282,10 @@ const removeRevokedRefreshTokens = () => {
271
282
  });
272
283
  };
273
284
 
274
- const getUsersRoleAndPermission = ({ userId, res }) => {
285
+ const getUsersRoleAndPermission = ({ userId }) => {
275
286
  const userRoles = authService.getUserRoleByUserId({ userId });
276
287
 
277
- if (userRoles <= 0) {
278
- res.status(401).send({ message: errorMessage.ROLE_NOT_FOUND_ERROR });
288
+ if (userRoles.length <= 0) {
279
289
  throw new Error(errorMessage.ROLE_NOT_FOUND_ERROR);
280
290
  }
281
291
 
package/src/index.js CHANGED
@@ -23,11 +23,11 @@ const {
23
23
  createDefaultTables,
24
24
  createInitialUser,
25
25
  removeRevokedRefreshTokens,
26
+ checkAuthConfigs,
26
27
  } = require('./controllers/auth');
27
28
 
28
29
  const { runCLICommands } = require('./commands');
29
30
  const { authConstants } = require('./constants');
30
-
31
31
  const app = express();
32
32
  app.get('/health', (req, res) => {
33
33
  res.send('OK');
@@ -85,6 +85,9 @@ if (config.rateLimit.enabled) {
85
85
  app.use(limiter);
86
86
  }
87
87
 
88
+ // If Auth mode is activated but if the tokenSecret value is undefined then throw an error
89
+ checkAuthConfigs({ auth: config.auth, tokenSecret: config.tokenSecret });
90
+
88
91
  // If Auth mode is activated then create auth tables in the DB & create a super user if there are no users in the DB
89
92
  if (config.auth) {
90
93
  createDefaultTables();