finish /plugin/ssh/update_passwd/

This commit is contained in:
jysperm
2014-05-16 22:52:20 +08:00
parent 585c3fb2ec
commit 5483ac3fcc
6 changed files with 44 additions and 25 deletions

View File

@@ -1,12 +0,0 @@
## Plugin API
### POST /plugin/action/
Request:
{
"plugin": "ssh",
"action": "update_passwd"
}
Response.

View File

@@ -18,7 +18,7 @@ module.exports =
]
plugin:
availablePlugin: ['shadowsocks']
availablePlugin: ['ssh']
plans:
all:

View File

@@ -47,7 +47,7 @@ exports.runWebServer = ->
api.bind app
plugin = require './plugin'
plugin.loadPlugins()
plugin.loadPlugins app
app.listen config.web.port

View File

@@ -6,6 +6,10 @@ config = require './config'
exports.get = (name) ->
return require path.join(__dirname, "../plugin/#{name}")
exports.loadPlugins = ->
exports.loadPlugins = (app) ->
for name in config.plugin.availablePlugin
i18n.loadPlugin path.join(__dirname, "../plugin/#{name}/locale"), name
p = exports.get name
app.use ('/plugin/' + name), p.action

16
plugin/ssh/API.md Normal file
View File

@@ -0,0 +1,16 @@
## SSH Plugin API
### POST /plugin/ssh/update_passwd/
Request:
{
"passwd": "123123"
}
No Response.
Exception:
* invalid_passwd
* not_in_service

View File

@@ -1,13 +1,24 @@
child_process = require 'child_process'
express = require 'express'
module.exports =
update_passwd:
mode: 'passwd'
callback: ->
mAccount = require '../../core/model/account'
killall:
mode: 'alert'
callback: ->
module.exports = exports = express.Router()
reset_permission:
mode: 'alert'
callback: ->
exports.use (req, res) ->
mAccount.authenticate req.token, (account) ->
unless account
return res.json 400, error: 'auth_failed'
unless 'ssh' in account.attribute.service
return res.json 400, error: 'not_in_service'
req.account = account
exports.post '/update_passwd/', (req, res) ->
unless req.body.passwd or not /^[A-Za-z0-9\-_]+$/.test req.body.passwd
return res.json 400, error: 'invalid_passwd'
child_process.exec "echo '#{req.account.username}:{#{req.body.passwd}}' | sudo chpasswd", (err, stdout, stderr) ->
throw err if err
res.json {}