Skip to content

Commit

Permalink
made validateAPIKey async
Browse files Browse the repository at this point in the history
  • Loading branch information
MineFact committed Jan 25, 2024
1 parent a990760 commit 2efe394
Show file tree
Hide file tree
Showing 21 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/routes/internal/GET_ResetCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
app.get('/api/internal/:apikey/resetCache', async function (req, res) {

// Validate that the API key is a valid GUID of the test Build Team
if(!network.validateAPIKey(req, res))
if(!await network.validateAPIKey(req, res))
return;

// Get the Build Team
Expand Down
2 changes: 1 addition & 1 deletion src/routes/internal/GET_Stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
app.get('/api/internal/:apikey/stats', async function (req, res) {

// Validate that the API key is a valid GUID of the test Build Team
if(!network.validateAPIKey(req, res))
if(!await network.validateAPIKey(req, res))
return;

// Get the Build Team
Expand Down
2 changes: 1 addition & 1 deletion src/routes/plotsystem/teams/DELETE_Plot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
app.delete('/api/plotsystem/teams/:apikey/plots', async function (req, res) {

// Validate that the API key is a valid GUID
if(!network.validateAPIKey(req, res))
if(!await network.validateAPIKey(req, res))
return;


Expand Down
2 changes: 1 addition & 1 deletion src/routes/plotsystem/teams/GET_Cities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
app.get('/api/plotsystem/teams/:apikey/cities', async function (req, res) {

// Validate that the API key is a valid GUID
if(!network.validateAPIKey(req, res))
if(!await network.validateAPIKey(req, res))
return;

const buildTeam = await network.getBuildTeam(req.params.apikey, BuildTeamIdentifier.APIKey);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/plotsystem/teams/GET_Countries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
app.get('/api/plotsystem/teams/:apikey/countries', async function (req, res) {

// Validate that the API key is a valid GUID
if(!network.validateAPIKey(req, res))
if(!await network.validateAPIKey(req, res))
return;

const buildTeam = await network.getBuildTeam(req.params.apikey, BuildTeamIdentifier.APIKey);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/plotsystem/teams/GET_FTP_Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
app.get('/api/plotsystem/teams/:apikey/ftp', async function (req, res) {

// Validate that the API key is a valid GUID
if(!network.validateAPIKey(req, res))
if(!await network.validateAPIKey(req, res))
return;

const buildTeam = await network.getBuildTeam(req.params.apikey, BuildTeamIdentifier.APIKey);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/plotsystem/teams/GET_Plots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
app.get('/api/plotsystem/teams/:apikey/plots', async function (req, res) {

// Validate that the API key is a valid GUID
if(!network.validateAPIKey(req, res))
if(!await network.validateAPIKey(req, res))
return;


Expand Down
2 changes: 1 addition & 1 deletion src/routes/plotsystem/teams/GET_Reviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
app.get('/api/plotsystem/teams/:apikey/reviews', async function (req, res) {

// Validate that the API key is a valid GUID
if(!network.validateAPIKey(req, res))
if(!await network.validateAPIKey(req, res))
return;


Expand Down
2 changes: 1 addition & 1 deletion src/routes/plotsystem/teams/GET_Servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
app.get('/api/plotsystem/teams/:apikey/servers', async function (req, res) {

// Validate that the API key is a valid GUID
if(!network.validateAPIKey(req, res))
if(!await network.validateAPIKey(req, res))
return;

const buildTeam = await network.getBuildTeam(req.params.apikey, BuildTeamIdentifier.APIKey);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/plotsystem/teams/POST_Plots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
app.post('/api/plotsystem/teams/:apikey/plots', async function (req, res) {

// Validate that the API key is a valid GUID
if(!network.validateAPIKey(req, res))
if(!await network.validateAPIKey(req, res))
return;

const buildTeam = await network.getBuildTeam(req.params.apikey, BuildTeamIdentifier.APIKey);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/plotsystem/teams/PUT_Plots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
app.put('/api/plotsystem/teams/:apikey/plots', async function (req, res) {

// Validate that the API key is a valid GUID
if(!network.validateAPIKey(req, res))
if(!await network.validateAPIKey(req, res))
return;


Expand Down
3 changes: 1 addition & 2 deletions src/routes/plotsystem/teams/orders/GET_Confirm_Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
app.get('/api/plotsystem/teams/:apikey/orders/:orderId/confirm', async function (req, res) {

// Validate that the API key is a valid GUID
if(!network.validateAPIKeyAndOrderID(req, res))
if(!await network.validateAPIKeyAndOrderID(req, res))
return;

const buildTeam = await network.getBuildTeam(req.params.apikey, BuildTeamIdentifier.APIKey);
Expand All @@ -32,5 +32,4 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
// Return the success message to the client including the plotID
res.send({success: true, plot_id: result.toString()})
})

}
2 changes: 1 addition & 1 deletion src/routes/teams/DELETE_Warp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
app.delete('/api/teams/:apikey/warps', async function (req, res) {

// Validate that the API key is a valid GUID
if(!network.validateAPIKey(req, res))
if(!await network.validateAPIKey(req, res))
return;

const buildTeam = await network.getBuildTeam(req.params.apikey, BuildTeamIdentifier.APIKey);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/teams/DELETE_WarpGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
app.delete('/api/teams/:apikey/warpgroups', async function (req, res) {

// Validate that the API key is a valid GUID
if(!network.validateAPIKey(req, res))
if(!await network.validateAPIKey(req, res))
return;

const buildTeam = await network.getBuildTeam(req.params.apikey, BuildTeamIdentifier.APIKey);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/teams/POST_Warp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
app.post('/api/teams/:apikey/warps', async function (req, res) {

// Validate that the API key is a valid GUID
if(!network.validateAPIKey(req, res))
if(!await network.validateAPIKey(req, res))
return;

const buildTeam = await network.getBuildTeam(req.params.apikey, BuildTeamIdentifier.APIKey);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/teams/POST_WarpGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
app.post('/api/teams/:apikey/warpgroups', async function (req, res) {

// Validate that the API key is a valid GUID
if(!network.validateAPIKey(req, res))
if(!await network.validateAPIKey(req, res))
return;

const buildTeam = await network.getBuildTeam(req.params.apikey, BuildTeamIdentifier.APIKey);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/teams/PUT_TeamHasBuildTeamToolsInstalled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
app.put('/api/teams/:apikey/hasBuildTeamToolsInstalled', async function (req, res) {

// Validate that the API key is a valid GUID
if(!network.validateAPIKey(req, res))
if(!await network.validateAPIKey(req, res))
return;

const buildTeam = await network.getBuildTeam(req.params.apikey, BuildTeamIdentifier.APIKey);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/teams/PUT_Warp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
app.put('/api/teams/:apikey/warps', async function (req, res) {

// Validate that the API key is a valid GUID
if(!network.validateAPIKey(req, res))
if(!await network.validateAPIKey(req, res))
return;

const buildTeam = await network.getBuildTeam(req.params.apikey, BuildTeamIdentifier.APIKey);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/teams/PUT_WarpGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function initRoutes(app: Router, joi: any, network: Network) {
app.put('/api/teams/:apikey/warpgroups', async function (req, res) {

// Validate that the API key is a valid GUID
if(!network.validateAPIKey(req, res))
if(!await network.validateAPIKey(req, res))
return;

const buildTeam = await network.getBuildTeam(req.params.apikey, BuildTeamIdentifier.APIKey);
Expand Down
2 changes: 2 additions & 0 deletions src/struct/core/buildteam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ export default class BuildTeam {
if(this.network.buildTeamServers == null)
await this.network.loadBuildTeamServers();

if(this.network.buildTeamInfo == null || this.network.buildTeamRegions == null || this.network.buildTeamServers == null)
return null;

// BuildTeamInfo is a json array with one object per buildteam
const info = this.network.buildTeamInfo.filter((info: any) => info.APIKey == this.apiKey)[0];
Expand Down
17 changes: 9 additions & 8 deletions src/struct/core/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export default class Network {
this.buildTeams.clear();
this.plotSystem.resetCache();

this.apiKeys = null;
this.apiKeyBuildTeamIDMap.clear();
this.apiKeyBuildTeamTagMap.clear();
this.apiKeyBuildTeamServerMap.clear();
Expand Down Expand Up @@ -170,9 +171,9 @@ export default class Network {
}


getAPIKeys(): string[] {
async getAPIKeys(): Promise<string[]> {
if (this.apiKeys == null) {
this.updateCache();
await this.updateCache();
return [];
}

Expand Down Expand Up @@ -204,7 +205,7 @@ export default class Network {
}

async getBuildTeam(key: string, identifier: BuildTeamIdentifier): Promise<BuildTeam|null|undefined> {
const api_keys = this.getAPIKeys();
const api_keys = await this.getAPIKeys();

let apiKey = null;

Expand Down Expand Up @@ -342,7 +343,7 @@ export default class Network {
// Validate values

// Validate an API key that looks like this "fffb262b-0324-499a-94a6-eebf845e6123"
validateAPIKey(req: express.Request, res: express.Response): boolean {
async validateAPIKey(req: express.Request, res: express.Response): Promise<boolean> {
// Validate that the API key is a valid GUID
const schema = joi.object().keys({
apikey: joi.string().guid().required(),
Expand All @@ -355,7 +356,7 @@ export default class Network {
}

//Validate that the API key exists in the plot system database
const api_keys = this.getAPIKeys();
const api_keys = await this.getAPIKeys();

if (!api_keys.includes(req.params.apikey)) {
res.status(401).send({ success: false, error: "Invalid API key" });
Expand All @@ -366,7 +367,7 @@ export default class Network {
}

// Validate an API key and an Order ID that look like this "fffb262b-0324-499a-94a6-eebf845e6123"
validateAPIKeyAndOrderID(req: express.Request, res: express.Response): boolean {
async validateAPIKeyAndOrderID(req: express.Request, res: express.Response): Promise<boolean> {
// Validate that the API key and the oOder ID is a valid GUID
const schema = joi.object().keys({
apikey: joi.string().guid().required(),
Expand All @@ -380,7 +381,7 @@ export default class Network {
}

//Validate that the API key exists in the plot system database
const api_keys = this.getAPIKeys();
const api_keys = await this.getAPIKeys();

if (!api_keys.includes(req.params.apikey)) {
res.status(401).send({ success: false, error: "Invalid API key" });
Expand All @@ -392,7 +393,7 @@ export default class Network {

// Validate a key that is either an API Key or a Build Team ID or a Build Team Tag or a BuildTeam Server ID
async validateKey(req: express.Request, res: express.Response): Promise<BuildTeamIdentifier|null> {
const apiKeys = this.getAPIKeys();
const apiKeys = await this.getAPIKeys();

// Check if key is an API Key
if(apiKeys.includes(req.params.key))
Expand Down

0 comments on commit 2efe394

Please sign in to comment.