Skip to content

Commit

Permalink
Merge pull request #4 from kearfy/master
Browse files Browse the repository at this point in the history
Added PAM authentication
  • Loading branch information
A1Gard authored Jul 31, 2020
2 parents 2c22878 + dc0896e commit 9b13cb6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 19 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ you can do with this application with web GUI and without any command:
Requirement:

* nodejs
* libpam0g-dev (for PAM authentication)

## how use:

Expand All @@ -32,6 +33,7 @@ you can change config in first lines of `pm2panel.js`:

```javascript
const PORT = 3001;
const PAM_AUTH = true; // if set to true, USER and PASS won't be used
const USER = 'admin';
const PASS = 'admin';
const SESSTION_AGE = 10 * 60000; // 10 minutes
Expand All @@ -42,4 +44,3 @@ change port or user name and password
<img src="https://www.uplooder.net/img/image/15/fd8d1c8ed2ea1e09e558f423ff2925ae/login-pm2.png" />
<br /><br />
<img src="https://www.uplooder.net/img/image/10/f9f161252a89283a2f5aa85b2b1e1718/pm2index.png" />

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"express": "^4.16.2",
"express-session": "^1.15.6",
"fs": "0.0.1-security",
"node-linux-pam": "0.0.1",
"path": "^0.12.7"
}
}
64 changes: 46 additions & 18 deletions pm2panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// config panel
//##############################################################################
const PORT = 3001;
const PAM_AUTH = true; // if set to true, USER and PASS won't be used
const USER = 'admin';
const PASS = 'admin';
const SESSTION_AGE = 10 * 60000; // 10 minutes
Expand All @@ -16,6 +17,7 @@ const express = require('express');
const app = express();
const exec = require("child_process").exec;
const fs = require('fs');
const { pamAuthenticate, pamErrors } = require('node-linux-pam');

var session = require('express-session');

Expand Down Expand Up @@ -55,22 +57,48 @@ app.get('/login', function (req, res) {
});

app.post('/loginCheck', function (req, res) {
// check username and password
if (req.body.username === USER && req.body.passwd == PASS) {
// login process
req.session.islogin = true;
// redirect to panel
res.writeHead(302, {
'Location': '/'
// check if local or pam authentication is requested
if (PAM_AUTH) {
pamAuthenticate({
username: req.body.username,
password: req.body.passwd
}, (err, code) => {
if (!err) {
// login process
req.session.islogin = true;
// redirect to panel
res.writeHead(302, {
'Location': '/'
});
} else {
// user or password incorrect go back to login and logging PAM code if not 7 (invalid credentials)
if (code != 7) console.log('Unsuccessful PAM authentication, code: ' + code);
res.writeHead(302, {
'Location': '/login?err=' + (code == 7 ? 'invalid_credentials' : 'system')

});
}

res.end();
});
} else {
// user or password incrrect go back to login
res.writeHead(302, {
'Location': '/login'
// check username and password by local authentication
if (req.body.username === USER && req.body.passwd == PASS) {
// login process
req.session.islogin = true;
// redirect to panel
res.writeHead(302, {
'Location': '/'
});
} else {
// user or password incrrect go back to login
res.writeHead(302, {
'Location': '/login?err=invalid_credentials'

});
});
}
res.end();
}
res.end();
});


Expand Down Expand Up @@ -154,7 +182,7 @@ app.get('/restart', function (req, res) {
res.end();

} else {
// check id exits
// check id exits
if (req.query.id) {
// restart the process
exec("pm2 restart " + req.query.id, (error, stdout, stderr) => {
Expand Down Expand Up @@ -184,7 +212,7 @@ app.get('/start', function (req, res) {
res.end();

} else {
// check id exits
// check id exits
if (req.query.id) {
// start the process
exec("pm2 start " + req.query.id, (error, stdout, stderr) => {
Expand Down Expand Up @@ -214,7 +242,7 @@ app.get('/stop', function (req, res) {
res.end();

} else {
// check id exits
// check id exits
if (req.query.id) {
// stop the process
exec("pm2 stop " + req.query.id, (error, stdout, stderr) => {
Expand Down Expand Up @@ -244,7 +272,7 @@ app.get('/delete', function (req, res) {
res.end();

} else {
// check id exits
// check id exits
if (req.query.id) {
// delete the process
exec("pm2 delete " + req.query.id, (error, stdout, stderr) => {
Expand Down Expand Up @@ -392,7 +420,7 @@ app.get('/log', function (req, res) {
res.end();

} else {
// check id exits
// check id exits
if (req.query.id) {
// log of the process
var proc = require('child_process').spawn("pm2", ['log', req.query.id]);
Expand Down Expand Up @@ -422,4 +450,4 @@ app.get('/log', function (req, res) {

app.listen(PORT, function () {
console.log('pm2panel app listening on port ' + PORT + '! \n test: http://localhost:' + PORT);
});
});
14 changes: 14 additions & 0 deletions www/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ <h1>
Login to Pm2 panel
</h1>
<form method="post" action="/loginCheck">
<p class="err" style="display:none;color:red;margin:2em auto 1.5em auto;text-align:center;"></p>
<fieldset>
<label for="user">Username</label>
<input type="text" placeholder="Username" id="user" name="username">
Expand All @@ -28,5 +29,18 @@ <h1>
</form>

</div>

<script type="text/javascript">
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.get('err') != null) {
if (urlParams.get('err') == 'invalid_credentials') {
document.querySelector('#login form p.err').innerText = 'Invalid credentials where provided.';
} else {
document.querySelector('#login form p.err').innerText = 'A system error occured.';
}

document.querySelector('#login form p.err').style.display = 'block';
}
</script>
</body>
</html>

0 comments on commit 9b13cb6

Please sign in to comment.