Session requests and types for fingerprint and user added.

This commit is contained in:
Mal 2020-09-07 22:45:52 +02:00
parent 49a1fd26e7
commit 894ba92016
11 changed files with 231 additions and 3 deletions

View File

@ -8,7 +8,7 @@ export default class ApiRequest
protected method: RequestMethod; protected method: RequestMethod;
protected url: string; protected url: string;
protected parameters: Record<string, any> = {}; protected parameters: Record<string, any> = {};
protected onSuccess: (responseText: string, status: number) => void; protected onSuccess: (responseText: string) => void;
protected onError: (responseText: string, status: number) => void; protected onError: (responseText: string, status: number) => void;
public constructor(url: string, method: RequestMethod) public constructor(url: string, method: RequestMethod)
@ -31,7 +31,7 @@ export default class ApiRequest
this.request.onreadystatechange = () => { this.request.onreadystatechange = () => {
if (this.request.readyState === 4) { if (this.request.readyState === 4) {
if (this.request.status === 200) { if (this.request.status === 200) {
this.onSuccess(this.request.responseText, this.request.status); this.onSuccess(this.request.responseText);
} else { } else {
this.onError(this.request.responseText, this.request.status); this.onError(this.request.responseText, this.request.status);
} }
@ -42,7 +42,7 @@ export default class ApiRequest
this.request.send(JSON.stringify(this.parameters)); this.request.send(JSON.stringify(this.parameters));
} }
public setOnSuccess(callback: (responseText: string, status: number) => void): void public setOnSuccess(callback: (responseText: string) => void): void
{ {
this.onSuccess = callback; this.onSuccess = callback;
} }

View File

@ -0,0 +1,19 @@
import ApiRequest from "./ApiRequest.js";
import RequestMethod from "./RequestMethod.js";
import Fingerprint from "../types/Fingerprint.js";
import FingerprintGetEvent from "../event/FingerprintGetEvent.js";
export default class FingerprintGetApiRequest extends ApiRequest
{
public constructor(fingerprintId: number) {
super('fingerprint/' + fingerprintId, RequestMethod.GET);
this.onSuccess = (response: string): void => {
let json = JSON.parse(response);
let fingerprint = new Fingerprint(json.result.fingerprintId, json.result.fingerprint, json.result.userId);
window.dispatchEvent(new FingerprintGetEvent(fingerprint));
}
}
}

View File

@ -1,10 +1,24 @@
import ApiRequest from "./ApiRequest.js"; import ApiRequest from "./ApiRequest.js";
import RequestMethod from "./RequestMethod.js"; import RequestMethod from "./RequestMethod.js";
import UserGetEvent from "../event/UserGetEvent.js";
import User from "../types/User.js";
export default class UserGetApiRequest extends ApiRequest export default class UserGetApiRequest extends ApiRequest
{ {
public constructor(userId: number) public constructor(userId: number)
{ {
super('user/' + userId, RequestMethod.GET); super('user/' + userId, RequestMethod.GET);
this.onSuccess = (response: string) => {
let json = JSON.parse(response);
let user = new User(json.result.userId);
user.setUsername(json.result.username);
user.setJabberAddress(json.result.jabberAddress);
user.setAdmin(json.result.isAdmin);
user.setFingerprintIds(json.result.fingerprintIds);
window.dispatchEvent(new UserGetEvent(user));
}
} }
} }

View File

@ -0,0 +1,20 @@
import ApiRequest from "./ApiRequest.js";
import RequestMethod from "./RequestMethod.js";
import UserLoginSuccessEvent from "../event/UserLoginSuccessEvent.js";
export default class UserLoginApiRequest extends ApiRequest
{
public constructor(username: string, password: string)
{
super('user/session', RequestMethod.POST);
this.addParameter('username', username);
this.addParameter('password', password);
this.onSuccess = (response: string) =>
{
let json = JSON.parse(response);
window.dispatchEvent(new UserLoginSuccessEvent(json.userId));
};
}
}

View File

@ -0,0 +1,9 @@
import ApiRequest from "./ApiRequest.js";
import RequestMethod from "./RequestMethod.js";
export default class UserLogoutApiRequest extends ApiRequest
{
public constructor() {
super('user/session', RequestMethod.DELETE);
}
}

View File

@ -0,0 +1,20 @@
import RingfingerEvent from "./RingfingerEvent.js";
import Fingerprint from "../types/Fingerprint.js";
export default class FingerprintGetEvent extends RingfingerEvent
{
public static readonly NAME: string = 'fingerprintgetevent';
private fingerprint: Fingerprint;
public constructor(fingerprint: Fingerprint) {
super(FingerprintGetEvent.NAME);
this.fingerprint = fingerprint;
}
public getFingerprint(): Fingerprint
{
return this.fingerprint;
}
}

View File

@ -0,0 +1,4 @@
export default class RingfingerEvent extends Event
{
public static readonly NAME: string;
}

21
js/event/UserGetEvent.ts Normal file
View File

@ -0,0 +1,21 @@
import RingfingerEvent from "./RingfingerEvent.js";
import User from "../types/User.js";
export default class UserGetEvent extends RingfingerEvent
{
public static readonly NAME: string = 'usergetevent';
private user: User;
public constructor(user: User)
{
super(UserGetEvent.NAME);
this.user = user;
}
public getUser(): User
{
return this.user;
}
}

View File

@ -0,0 +1,19 @@
import RingfingerEvent from "./RingfingerEvent.js";
export default class UserLoginSuccessEvent extends RingfingerEvent
{
public static readonly NAME: string = 'userloginsuccessevent';
private userId: number;
public constructor(userId: number) {
super(UserLoginSuccessEvent.NAME);
this.userId = userId;
}
public getUserId(): number
{
return this.userId;
}
}

49
js/types/Fingerprint.ts Normal file
View File

@ -0,0 +1,49 @@
export default class Fingerprint
{
private static readonly CHARS_ALLOWED: string = '1234567890abcdef';
private readonly fingerprintId: number;
private readonly fingerprint: string;
private readonly userId: number;
public constructor(fingerprintId: number, fingerprint: string, userId: number)
{
if (!Fingerprint.validateFingerprint(fingerprint)) {
throw new Error(fingerprint + ' is not a valid fingerprint!');
}
this.fingerprintId = fingerprintId;
this.fingerprint = fingerprint;
this.userId = userId;
}
public getFingerprint(): string
{
return this.fingerprint;
}
public getUserId(): number
{
return this.userId;
}
public getFingerprintId(): number
{
return this.fingerprintId;
}
private static validateFingerprint(fingerprint: string): boolean
{
if (fingerprint.length !== 64) {
return false;
}
for (let i: number = 0; i < fingerprint.length; ++i) {
if (Fingerprint.CHARS_ALLOWED.indexOf(fingerprint[i]) === -1) {
return false;
}
}
return true;
}
}

53
js/types/User.ts Normal file
View File

@ -0,0 +1,53 @@
export default class User
{
private userId: number;
private username: string = '';
private jabberAddress: string = '';
private admin: boolean = false;
private fingerprintIds: Array<number> = [];
public constructor(userId: number)
{
this.userId = userId;
}
public setUsername(username: string): void
{
this.username = username;
}
public getUsername(): string
{
return this.username;
}
public setJabberAddress(jabberAddress: string): void
{
this.jabberAddress = jabberAddress;
}
public getJabberAddress(): string
{
return this.jabberAddress;
}
public setAdmin(isAdmin: boolean): void
{
this.admin = isAdmin;
}
public isAdmin(): boolean
{
return this.admin;
}
public setFingerprintIds(fingerprintIds: Array<number>)
{
this.fingerprintIds = fingerprintIds;
}
public getFingerprintIds(): Array<number>
{
return this.fingerprintIds;
}
}