Browse Source

Added generation of random token

kanadeko 7 years ago
parent
commit
7b72c3e560
3 changed files with 40 additions and 9 deletions
  1. 0 3
      config.sample.js
  2. 38 1
      database/db.js
  3. 2 5
      lolisafe.js

+ 0 - 3
config.sample.js

@@ -9,9 +9,6 @@ module.exports = {
 	// Your base domain where the app is running. Remember to finish it with '/'
 	basedomain: 'https://i.kanacchi.moe/',
 
-	// Token to use on the api. Leave blank for public
-	TOKEN: 'YOURSUPERSECRETTOKEN',
-
 	// Port on which to run the server
 	port: 9999,
 

+ 38 - 1
database/db.js

@@ -1,5 +1,5 @@
 
-let init = function(db){
+let init = function(db, config){
 
 	// Create the tables we need to store galleries and files
 	db.schema.createTableIfNotExists('gallery', function (table) {
@@ -19,6 +19,43 @@ let init = function(db){
 		table.timestamps()
 	}).then(() => {})
 
+	db.schema.createTableIfNotExists('tokens', function (table) {
+		table.string('name')
+		table.string('value')
+		table.timestamps()
+	}).then(() => {
+
+		// == Generate a 1 time token == //
+		db.table('tokens').then((tokens) => {
+			if(tokens.length === 0){
+				
+				// This is the first launch of the app
+				let clientToken = require('randomstring').generate()
+				let adminToken = require('randomstring').generate()
+
+				db.table('tokens').insert(
+					[
+						{ 
+							name: 'client', 
+							value: clientToken 
+						},
+						{ 
+							name: 'admin', 
+							value: adminToken 
+						}
+					]
+				).then(() => {
+					console.log('Your client token is: ' + clientToken)
+					console.log('Your admin token is: ' + adminToken)
+					config.clientToken = clientToken
+					config.adminToken = adminToken
+				})
+
+			}
+		})
+
+	})
+
 }
 
 module.exports = init

+ 2 - 5
lolisafe.js

@@ -6,7 +6,7 @@ const db = require('knex')(config.database)
 const fs = require('fs')
 const safe = express()
 
-require('./database/db.js')(db)
+require('./database/db.js')(db, config)
 
 fs.existsSync('./' + config.uploads.folder) || fs.mkdirSync('./' + config.uploads.folder)
 fs.existsSync('./' + config.logsFolder) || fs.mkdirSync('./' + config.logsFolder)
@@ -32,7 +32,4 @@ safe.use(function (err, req, res, next) {
 	res.status(500).end()
 })
 
-safe.listen(config.port, () => console.log(`loli-safe started on port ${config.port}`))
-
-if(config.TOKEN !== '') console.log('Use the following token as the \'auth\' header in your requests to the API: ' + config.TOKEN)
-else console.log('Running lolisafe in public mode. No token required.')
+safe.listen(config.port, () => console.log(`loli-safe started on port ${config.port}`))