2019-07-16 08:40:01 +02:00
|
|
|
import { Response, Router } from 'express';
|
2020-10-03 05:47:04 -07:00
|
|
|
import buildDebug from 'debug';
|
2020-09-17 06:48:16 +02:00
|
|
|
import { $RequestExtend, $NextFunctionVer } from '../types/custom';
|
2020-10-03 05:47:04 -07:00
|
|
|
|
|
|
|
const debug = buildDebug('verdaccio:api:user');
|
2019-07-16 08:40:01 +02:00
|
|
|
|
2021-03-14 08:42:46 +01:00
|
|
|
export default function (route: Router): void {
|
|
|
|
route.get('/whoami', (req: $RequestExtend, res: Response, next: $NextFunctionVer): void => {
|
2020-10-03 05:47:04 -07:00
|
|
|
debug('whoami: reditect');
|
2021-03-14 08:42:46 +01:00
|
|
|
if (req.headers.referer === 'whoami') {
|
2020-10-03 05:47:04 -07:00
|
|
|
const username = req.remote_user.name;
|
|
|
|
// FIXME: this service should return 401 if user missing
|
|
|
|
// if (!username) {
|
|
|
|
// debug('whoami: user not found');
|
|
|
|
// return next(getUnauthorized('Unauthorized'));
|
|
|
|
// }
|
|
|
|
debug('whoami: logged by user');
|
|
|
|
return next({ username: username });
|
2021-03-14 08:42:46 +01:00
|
|
|
} else {
|
2020-10-03 05:47:04 -07:00
|
|
|
debug('whoami: redirect next route');
|
|
|
|
// redirect to the route below
|
|
|
|
return next('route');
|
2019-07-16 08:40:01 +02:00
|
|
|
}
|
2021-03-14 08:42:46 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
route.get('/-/whoami', (req: $RequestExtend, res: Response, next: $NextFunctionVer): any => {
|
2020-10-03 05:47:04 -07:00
|
|
|
const username = req.remote_user.name;
|
|
|
|
// FIXME: this service should return 401 if user missing
|
|
|
|
// if (!username) {
|
|
|
|
// debug('whoami: user not found');
|
|
|
|
// return next(getUnauthorized('Unauthorized'));
|
|
|
|
// }
|
|
|
|
|
|
|
|
debug('whoami: response %o', username);
|
|
|
|
return next({ username: username });
|
2021-03-14 08:42:46 +01:00
|
|
|
});
|
2019-07-16 08:40:01 +02:00
|
|
|
}
|