Added new API key and using API key config items to enable a public API

API key config items are implemented UI-side

Added ability to generate API keys through the settings

Switched getmp3s and getmp4s api calls to be GET requests rather than POST

Removed unused code from settings dialog
This commit is contained in:
Isaac Grynsztein
2020-04-10 14:46:36 -04:00
parent 02441ac846
commit 1e96e31053
9 changed files with 75 additions and 43 deletions

View File

@@ -1218,7 +1218,10 @@ const deleteFolderRecursive = function(folder_to_delete) {
};
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", getOrigin());
var client_origin = req.get('origin');
if (client_origin === getOrigin() || (req.headers.authorization && config_api.getConfigItem('ytdl_use_api_key') && req.headers.authorization === config_api.getConfigItem('ytdl_api_key'))) {
res.header("Access-Control-Allow-Origin", client_origin);
}
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
@@ -1584,7 +1587,7 @@ app.post('/api/fileStatusMp4', function(req, res) {
});
// gets all download mp3s
app.post('/api/getMp3s', function(req, res) {
app.get('/api/getMp3s', function(req, res) {
var mp3s = db.get('files.audio').value(); // getMp3s();
var playlists = db.get('playlists.audio').value();
@@ -1596,7 +1599,7 @@ app.post('/api/getMp3s', function(req, res) {
});
// gets all download mp4s
app.post('/api/getMp4s', function(req, res) {
app.get('/api/getMp4s', function(req, res) {
var mp4s = db.get('files.video').value(); // getMp4s();
var playlists = db.get('playlists.video').value();
@@ -2061,7 +2064,7 @@ app.post('/api/deleteFile', async (req, res) => {
} else if (type === 'video') {
deleteVideoFile(fileName);
}
res.send()
res.send({});
});
app.post('/api/downloadArchive', async (req, res) => {
@@ -2145,6 +2148,16 @@ app.post('/api/checkPin', async (req, res) => {
});
});
// API Key API calls
app.post('/api/generateNewAPIKey', function (req, res) {
const new_api_key = uuid();
config_api.setConfigItem('ytdl_api_key', new_api_key);
res.send({new_api_key: new_api_key});
});
// Streaming API calls
app.get('/api/video/:id', function(req , res){
var head;
let optionalParams = url_api.parse(req.url,true).query;

View File

@@ -24,6 +24,8 @@
"settings_pin_required": false
},
"API": {
"use_API_key": false,
"API_key": "",
"use_youtube_API": false,
"youtube_API_key": ""
},

View File

@@ -24,6 +24,8 @@
"settings_pin_required": false
},
"API": {
"use_API_key": false,
"API_key": "",
"use_youtube_API": false,
"youtube_API_key": ""
},

View File

@@ -163,6 +163,8 @@ DEFAULT_CONFIG = {
"settings_pin_required": false
},
"API": {
"use_API_key": false,
"API_key": "",
"use_youtube_API": false,
"youtube_API_key": ""
},

View File

@@ -68,6 +68,14 @@ let CONFIG_ITEMS = {
},
// API
'ytdl_use_api_key': {
'key': 'ytdl_use_api_key',
'path': 'YoutubeDLMaterial.API.use_API_key'
},
'ytdl_api_key': {
'key': 'ytdl_api_key',
'path': 'YoutubeDLMaterial.API.API_key'
},
'ytdl_use_youtube_api': {
'key': 'ytdl_use_youtube_api',
'path': 'YoutubeDLMaterial.API.use_youtube_API'