Skip to content

Commit

Permalink
#70 add new table, change credential request, add frontend page
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Werfling committed Apr 29, 2024
1 parent bfd8195 commit 24ecf9b
Show file tree
Hide file tree
Showing 22 changed files with 467 additions and 37 deletions.
26 changes: 26 additions & 0 deletions backend/src/Routes/Main/Credential.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {Router} from 'express';
import {DefaultRoute} from 'flyingfish_core';
import {List} from './Credential/List.js';

export class Credential extends DefaultRoute {

/**
* getExpressRouter
*/
public getExpressRouter(): Router {
this._get(
'/json/credential/list',
async(
req,
res
) => {
if (this.isUserLogin(req, res)) {
res.status(200).json(List.getCredentials());
}
}
);

return super.getExpressRouter();
}

}
26 changes: 26 additions & 0 deletions backend/src/Routes/Main/Credential/List.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {CredentialServiceDB} from 'flyingfish_core';
import {Credential, CredentialResponse, StatusCodes} from 'flyingfish_schemas';

export class List {

public static async getCredentials(): Promise<CredentialResponse> {
const credentials = await CredentialServiceDB.getInstance().findAll();
const list: Credential[] = [];

for (const credential of credentials) {
list.push({
id: credential.id,
name: credential.name,
provider: credential.provider,
authSchemaType: credential.scheme,
settings: credential.settings
});
}

return {
statusCode: StatusCodes.OK,
list: list
};
}

}
29 changes: 10 additions & 19 deletions backend/src/inc/Credential/Credential.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
import {CredentialServiceDB, Logger, NginxLocationServiceDB} from 'flyingfish_core';
import {CredentialLocationServiceDB, CredentialServiceDB, Logger, NginxLocationServiceDB} from 'flyingfish_core';
import {CredentialSchemaBasic} from 'flyingfish_schemas';
import {CredentialProvider} from './CredentialProvider.js';
import {ICredentialAuthBasic} from './ICredential.js';

/**
* CredentialSchemes
*/
export enum CredentialSchemes {
Basic,
Digest
}

/**
* CredentialSchemeBasic
*/
export type CredentialSchemeBasic = {
username: string;
password: string;
};

