Skip to content

Commit aee77fc

Browse files
committed
update for openflow-api
1 parent c770c56 commit aee77fc

57 files changed

Lines changed: 952 additions & 3242 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

OpenFlow/src/Ace.ts

Lines changed: 0 additions & 77 deletions
This file was deleted.

OpenFlow/src/Audit.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Base, Rights } from "./base";
21
import { Config } from "./Config";
3-
import { TokenUser } from "./TokenUser";
2+
import { TokenUser, Base, Rights } from "openflow-api";
3+
import { Crypt } from "./Crypt";
44

55
export class Audit {
66
public static LoginSuccess(user: TokenUser, type: string, provider: string, remoteip: string, clientagent: string, clientversion: string) {
@@ -15,7 +15,7 @@ export class Audit {
1515
log.username = user.username;
1616
log.clientagent = clientagent;
1717
log.clientversion = clientversion;
18-
Config.db.InsertOne(log, "audit", 0, false, TokenUser.rootToken())
18+
Config.db.InsertOne(log, "audit", 0, false, Crypt.rootToken())
1919
.catch((error) => console.error("failed InsertOne in LoginSuccess: " + error));
2020
}
2121
public static ImpersonateSuccess(user: TokenUser, impostor: TokenUser, clientagent: string, clientversion: string) {
@@ -32,7 +32,7 @@ export class Audit {
3232
log.impostorusername = impostor.username;
3333
log.clientagent = clientagent;
3434
log.clientversion = clientversion;
35-
Config.db.InsertOne(log, "audit", 0, false, TokenUser.rootToken())
35+
Config.db.InsertOne(log, "audit", 0, false, Crypt.rootToken())
3636
.catch((error) => console.error("failed InsertOne in ImpersonateSuccess: " + error));
3737
}
3838
public static ImpersonateFailed(user: TokenUser, impostor: TokenUser, clientagent: string, clientversion: string) {
@@ -48,7 +48,7 @@ export class Audit {
4848
log.impostorname = impostor.name;
4949
log.clientagent = clientagent;
5050
log.clientversion = clientversion;
51-
Config.db.InsertOne(log, "audit", 0, false, TokenUser.rootToken())
51+
Config.db.InsertOne(log, "audit", 0, false, Crypt.rootToken())
5252
.catch((error) => console.error("failed InsertOne in ImpersonateFailed: " + error));
5353
}
5454
public static LoginFailed(username: string, type: string, provider: string, remoteip: string, clientagent: string, clientversion: string) {
@@ -61,7 +61,7 @@ export class Audit {
6161
log.username = username;
6262
log.clientagent = clientagent;
6363
log.clientversion = clientversion;
64-
Config.db.InsertOne(log, "audit", 0, false, TokenUser.rootToken())
64+
Config.db.InsertOne(log, "audit", 0, false, Crypt.rootToken())
6565
.catch((error) => console.error("failed InsertOne in LoginFailed: " + error));
6666
}
6767
}

OpenFlow/src/Auth.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import { User } from "./User";
2-
import { DatabaseConnection } from "./DatabaseConnection";
3-
import { TokenUser } from "./TokenUser";
41
import { Crypt } from "./Crypt";
5-
2+
import { User } from "openflow-api";
3+
import { DBHelper } from "./DBHelper";
64
export class Auth {
7-
8-
public static async ValidateByPassword(username:string, password:string): Promise<User> {
9-
if(username===null||username===undefined||username==="") { throw Error("Username cannot be null"); }
10-
if(password===null||password===undefined||password==="") { throw Error("Password cannot be null"); }
11-
var user: User = await User.FindByUsername(username);
12-
if(user===null || user===undefined) { return null; }
13-
if((await Crypt.compare(password, user.passwordhash)) !== true) { return null; }
5+
public static async ValidateByPassword(username: string, password: string): Promise<User> {
6+
if (username === null || username === undefined || username === "") { throw Error("Username cannot be null"); }
7+
if (password === null || password === undefined || password === "") { throw Error("Password cannot be null"); }
8+
var user: User = await DBHelper.FindByUsername(username);
9+
if (user === null || user === undefined) { return null; }
10+
if ((await Crypt.compare(password, user.passwordhash)) !== true) { return null; }
1411
return user;
1512
}
1613
}

OpenFlow/src/Config.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import * as fs from "fs";
33
import * as retry from "async-retry";
44
import { json } from "body-parser";
55
import { DatabaseConnection } from "./DatabaseConnection";
6-
import { Provider } from "./LoginProvider";
7-
import { TokenUser } from "./TokenUser";
86
import { Logger } from "./Logger";
9-
import { Util } from "./Util";
7+
import { NoderedUtil } from "openflow-api";
108

119
export class Config {
1210
public static reload(): void {
@@ -166,7 +164,7 @@ export class Config {
166164
return result;
167165
}
168166
// public static async get_login_providers():Promise<void> {
169-
// this.login_providers = await Config.db.query<Provider>({_type: "provider"}, null, 1, 0, null, "config", TokenUser.rootToken());
167+
// this.login_providers = await Config.db.query<Provider>({_type: "provider"}, null, 1, 0, null, "config", Crypt.rootToken());
170168
// // if(this.login_providers.length > 0) { return; }
171169
// if(fs.existsSync("config/login_providers.json")) {
172170
// // this.login_providers = JSON.parse(fs.readFileSync("config/login_providers.json", "utf8"));
@@ -181,7 +179,7 @@ export class Config {
181179
// if anything throws, we retry
182180
var metadata: any = await retry(async bail => {
183181
var reader: any = await fetch({ url });
184-
if (Util.IsNullUndefinded(reader)) { bail(new Error("Failed getting result")); return; }
182+
if (NoderedUtil.IsNullUndefinded(reader)) { bail(new Error("Failed getting result")); return; }
185183
var config: any = toPassportConfig(reader);
186184
// we need this, for Office 365 :-/
187185
if (reader.signingCerts && reader.signingCerts.length > 1) {

OpenFlow/src/Crypt.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
import * as crypto from "crypto";
22
import * as bcrypt from "bcryptjs";
33
import * as jsonwebtoken from "jsonwebtoken";
4-
import { Base } from "./base";
5-
import { TokenUser } from "./TokenUser";
6-
import { User } from "./User";
74
import { Config } from "./Config";
8-
import { Util } from "./Util";
9-
5+
import { NoderedUtil, TokenUser, WellknownIds, Rolemember, User } from "openflow-api";
106
export class Crypt {
117
static encryption_key: string = Config.aes_secret.substr(0, 32); // must be 256 bytes (32 characters)
128
static iv_length: number = 16; // for AES, this is always 16
139
static bcrypt_salt_rounds: number = 12;
14-
10+
static rootUser(): User {
11+
var result: User = new User();
12+
result._type = "user"; result.name = "root"; result.username = "root"; result._id = WellknownIds.root;
13+
result.roles = []; result.roles.push(new Rolemember("admins", WellknownIds.admins));
14+
return result;
15+
}
16+
static rootToken(): string {
17+
return Crypt.createToken(this.rootUser(), Config.shorttoken_expires_in);
18+
}
19+
public static async SetPassword(user: User, password: string): Promise<void> {
20+
user.passwordhash = await Crypt.hash(password);
21+
if (!(this.ValidatePassword(user, password))) { throw new Error("Failed validating password after hasing"); }
22+
}
23+
public static async ValidatePassword(user: User, password: string): Promise<boolean> {
24+
return await Crypt.compare(password, user.passwordhash);
25+
}
1526
static encrypt(text: string): string {
1627
let iv: Buffer = crypto.randomBytes(Crypt.iv_length);
1728
let cipher: crypto.Cipher = crypto.createCipheriv("aes-256-cbc", Buffer.from(Crypt.encryption_key), iv);
1829
let encrypted: Buffer = cipher.update((text as any));
1930
encrypted = Buffer.concat([encrypted, cipher.final()]);
2031
return iv.toString("hex") + ":" + encrypted.toString("hex");
2132
}
22-
2333
static decrypt(text: string): string {
2434
let textParts: string[] = text.split(":");
2535
let iv: Buffer = Buffer.from(textParts.shift(), "hex");
@@ -29,7 +39,6 @@ export class Crypt {
2939
decrypted = Buffer.concat([decrypted, decipher.final()]);
3040
return decrypted.toString();
3141
}
32-
3342
static async hash(password: string): Promise<string> {
3443
return new Promise<string>(async (resolve, reject) => {
3544
try {
@@ -45,8 +54,8 @@ export class Crypt {
4554
static async compare(password: string, passwordhash: string): Promise<boolean> {
4655
return new Promise<boolean>(async (resolve, reject) => {
4756
try {
48-
if (Util.IsNullEmpty(password)) { return reject("Password cannot be empty"); }
49-
if (Util.IsNullEmpty(passwordhash)) { return reject("Passwordhash cannot be empty"); }
57+
if (NoderedUtil.IsNullEmpty(password)) { return reject("Password cannot be empty"); }
58+
if (NoderedUtil.IsNullEmpty(passwordhash)) { return reject("Passwordhash cannot be empty"); }
5059
bcrypt.compare(password, passwordhash, async (error, res) => {
5160
if (error) { return reject(error); }
5261
resolve(res);
@@ -57,7 +66,14 @@ export class Crypt {
5766
});
5867
}
5968
static createToken(item: User | TokenUser, expiresIn: string): string {
60-
var user: TokenUser = new TokenUser(item);
69+
var user: TokenUser = new TokenUser();
70+
user._type = (item as User)._type;
71+
user._id = item._id;
72+
user.impostor = (item as TokenUser).impostor;
73+
user.name = item.name;
74+
user.username = item.username;
75+
user.roles = item.roles;
76+
6177
var token: string = jsonwebtoken.sign({ data: user }, Crypt.encryption_key,
6278
{ expiresIn: expiresIn }); // 60 (seconds), "2 days", "10h", "7d"
6379
return token;

0 commit comments

Comments
 (0)