1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-30 14:31:42 -05:00

Add auth to login and start panel

This commit is contained in:
Jack 2017-10-11 20:26:07 -04:00
parent 123672dbfb
commit 53bf056bc5
Signed by: foltik
GPG Key ID: 303F88F996E95541
22 changed files with 107 additions and 29 deletions

View File

@ -6,7 +6,7 @@ var InviteSchema = mongoose.Schema({
unique: true,
required: true
},
level: Number,
scope: [String],
issuer: String,
recipient: String,
issued: Date,

30
app/models/Token.js Normal file
View File

@ -0,0 +1,30 @@
var fs = require('fs');
var path = require('path');
var mongoose = require('mongoose');
var jwt = require('jsonwebtoken');
var jwtsign = require('jwt-sign');
var TokenSchema = mongoose.Schema({
scope: [String],
issuer: String,
issued: Date,
exp: Date
});
TokenSchema.methods.genJwt = function(expiry) {
var exp = new Date();
exp.setDate(exp.getDate() + expiry);
var payload = {
_id: this._id,
username: this.username,
scope: this.scope,
exp: parseInt(exp.getTime() / 1000)
};
var key = fs.readFilySync(path.join(__dirname, '../../jwt.pem'), 'utf8');
return jwt.sign(payload, key);
};
module.exports = mongoose.model('Token', TokenSchema);

View File

@ -11,12 +11,7 @@ var UserSchema = mongoose.Schema({
unique: true,
required: true
},
apikey: {
type: String,
unique: true,
required: true
},
level: Number,
scope: [String],
hash: String,
salt: String,
date: Date
@ -32,10 +27,6 @@ UserSchema.methods.validatePassword = function(password) {
return this.hash === hash;
};
UserSchema.methods.genApiKey = function() {
this.apikey = crypto.randomBytes(16).toString('hex');
};
UserSchema.methods.genJwt = function() {
var expiry = new Date();
expiry.setDate(expiry.getDate() + 7);
@ -43,7 +34,8 @@ UserSchema.methods.genJwt = function() {
var payload = {
_id: this._id,
username: this.username,
level: this.level
scope: this.scope,
exp: parseInt(expiry.getTime() / 1000)
};
var key = fs.readFileSync(path.join(__dirname, '../../jwt.pem'), 'utf8');

View File

@ -4,6 +4,7 @@ var view = require('./routes/view.js');
var auth = require('./routes/auth.js');
var register = require('./routes/register.js');
var login = require('./routes/login.js');
var panel = require('./routes/panel');
var fs = require('fs');
var path = require('path');
@ -16,10 +17,12 @@ var jwtauth = jwt({
module.exports = function(app) {
app.use('/', index);
app.use('/v', view);
app.use('/upload', upload);
app.use('/api/upload', jwtauth, upload);
app.use('/api/auth', auth);
app.use('/register', register);
app.use('/login', login);
app.use('/panel', panel);
app.use('/panel*', panel);
app.use(function(err, req, res, next) {
if (err.name === 'UnauthorizedError') {

View File

@ -48,9 +48,8 @@ router.post('/register', function(req, res) {
var user = new User();
user.username = req.body.username;
user.level = invite.level;
user.scope = invite.scope;
user.date = Date.now();
user.genApiKey();
user.setPassword(req.body.password);
user.save(function(err) {

9
app/routes/panel.js Normal file
View File

@ -0,0 +1,9 @@
var express = require('express');
var router = express.Router();
var path = require('path');
router.get('/', function(req, res, next) {
res.sendFile(path.join(__dirname, '../../public/views', 'panel.html'));
});
module.exports = router;

View File

@ -5,7 +5,6 @@ var mongoose = require('mongoose');
var Upload = mongoose.model('Upload');
router.get('/:name', function(req, res, next) {
console.log('GET: ', req.params);
Upload.findOne({
'name': req.params.name
}, function(err, upload) {

View File

@ -6,7 +6,7 @@
"font-awesome": "latest",
"animate.css": "latest",
"angular": "latest",
"angular-route": "latest",
"angular-ui-router": "latest",
"ng-file-upload": "latest",
"ngclipboard": "^1.1.1"
}

View File

@ -3,6 +3,7 @@
"version": "1.0.0",
"dependencies": {
"angular-messages": "^1.6.6",
"angular-ui-router": "^0.4.3",
"body-parser": "^1.18.2",
"express": "latest",
"express-jwt": "^5.3.0",

0
public/css/panel.css Normal file
View File

14
public/js/PanelRoutes.js Normal file
View File

@ -0,0 +1,14 @@
angular.module('PanelRoutes', ['ui.router']).config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$urlRouterProvider.otherwise('/panel');
$stateProvider
.state('/panel', {
url: '/panel',
templateUrl: '/views/panel-home.html'
}).state('/panel/api', {
url: '/panel/api',
templateUrl: '/views/panel-api.html'
});
}]);

View File

@ -1 +0,0 @@
angular.module('shimapan', ['ngFileUpload', 'ngclipboard', 'UploadComp', 'RegisterComp', 'LoginComp']);

View File

@ -12,7 +12,7 @@ function UploadController($scope, Upload, $timeout, AuthService) {
angular.forEach(files, function (file) {
file.upload = Upload.upload({
url: '/upload',
url: '/api/upload',
method: 'POST',
headers: {
'Authorization': AuthService.getAuthHeader()
@ -27,8 +27,16 @@ function UploadController($scope, Upload, $timeout, AuthService) {
});
},
function (response) {
if (response.status > 0)
$scope.errorMsg = response.status + ': ' + response.data;
if (response.status > 0) {
if (response.status === 401) {
file.$error = "Invalid authorization token.";
} else {
file.$error = "Internal server error.";
}
var index = $scope.files.indexOf(file);
$scope.errorFiles.push(file);
$scope.files.splice(index, 1);
}
},
function (evt) {
file.progress = Math.floor(Math.min(100.0, 100 * evt.loaded / evt.total));

View File

@ -0,0 +1 @@
var app = angular.module('shimapan-panel', ['ui.router', 'PanelRoutes']);

1
public/js/shimapan.js Normal file
View File

@ -0,0 +1 @@
var app = angular.module('shimapan', ['ngFileUpload', 'ngclipboard', 'UploadComp', 'RegisterComp', 'LoginComp']);

View File

@ -18,7 +18,7 @@
<script src="/js/components/RegisterComp.js"></script>
<script src="/js/components/LoginComp.js"></script>
<script src="/js/services/AuthSvc.js"></script>
<script src="/js/app.js"></script>
<script src="/js/shimapan.js"></script>
</head>
<body ng-app="shimapan">
<div id="container">
@ -27,7 +27,7 @@
<nav>
<ul>
<li><a href="/">Shimapan</a></li>
<li><a href="">Panel</a></li>
<li><a href="/panel">Panel</a></li>
<li><a href="">Tools</a></li>
<li><a href="">Git</a></li>
<li><a href="">FAQ</a></li>

View File

@ -6,15 +6,17 @@
<title>Login</title>
<script src="/libs/angular/angular.min.js"></script>
<script src="/libs/angular-route/angular-route.min.js"></script>
<script src="/libs/ng-file-upload/ng-file-upload-all.min.js"></script>
<script src="https://cdn.rawgit.com/zenorocha/clipboard.js/master/dist/clipboard.min.js"></script>
<script src="/libs/ngclipboard/src/ngclipboard.js"></script>
<script src="/js/ngRoutes.js"></script>
<script src="/js/components/UploadComp.js"></script>
<script src="/js/components/RegisterComp.js"></script>
<script src="/js/components/LoginComp.js"></script>
<script src="/js/services/AuthSvc.js"></script>
<script src="/js/app.js"></script>
<script src="/js/shimapan.js"></script>
</head>
<body ng-app="shimapan">
<div id="container">

View File

@ -0,0 +1 @@
<h1>Panel api hurr durr</h1>

View File

@ -0,0 +1,2 @@
<h1>This is yer home</h1>
<p>This is a pargragraph</p>

21
public/views/panel.html Normal file
View File

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<base href="/panel"/>
<title>Panel</title>
<link rel="stylesheet" href="/css/panel.css"/>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"/>
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet"/>
<script src="/libs/angular/angular.min.js"></script>
<script src="/libs/angular-ui-router/release/angular-ui-router.min.js"></script>
<script src="/js/PanelRoutes.js"></script>
<script src="/js/shimapan-panel.js"></script>
</head>
<body ng-app="shimapan-panel">
<div id="container" ui-view></div>
</body>
</html>

View File

@ -14,7 +14,7 @@
<script src="/js/components/RegisterComp.js"></script>
<script src="/js/components/LoginComp.js"></script>
<script src="/js/services/AuthSvc.js"></script>
<script src="/js/app.js"></script>
<script src="/js/shimapan.js"></script>
</head>
<body ng-app="shimapan">
<div id="container">

View File

@ -27,10 +27,6 @@ app.use(bodyParser.urlencoded({ extended: true }));
app.use(passport.initialize());
app.use(methodOverride('X-HTTP-Method-Override'));
app.get('/secret', passport.authenticate('local', { session: false }), function(req, res) {
res.json("You cannot see this without a token!");
});
// Set /public to document root
app.use(express.static(__dirname + '/public'));
require('./app/routes')(app);