/**
* Credential
*/
Expand All @@ -28,15 +13,21 @@ export class Credential {
* @param locationId
* @param auth
*/
public static async authBasic(locationId: string, auth: CredentialSchemeBasic): Promise<boolean> {
public static async authBasic(locationId: string, auth: CredentialSchemaBasic): Promise<boolean> {
const nLocationId = parseInt(locationId, 10) || 0;

const location = await NginxLocationServiceDB.getInstance().findOne(nLocationId);

if (location) {
Logger.getLogger().silly(`Credential::authBasic: location found by id: ${location.id}`);

const credentials = await CredentialServiceDB.getInstance().getListByLocation(location.id);
const credLocactions = await CredentialLocationServiceDB.getInstance().getListByLocation(location.id);

const credentials = await CredentialServiceDB.getInstance().findByIds(
credLocactions.map(value => {
return value.credential_id;
})
);

Logger.getLogger().silly(`Credential::authBasic: Found credentials: ${credentials.length}`);

Expand Down
6 changes: 3 additions & 3 deletions backend/src/inc/Credential/CredentialDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as bcrypt from 'bcrypt';
import {CredentialDB, CredentialUserServiceDB, Logger} from 'flyingfish_core';
import {CredentialSchemes} from './Credential.js';
import {CredentialSchemaTypes} from 'flyingfish_schemas/dist/src/index.js';
import {ICredential, ICredentialAuthBasic} from './ICredential.js';

/**
Expand Down Expand Up @@ -55,8 +55,8 @@ export class CredentialDatabase implements ICredential, ICredentialAuthBasic {
/**
* getSupports
*/
public getSupports(): CredentialSchemes[] {
return [CredentialSchemes.Basic];
public getSupports(): CredentialSchemaTypes[] {
return [CredentialSchemaTypes.Basic];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions backend/src/inc/Credential/ICredential.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {CredentialSchemes} from './Credential.js';
import {CredentialSchemaTypes} from 'flyingfish_schemas';

/**
* ICredential
*/
export interface ICredential {
getSupports(): CredentialSchemes[];
getSupports(): CredentialSchemaTypes[];

getName(): string;
}
Expand Down
2 changes: 2 additions & 0 deletions core/src/inc/Db/MariaDb/DBEntitiesLoader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {PluginManager} from '../../PluginSystem/PluginManager.js';
import {Credential} from './Entity/Credential.js';
import {CredentialLocation} from './Entity/CredentialLocation.js';
import {CredentialUser} from './Entity/CredentialUser.js';
import {Domain} from './Entity/Domain.js';
import {DomainRecord} from './Entity/DomainRecord.js';
Expand Down Expand Up @@ -43,6 +44,7 @@ export class DBEntitiesLoader {
const list: MixedList<Function | string | EntitySchema> = [
Credential,
CredentialUser,
CredentialLocation,
Domain,
DomainRecord,
DynDnsClient,
Expand Down
7 changes: 3 additions & 4 deletions core/src/inc/Db/MariaDb/Entity/Credential.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import {Column, Index, Entity} from 'typeorm';
import {Column, Entity} from 'typeorm';
import {DBBaseEntityId} from '../DBBaseEntityId.js';

@Entity({name: 'credential'})
export class Credential extends DBBaseEntityId {

/**
* location id
* Name of credential
*/
@Index()
@Column()
public location_id!: number;
public name!: string;

/**
* scheme
Expand Down
30 changes: 30 additions & 0 deletions core/src/inc/Db/MariaDb/Entity/CredentialLocation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {Column, Entity, Index} from 'typeorm';
import {DBBaseEntityId} from '../DBBaseEntityId.js';

/**
* Credential location a N to N
*/
@Entity({name: 'credential_location'})
export class CredentialLocation extends DBBaseEntityId {

/**
* Id from credential
*/
@Index()
@Column()
public credential_id!: number;

/**
* Id from location
*/
@Index()
@Column()
public location_id!: number;

/**
* Position for fallback
*/
@Column()
public position!: number;

}
38 changes: 38 additions & 0 deletions core/src/inc/Db/MariaDb/Service/CredentialLocationService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {DBService} from '../DBService.js';
import {CredentialLocation} from '../Entity/CredentialLocation.js';

export class CredentialLocationService extends DBService<CredentialLocation> {

/**
* register name
*/
public static REGISTER_NAME = 'credential_location';

/**
* getInstance
*/
public static getInstance(): CredentialLocationService {
return DBService.getSingleInstance(
CredentialLocationService,
CredentialLocation,
CredentialLocationService.REGISTER_NAME
);
}

/**
* getListByLocation
* @param {number} locationId
* @returns {CredentialLocation[]}
*/
public async getListByLocation(locationId: number): Promise<CredentialLocation[]> {
return this._repository.find({
where: {
location_id: locationId
},
order: {
position: 'ASC'
}
});
}

}
13 changes: 6 additions & 7 deletions core/src/inc/Db/MariaDb/Service/CredentialService.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {In} from 'typeorm';
import {DBService} from '../DBService.js';
import {Credential} from '../Entity/Credential.js';

Expand All @@ -23,16 +24,14 @@ export class CredentialService extends DBService<Credential> {
}

/**
* getListByLocation
* @param locationId
* Return all credential by id list
* @param {number[]} credIds
* @returns {Credential[]}
*/
public async getListByLocation(locationId: number): Promise<Credential[]> {
public async findByIds(credIds: number[]): Promise<Credential[]> {
return this._repository.find({
where: {
location_id: locationId
},
order: {
position: 'ASC'
id: In(credIds)
}
});
}
Expand Down
2 changes: 2 additions & 0 deletions core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export {DBEntitiesLoader} from './inc/Db/MariaDb/DBEntitiesLoader.js';

export {Credential as CredentialDB} from './inc/Db/MariaDb/Entity/Credential.js';
export {CredentialUser as CredentialUserDB} from './inc/Db/MariaDb/Entity/CredentialUser.js';
export {CredentialLocation as CredentialLocationDB} from './inc/Db/MariaDb/Entity/CredentialLocation.js';
export {Domain as DomainDB} from './inc/Db/MariaDb/Entity/Domain.js';
export {DomainRecord as DomainRecordDB} from './inc/Db/MariaDb/Entity/DomainRecord.js';
export {DynDnsClient as DynDnsClientDB} from './inc/Db/MariaDb/Entity/DynDnsClient.js';
Expand Down Expand Up @@ -47,6 +48,7 @@ export {User as UserDB} from './inc/Db/MariaDb/Entity/User.js';

export {CredentialService as CredentialServiceDB} from './inc/Db/MariaDb/Service/CredentialService.js';
export {CredentialUserService as CredentialUserServiceDB} from './inc/Db/MariaDb/Service/CredentialUserService.js';
export {CredentialLocationService as CredentialLocationServiceDB} from './inc/Db/MariaDb/Service/CredentialLocationService.js';
export {DomainService as DomainServiceDB} from './inc/Db/MariaDb/Service/DomainService.js';
export {DomainRecordService as DomainRecordServiceDB} from './inc/Db/MariaDb/Service/DomainRecordService.js';
export {DynDnsClientService as DynDnsClientServiceDB} from './inc/Db/MariaDb/Service/DynDnsClientService.js';
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/inc/Api/Credential.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {CredentialResponse, SchemaCredentialResponse} from 'flyingfish_schemas/dist/src';
import {NetFetch} from '../Net/NetFetch';

export class Credential {

public static async getList(): Promise<CredentialResponse> {
return NetFetch.getData('/json/credential/list', SchemaCredentialResponse);
}

}
1 change: 1 addition & 0 deletions frontend/src/inc/Api/Dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ export class Dashboard {

return true;
}

}
Loading

0 comments on commit 24ecf9b

Please sign in to comment.