Skip to content

Commit

Permalink
Merge branch 'activity#2' of github.com:BugBusterSWE/MaaS into activi…
Browse files Browse the repository at this point in the history
…ty#2

Conflicts:
	Jenkinsfile
  • Loading branch information
korut94 committed May 16, 2016
2 parents 6cc8291 + 42c608c commit edc1558
Show file tree
Hide file tree
Showing 39 changed files with 3,815 additions and 41 deletions.
2 changes: 1 addition & 1 deletion burstmake.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"moduleResolution" : "node",
"target": "es5",
"declaration": false,
"noImplicitAny": true,
"noImplicitAny": false,
"noEmitOnError": true,
"sourceMap": true
},
Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@
"license": "MIT",
"dependencies": {
"express": "^4.13.1",
"mongoose": "^4.4.14"
"mongoose": "^4.4.14",
"promise": "^7.1.1",
"body-parser": "^1.15.1",
"helmet": "^2.0.0"

},
"devDependencies": {
"apidoc": "^0.15.1",
Expand All @@ -51,8 +55,8 @@
"mocha": "^2.4.5",
"rimraf": "^2.5.2",
"tslint": "^3.5.0",
"typedoc": "^0.3.12",
"typedoc": "latest",
"typescript": "^1.8.7",
"typings": "^0.7.9"
"typings": "latest"
}
}
8 changes: 4 additions & 4 deletions src/apidoc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "example",
"name": "MaaS",
"version": "0.1.0",
"description": "apiDoc basic example",
"title": "Custom apiDoc browser title",
"url" : "https://api.github.com/v1"
"description": "API for MaaS",
"title": "MaaS: MongoDB as a Service API documentation",
"url" : "http://bugbusterswe.github.io/MaaS/"
}
2 changes: 1 addition & 1 deletion src/config/devConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Configuration from "./configuration";
import MongoConnection from "./mongoConnection";
/**
* @description Development configuration.
* Development configuration.
* @history
* | Author | Action Performed | Data |
* | --- | --- | --- |
Expand Down
20 changes: 13 additions & 7 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import Configuration from "./configuration";
import DevConfiguration from "./devConfiguration";
import TestConfiguration from "./testConfiguration";
import ProdConfiguration from "./prodConfiguration";
import {readFileSync} from "fs";

/**
* @description Class used for get the necessary configuration.
* Class used for get the necessary configuration.
* @history
* | Author | Action Performed | Data |
* | --- | --- | --- |
Expand All @@ -20,16 +21,21 @@ class ChooseConfiguration {
* @description Return the right configuration according to the Node.js
* environment variable. It may be: 'development', 'test' or 'production'.
* The default configuration is the 'production' one.
* The connection's parameters are read fro an external json file named
* mongoParameters.json.
* @returns {Configuration} The configuration.
*/
public static getConfig() : Configuration {
/** @todo parameters */
let params : MongoConnection = JSON.parse(readFileSync(
"src/config/mongoParameters.json",
"utf-8"
));
let connection : MongoConnection = new MongoConnection(
"admin",
"admin",
"ds013250.mlab.com",
13250,
"mongocbtest"
params["user"],
params["password"],
params["host"],
params["port"],
params["dbName"]
);
let serverSecret : string = "serverSecret";
let config : Configuration;
Expand Down
2 changes: 1 addition & 1 deletion src/config/mongoConnection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @description This class stores the parameters for the MondoDB connection.
* This class stores the parameters for the MondoDB connection.
* @history
* | Author | Action Performed | Data |
* | --- | --- | --- |
Expand Down
7 changes: 7 additions & 0 deletions src/config/mongoParameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"user" : "admin",
"password" : "admin",
"host" : "ds013250.mlab.com",
"port" : 13250,
"dbName" : "mongocbtest"
}
2 changes: 1 addition & 1 deletion src/config/prodConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Configuration from "./configuration";
import MongoConnection from "./mongoConnection";

