1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
/*
* SonarQube
* Copyright (C) 2009-2022 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { getAllValues } from '../api/settings';
import { SettingsKey } from '../types/settings';
import { TokenExpiration, UserToken } from '../types/token';
import { now, toShortNotSoISOString } from './dates';
import { translate } from './l10n';
export const EXPIRATION_OPTIONS = [
TokenExpiration.OneMonth,
TokenExpiration.ThreeMonths,
TokenExpiration.OneYear,
TokenExpiration.NoExpiration,
].map((value) => {
return {
value,
label: translate('users.tokens.expiration', value.toString()),
};
});
const SETTINGS_EXPIRATION_MAP: { [key: string]: TokenExpiration } = {
'30 days': TokenExpiration.OneMonth,
'90 days': TokenExpiration.ThreeMonths,
'1 year': TokenExpiration.OneYear,
'No expiration': TokenExpiration.NoExpiration,
};
export async function getAvailableExpirationOptions() {
/*
* We intentionally fetch all settings, because fetching a specific setting will
* return it from the DB as a fallback, even if the setting is not defined at startup.
*/
const setting = (await getAllValues()).find((v) => v.key === SettingsKey.TokenMaxAllowedLifetime);
if (setting === undefined || setting.value === undefined) {
return EXPIRATION_OPTIONS;
}
const maxTokenLifetime = setting.value;
if (SETTINGS_EXPIRATION_MAP[maxTokenLifetime] !== TokenExpiration.NoExpiration) {
return EXPIRATION_OPTIONS.filter(
(option) =>
option.value <= SETTINGS_EXPIRATION_MAP[maxTokenLifetime] &&
option.value !== TokenExpiration.NoExpiration
);
}
return EXPIRATION_OPTIONS;
}
export function computeTokenExpirationDate(days: number) {
const expirationDate = now();
expirationDate.setDate(expirationDate.getDate() + days);
return toShortNotSoISOString(expirationDate);
}
export function getNextTokenName(tokenNameBase: string, tokens: UserToken[]) {
let tokenName = tokenNameBase;
let counter = 1;
let existingToken = tokens.find(({ name }) => name === tokenName);
while (existingToken !== undefined) {
tokenName = `${tokenNameBase}-${counter}`;
counter += 1;
// eslint-disable-next-line no-loop-func
existingToken = tokens.find(({ name }) => name === tokenName);
}
return tokenName;
}
|