All checks were successful
CI / container-job (push) Successful in 49s
- Modified Api/Authors.Controller.ts to use only personal types and sort by dataset_count. - Completely rewritten AvatarController.ts. - Added new Api/CollectionsController.ts for querying collections and collection_roles. - Modified Api/DatasetController.ts to preload titles, identifier and order by server_date_published. - Modified FileController.ts to serve files from /storage/app/data/ instead of /storage/app/public. - Added new Api/UserController for requesting submitters (getSubmitters). - Improved OaiController.ts with performant DB queries for better ResumptionToken handling. - Modified Submitter/DatasetController.ts by adding a categorize method for library classification. - Rewritten ResumptionToken.ts. - Improved TokenWorkerService.ts to utilize browser fingerprint. - Edited dataset.ts by adding the doiIdentifier property. - Enhanced person.ts to improve the fullName property. - Completely rewritten AsideMenuItem.vue component. - Updated CarBoxClient.vue to use TypeScript. - Added new CardBoxDataset.vue for displaying recent datasets on the dashboard. - Completely rewritten TableSampleClients.vue for the dashboard. - Completely rewritten UserAvatar.vue. - Made small layout changes in Dashboard.vue. - Added new Category.vue for browsing scientific collections. - Adapted the pinia store in main.ts. - Added additional routes in start/routes.ts and start/api/routes.ts. - Improved referenceValidation.ts for better ISBN existence checking. - NPM dependency updates.
132 lines
5.2 KiB
TypeScript
132 lines
5.2 KiB
TypeScript
import type { HttpContext } from '@adonisjs/core/http';
|
||
import User from '#models/user';
|
||
import TwoFactorAuthProvider from '#app/services/TwoFactorAuthProvider';
|
||
import { StatusCodes } from 'http-status-codes';
|
||
import { InvalidArgumentException } from 'node-exceptions';
|
||
import { TotpState } from '#contracts/enums';
|
||
import BackupCodeStorage, { SecureRandom } from '#services/backup_code_storage';
|
||
import BackupCode from '#models/backup_code';
|
||
|
||
// Here we are generating secret and recovery codes for the user that’s enabling 2FA and storing them to our database.
|
||
export default class UserController {
|
||
public async getSubmitters({ response }: HttpContext) {
|
||
try {
|
||
const submitters = await User.query()
|
||
.preload('roles', (query) => {
|
||
query.where('name', 'submitter')
|
||
})
|
||
.whereHas('roles', (query) => {
|
||
query.where('name', 'submitter')
|
||
})
|
||
.exec();
|
||
return submitters;
|
||
} catch (error) {
|
||
return response.status(StatusCodes.INTERNAL_SERVER_ERROR).json({
|
||
message: 'Invalid TOTP state',
|
||
});
|
||
}
|
||
}
|
||
|
||
public async enable({ auth, response, request }: HttpContext) {
|
||
const user = (await User.find(auth.user?.id)) as User;
|
||
// await user.load('totp_secret');
|
||
// if (!user.totp_secret) {
|
||
// let totpSecret = new TotpSecret();
|
||
// user.related('totp_secret').save(totpSecret);
|
||
// await user.load('totp_secret');
|
||
// }
|
||
|
||
if (!user) {
|
||
throw new Error('user not available');
|
||
}
|
||
const state: number = request.input('state');
|
||
|
||
try {
|
||
switch (state) {
|
||
case TotpState.STATE_DISABLED:
|
||
// user.twoFactorSecret = null;
|
||
// user.twoFactorRecoveryCodes = null;
|
||
await BackupCode.deleteCodes(user);
|
||
user.twoFactorSecret = '';
|
||
// user.twoFactorRecoveryCodes = [''];
|
||
await user.save();
|
||
|
||
user.state = TotpState.STATE_DISABLED;
|
||
await user.save();
|
||
|
||
let storage = new BackupCodeStorage(new SecureRandom());
|
||
let backupState = await storage.getBackupCodesState(user);
|
||
|
||
return response.status(StatusCodes.OK).json({
|
||
state: TotpState.STATE_DISABLED,
|
||
backupState: backupState,
|
||
});
|
||
case TotpState.STATE_CREATED:
|
||
user.twoFactorSecret = TwoFactorAuthProvider.generateSecret(user);
|
||
user.state = TotpState.STATE_CREATED;
|
||
await user.save();
|
||
|
||
let qrcode = await TwoFactorAuthProvider.generateQrCode(user);
|
||
// throw new InvalidArgumentException('code is missing');
|
||
return response.status(StatusCodes.OK).json({
|
||
state: user.state,
|
||
secret: user.twoFactorSecret,
|
||
url: qrcode.url,
|
||
svg: qrcode.svg,
|
||
});
|
||
case TotpState.STATE_ENABLED:
|
||
let code: string = request.input('code');
|
||
if (!code) {
|
||
throw new InvalidArgumentException('code is missing');
|
||
}
|
||
const success = await TwoFactorAuthProvider.enable(user, code);
|
||
|
||
return response.status(StatusCodes.OK).json({
|
||
state: success ? TotpState.STATE_ENABLED : TotpState.STATE_CREATED,
|
||
});
|
||
default:
|
||
throw new InvalidArgumentException('Invalid TOTP state');
|
||
}
|
||
} catch (error) {
|
||
return response.status(StatusCodes.INTERNAL_SERVER_ERROR).json({
|
||
message: 'Invalid TOTP state',
|
||
});
|
||
}
|
||
}
|
||
|
||
// public async fetchRecoveryCodes({ auth, view }) {
|
||
// const user = auth?.user;
|
||
|
||
// return view.render('pages/settings', {
|
||
// twoFactorEnabled: user.isTwoFactorEnabled,
|
||
// recoveryCodes: user.twoFactorRecoveryCodes,
|
||
// });
|
||
// }
|
||
|
||
/**
|
||
* @NoAdminRequired
|
||
* @PasswordConfirmationRequired
|
||
*
|
||
* @return JSONResponse
|
||
*/
|
||
public async createCodes({ auth, response }: HttpContext) {
|
||
// $user = $this->userSession->getUser();
|
||
const user = (await User.find(auth.user?.id)) as User;
|
||
|
||
// let codes = TwoFactorAuthProvider.generateRecoveryCodes();
|
||
let storage = new BackupCodeStorage(new SecureRandom());
|
||
// $codes = $this->storage->createCodes($user);
|
||
const codes = await storage.createCodes(user);
|
||
|
||
let backupState = await storage.getBackupCodesState(user);
|
||
// return new JSONResponse([
|
||
// 'codes' => $codes,
|
||
// 'state' => $this->storage->getBackupCodesState($user),
|
||
// ]);
|
||
return response.status(StatusCodes.OK).json({
|
||
codes: codes,
|
||
// state: success ? TotpState.STATE_ENABLED : TotpState.STATE_CREATED,
|
||
backupState: backupState, //storage.getBackupCodesState(user),
|
||
});
|
||
}
|
||
}
|