1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00
node-red/red/api/editor/sshkeys.js

102 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

2017-12-07 15:11:24 +01:00
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
2018-04-24 16:01:49 +02:00
var apiUtils = require("../util");
2017-12-07 15:11:24 +01:00
var express = require("express");
2018-04-15 12:18:10 +02:00
var runtimeAPI;
2017-12-07 15:11:24 +01:00
function getUsername(userObj) {
var username = '__default';
2017-12-07 15:11:24 +01:00
if ( userObj && userObj.name ) {
username = userObj.name;
}
return username;
}
module.exports = {
2018-04-15 12:18:10 +02:00
init: function(_runtimeAPI) {
runtimeAPI = _runtimeAPI;
2017-12-07 15:11:24 +01:00
},
app: function() {
var app = express();
// List all SSH keys
2018-04-24 16:01:49 +02:00
app.get("/", function(req,res) {
2018-04-15 12:18:10 +02:00
var opts = {
user: req.user
}
runtimeAPI.settings.getUserKeys(opts).then(function(list) {
2017-12-07 15:11:24 +01:00
res.json({
keys: list
});
2018-04-15 12:18:10 +02:00
}).catch(function(err) {
apiUtils.rejectHandler(req,res,err);
2017-12-07 15:11:24 +01:00
});
});
// Get SSH key detail
2018-04-24 16:01:49 +02:00
app.get("/:id", function(req,res) {
2018-04-15 12:18:10 +02:00
var opts = {
user: req.user,
id: req.params.id
}
runtimeAPI.settings.getUserKey(opts).then(function(data) {
res.json({
publickey: data
});
}).catch(function(err) {
apiUtils.rejectHandler(req,res,err);
2017-12-07 15:11:24 +01:00
});
});
// Generate a SSH key
2018-04-24 16:01:49 +02:00
app.post("/", function(req,res) {
2018-04-15 12:18:10 +02:00
var opts = {
user: req.user,
id: req.params.id
2017-12-07 15:11:24 +01:00
}
2018-04-24 16:01:49 +02:00
// TODO: validate params
opts.name = req.body.name;
opts.password = req.body.password;
opts.comment = req.body.comment;
opts.size = req.body.size;
2018-04-15 12:18:10 +02:00
runtimeAPI.settings.generateUserKey(opts).then(function(name) {
res.json({
name: name
});
}).catch(function(err) {
apiUtils.rejectHandler(req,res,err);
});
2017-12-07 15:11:24 +01:00
});
// Delete a SSH key
2018-04-24 16:01:49 +02:00
app.delete("/:id", function(req,res) {
2018-04-15 12:18:10 +02:00
var opts = {
user: req.user,
id: req.params.id
}
2018-04-24 16:01:49 +02:00
runtimeAPI.settings.removeUserKey(opts).then(function(name) {
2017-12-07 15:11:24 +01:00
res.status(204).end();
2018-04-15 12:18:10 +02:00
}).catch(function(err) {
apiUtils.rejectHandler(req,res,err);
2017-12-07 15:11:24 +01:00
});
});
return app;
}
}