2015-11-13 23:38:55 +03:00
|
|
|
//
|
2019-10-03 04:41:16 +03:00
|
|
|
// Copyright (c) Microsoft.
|
2015-11-13 23:38:55 +03:00
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
//
|
|
|
|
|
2021-03-06 01:50:31 +03:00
|
|
|
import logger from 'morgan';
|
2021-03-19 19:20:39 +03:00
|
|
|
|
2021-04-10 02:36:15 +03:00
|
|
|
import { ReposAppRequest } from '../interfaces';
|
2024-01-03 22:44:13 +03:00
|
|
|
import { getProviders } from '../lib/transitional';
|
2015-11-13 23:38:55 +03:00
|
|
|
|
2016-07-26 23:12:51 +03:00
|
|
|
const encryptionMetadataKey = '_ClientEncryptionMetadata2';
|
2022-10-07 09:59:30 +03:00
|
|
|
const piiFormat =
|
|
|
|
':id :method :scrubbedUrl :status :response-time ms - :res[content-length] :encryptedSession :correlationId';
|
|
|
|
const format =
|
|
|
|
':method :scrubbedUrl :status :response-time ms - :res[content-length] :encryptedSession :correlationId';
|
2016-07-26 23:12:51 +03:00
|
|
|
|
2021-03-06 01:50:31 +03:00
|
|
|
logger.token('encryptedSession', function getUserId(req: ReposAppRequest) {
|
2022-10-07 09:59:30 +03:00
|
|
|
const config = getProviders(req).config;
|
2021-03-06 01:50:31 +03:00
|
|
|
if (req.session) {
|
|
|
|
const sessionPassport = (req.session as any).passport;
|
|
|
|
if (sessionPassport && sessionPassport.user) {
|
2022-10-08 01:25:28 +03:00
|
|
|
const userType = config.authentication.scheme === 'aad' ? 'azure' : 'github';
|
2022-10-07 09:59:30 +03:00
|
|
|
return sessionPassport.user[userType] &&
|
|
|
|
sessionPassport.user[userType][encryptionMetadataKey] !== undefined
|
|
|
|
? 'encrypted'
|
|
|
|
: 'plain';
|
2021-03-06 01:50:31 +03:00
|
|
|
}
|
2016-07-26 23:12:51 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-06 01:50:31 +03:00
|
|
|
logger.token('id', function getUserId(req: ReposAppRequest) {
|
2022-10-07 09:59:30 +03:00
|
|
|
const config = getProviders(req).config;
|
2016-07-20 09:47:41 +03:00
|
|
|
if (config) {
|
2022-10-08 01:25:28 +03:00
|
|
|
const userType = config.authentication.scheme === 'aad' ? 'azure' : 'github';
|
2022-10-07 09:59:30 +03:00
|
|
|
return req.user && req.user[userType] && req.user[userType].username
|
|
|
|
? req.user[userType].username
|
|
|
|
: undefined;
|
2016-07-20 09:47:41 +03:00
|
|
|
}
|
2015-11-13 23:38:55 +03:00
|
|
|
});
|
|
|
|
|
2021-03-06 01:50:31 +03:00
|
|
|
logger.token('correlationId', function getCorrelationId(req: ReposAppRequest) {
|
2016-07-14 21:00:31 +03:00
|
|
|
return req.correlationId;
|
2015-11-13 23:38:55 +03:00
|
|
|
});
|
|
|
|
|
2021-03-06 01:50:31 +03:00
|
|
|
logger.token('scrubbedUrl', function getScrubbedUrl(req: ReposAppRequest) {
|
2016-07-14 10:38:47 +03:00
|
|
|
return req.scrubbedUrl || req.originalUrl || req.url;
|
2015-11-13 23:38:55 +03:00
|
|
|
});
|
|
|
|
|
2021-03-06 01:50:31 +03:00
|
|
|
export default function createLogger(config) {
|
2022-10-08 01:25:28 +03:00
|
|
|
return logger(config && config.debug && config.debug.showUsers === true ? piiFormat : format);
|
2022-10-07 09:59:30 +03:00
|
|
|
}
|