/**
* @description Production configuration.
* Production configuration.
* @history
* | Author | Action Performed | Data |
* | --- | --- | --- |
Expand Down
169 changes: 169 additions & 0 deletions src/lib/authenticationChecker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import * as jwt from "jsonwebtoken";
import UserModel from "../models/userModel";
import UserDocument from "../models/userModel";
import * as express from "express";
import ConfigurationChooser from "../config/index";
import * as mongoose from "mongoose";

/**
* This class is used to check if the current user is correctly authenticate
* to the MaaS application.
*
* @history
* | Author | Action Performed | Data |
* | --- | --- | --- |
* | Luca Bianco | Create class | 07/05/2016 |
*
* @author Luca Bianco
* @license MIT
*/

class AuthenticationChecker {

/**
* @description Server's secret string, used for encode the JWT tokens.
*/
private secret : string;

/**
* @description Request's expire time. By default it is 60*24*7.
*/
private DEFAULT_EXPIRE_TIME : number = 60 * 24 * 7;

/**
* @description Default name of the 'username' field in every request.
*/
private USERNAME_BODY_FIELD : string = "username";

/**
* @description Default name of the 'password' field in every request.
*/
private PASSWORD_BODY_FIELD : string = "password";

/**
* @description Login the user.
* @param request The express request.
* <a href="http://expressjs.com/en/api.html#req">See</a> the official
* documentation for more details.
* @param response The express response object.
* <a href="http://expressjs.com/en/api.html#res">See</a> the official
* documentation for more details.
*/
public login(request : express.Request,
response : express.Response) : void {
let username : string = request.body[this.USERNAME_BODY_FIELD];
let password : string = request.body[this.PASSWORD_BODY_FIELD];

// TODO: sistemare inclusione del modello utente
let userModel : UserModel = new UserModel();
userModel
.login(username, password) // Call the login method...
.then(function (user : UserDocument) :
void { // ...when done, let's say it to the client
if (!user) {
this.loginFailed(response);
} else {
let userToken : string = this.createToken(user);
response.status(200);
response.json({
done: true,
message: "Authentication done",
token: userToken,
user_id: user["_id"]
});
}
})
}

/**
* @description Authenticate the user.
* @param request The express request.
* <a href="http://expressjs.com/en/api.html#req">See</a> the official
* documentation for more details.
* @param response The express response object.
* <a href="http://expressjs.com/en/api.html#res">See</a> the official
* documentation for more details.
* @param next Function which invokes the next route handler in framework.
*/
public authenticate(
request : express.Request,
response : express.Response,
next : express.NextFunction) : void {
let token : string = request.body.token ||
request.query.token ||
request.headers["x-access-token"];

if (!token) { // Token not found
this.responseTokenNotFound(response);
} else {
jwt.verify(token, this.secret,
function (err : Error, decoded : Object) : void {
if (err) { // Authentication failed
this.responseAuthenticationFailed(response);
} else { // Success!
request.user = decoded;
next();
}
});
}
}

/**
* @description Create the JWT token for the current user.
* @param data User's data.
* @returns {string} A string which represents the JWT token created.
*/
private createToken(data : Object) : string {
return jwt.sign(
{
data: data,
expireTime: this.DEFAULT_EXPIRE_TIME
},
ConfigurationChooser.getConfig().getServerSecret()
);
}

/**
* @descripton
* Create a parametrized response for the token not found situation.
* @param response The generated response with an error message which
* represents the "token not found" situation.
*/
private responseTokenNotFound(response : express.Response) : void {
response.status(403);
response.json({
done: false,
message: "Authentication failed. No Token Found"
});
}

/**
* @description
* Create a parametrized response for the authentication failed situation.
* @param response The generated response with an error message which
* represents the "authentication failed" situation.
*/
private responseAuthenticationFailed(response : express.Response) : void {
response.status(403);
response.json({
done: false,
message: "Authentication failed. Token invalid"
});
}

/**
* @description
* Create a parametrized response for the login failed situation.
* @param response The generated response with an error message which
* represents the "login failed" situation.
*/
private loginFailed(response : express.Response) : void {
response.status(401);
response.json({
done: false,
message: "Login Failed"
});
}
}

export default AuthenticationChecker;
Loading

0 comments on commit edc1558

Please sign in to comment.