1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2025-06-20 00:25:17 +00:00

Sign in user

This commit is contained in:
2019-11-23 13:16:28 +01:00
parent 89e612900c
commit 293752b0f4
7 changed files with 234 additions and 8 deletions

View File

@ -0,0 +1,102 @@
import { crypt, sha1, randomStr } from "../utils/CryptUtils";
import { APIClient } from "../entities/APIClient";
import { UserLoginTokens } from "../entities/UserLoginTokens";
import { DatabaseHelper } from "./DatabaseHelper";
/**
* Account helper
*
* @author Pierre HUBERT
*/
const USER_TABLE = "utilisateurs";
const USERS_TOKENS_TABLE = "comunic_api_users_tokens";
export class AccountHelper {
/**
* Given email address and password, try to sign in user
*
* @param email The email of the user
* @param password User password
* @param client Information about associated client / null if none found
*/
static async LoginUser(email: string, password: string, client: APIClient) : Promise<UserLoginTokens | null> {
// Perform a request on the database
const row = await DatabaseHelper.QueryRow({
table: USER_TABLE,
fields: ["ID"],
where: {
mail: email,
password: this.CryptPassword(password)
}
});
// Check if user was found
if(row == null)
return null;
const userID = row.ID;
// Check for existing tokens
let tokens = await this.GetClientTokens(userID, client);
if(tokens != null)
return tokens;
const newTokens : UserLoginTokens = {
userID: userID,
clientID: client.id,
token1: randomStr(150),
token2: "dummy_data"
}
// Save new tokens
await DatabaseHelper.InsertRow(USERS_TOKENS_TABLE, {
user_id: newTokens.userID,
service_id: newTokens.clientID,
token1: newTokens.token1,
token2: newTokens.token2
});
return newTokens;
}
/**
* Get user client tokens (if it exists)
*
* @param userID Target user ID
* @param client Information about associated client / null if none found
*/
private static async GetClientTokens(userID: number, client: APIClient): Promise<UserLoginTokens | null> {
const row = await DatabaseHelper.QueryRow({
table: USERS_TOKENS_TABLE,
where: {
user_id: userID,
service_id: client.id
}
});
return row == null ? null : this.DBToUserTokens(row);
}
/**
* Crypt a password
*
* @param pass The password to crypt
* @return Encrypted string
*/
private static CryptPassword(pass: string) : string {
return crypt(sha1(pass), sha1(pass));
}
private static DBToUserTokens(row : any) : UserLoginTokens {
return {
userID: row.user_id,
clientID: row.service_id,
token1: row.token1,
token2: row.token2
};
}
}

View File

@ -9,6 +9,7 @@ import { conf } from "./ConfigHelper";
export interface QueryInformation {
table: string,
fields ?: Array<String>,
where ?: Object,
limit ?: number,
}
@ -51,7 +52,12 @@ export class DatabaseHelper {
*/
static async Query(info: QueryInformation) : Promise<Array<any>> {
// Prepare SQL request
let request = "SELECT * FROM " + info.table;
let request = "SELECT ";
// Requested fields
request += info.fields ? info.fields.join(",") : "*";
request += " FROM " + info.table;
let args = [];
// Add where arguments
@ -61,7 +67,7 @@ export class DatabaseHelper {
for(const k in info.where) {
if(!info.where.hasOwnProperty(k))
continue;
const v = info.where[k];
const v = info.where[k].toString();
request += k;
request += v.startsWith("%") || v.endsWith("%") ? " LIKE " : " = "
@ -107,4 +113,21 @@ export class DatabaseHelper {
return result[0];
}
/**
* Insert an new entry into the database
*
* @param info Information about the entry
* @returns The ID of the inserted column (if any)
*/
static async InsertRow(table : string, values : any) : Promise<number> {
return new Promise((resolve, reject) => {
this.connection.query("INSERT INTO " + table + " SET ?", values, (err, results, fields) => {
if(err)
reject(err);
resolve(results.insertId);
});
});
}
}