forked from JensForstmann/tmt2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to disable match creation for unauthenticated users
- Loading branch information
Showing
21 changed files
with
576 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Controller, Get, NoSecurity, Route, Request, Security, Post } from '@tsoa/runtime'; | ||
import { IAuthResponseOptional } from '../../common'; | ||
import * as Auth from './auth'; | ||
|
||
@Route('/api/auth') | ||
export class AuthController extends Controller { | ||
/** | ||
* Get the authentication type for a token. | ||
*/ | ||
@Get() | ||
@NoSecurity() | ||
async getAuthType( | ||
@Request() req: Auth.ExpressRequest<IAuthResponseOptional>, | ||
): Promise<IAuthResponseOptional> { | ||
const token = req.get('authorization'); | ||
if (!token) { | ||
return { type: 'UNAUTHORIZED' }; | ||
} | ||
return Auth.getTokenType(token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { IConfig, IConfigUpdateDto } from "../../common"; | ||
import { checkAndNormalizeLogAddress } from "./match"; | ||
import * as Storage from './storage'; | ||
|
||
const FILE_NAME = 'config.json'; | ||
|
||
// These are not defaults, they're temporary until setup() is called | ||
let config: IConfig = { | ||
tmtLogAddress: null, | ||
allowUnregisteredMatchCreation: false | ||
}; | ||
|
||
const defaults = (): IConfig => { | ||
let tmtLogAdress = null; | ||
if (!process.env['TMT_LOG_ADDRESS']) { | ||
console.warn('Environment variable TMT_LOG_ADDRESS is not set'); | ||
console.warn('Every match must be init with tmtLogAddress'); | ||
} else { | ||
tmtLogAdress = checkAndNormalizeLogAddress(process.env['TMT_LOG_ADDRESS']); | ||
if (!tmtLogAdress) { | ||
throw 'invalid environment variable: TMT_LOG_ADDRESS'; | ||
} | ||
} | ||
|
||
return { | ||
tmtLogAddress: tmtLogAdress, | ||
allowUnregisteredMatchCreation: false | ||
} | ||
}; | ||
|
||
export const get = async (): Promise<IConfig> => { | ||
return config; | ||
} | ||
|
||
export const set = async (data: IConfigUpdateDto) => { | ||
let tmtLogAdress = null; | ||
if ('tmtLogAddress' in data && data.tmtLogAddress) { | ||
tmtLogAdress = checkAndNormalizeLogAddress(data.tmtLogAddress); | ||
if (!tmtLogAdress) { | ||
throw 'invalid tmtLogAddress'; | ||
} | ||
} else if ('tmtLogAddress' in data) { | ||
tmtLogAdress = null; | ||
} else { | ||
tmtLogAdress = config.tmtLogAddress; | ||
} | ||
|
||
let allowUnregisteredMatchCreation = data.allowUnregisteredMatchCreation ?? config.allowUnregisteredMatchCreation; | ||
|
||
config = { | ||
tmtLogAddress: tmtLogAdress, | ||
allowUnregisteredMatchCreation: allowUnregisteredMatchCreation | ||
}; | ||
|
||
await write(); | ||
|
||
return config; | ||
}; | ||
|
||
const write = async () => { | ||
await Storage.write(FILE_NAME, config); | ||
}; | ||
|
||
export const setup = async () => { | ||
// Only get the defaults if the config does not already exist | ||
const data = await Storage.read(FILE_NAME, defaults()); | ||
set(data) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,26 @@ | ||
import { Controller, Get, NoSecurity, Route, Security } from '@tsoa/runtime'; | ||
import { TMT_LOG_ADDRESS } from '.'; | ||
import { IConfig } from '../../common'; | ||
import { Body, Controller, Get, NoSecurity, Patch, Route, Security } from '@tsoa/runtime'; | ||
import { IConfig, IConfigUpdateDto } from '../../common'; | ||
import * as Config from './config'; | ||
|
||
@Route('/api/config') | ||
@Security('bearer_token') | ||
export class ConfigController extends Controller { | ||
/** | ||
* Get some internal config variables. Currently only the set TMT_LOG_ADDRESS. | ||
* Get some internal config variables. | ||
*/ | ||
@Get() | ||
@NoSecurity() | ||
async getConfig(): Promise<IConfig> { | ||
return { | ||
tmtLogAddress: TMT_LOG_ADDRESS, | ||
}; | ||
return Config.get(); | ||
} | ||
|
||
/** | ||
* Update the configuration. | ||
*/ | ||
@Patch() | ||
async updateConfig( | ||
@Body() requestBody: IConfigUpdateDto, | ||
): Promise<IConfig> { | ||
return await Config.set(requestBody); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.