-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathlocal.ts
More file actions
131 lines (118 loc) · 3.82 KB
/
local.ts
File metadata and controls
131 lines (118 loc) · 3.82 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Copyright 2026 GitProxy Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import bcrypt from 'bcryptjs';
import { IVerifyOptions, Strategy as LocalStrategy } from 'passport-local';
import type { PassportStatic } from 'passport';
import * as db from '../../db';
import type { DefaultLocalUser } from './types';
export const type = 'local';
const DEFAULT_LOCAL_USERS: DefaultLocalUser[] = [
{
username: 'admin',
password: 'admin',
email: '[email protected]',
gitAccount: 'none',
admin: true,
},
{
username: 'user',
password: 'user',
email: '[email protected]',
gitAccount: 'none',
admin: false,
},
];
const isProduction = (): boolean => process.env.NODE_ENV === 'production';
const isKnownDefaultCredentialAttempt = (username: string, password: string): boolean =>
DEFAULT_LOCAL_USERS.some(
(defaultUser) =>
defaultUser.username.toLowerCase() === username.toLowerCase() &&
defaultUser.password === password,
);
// Dynamic import to always get the current db module instance
// This is necessary for test environments where modules may be reset
const getDb = () => import('../../db');
export const configure = async (passport: PassportStatic): Promise<PassportStatic> => {
passport.use(
new LocalStrategy(
async (
username: string,
password: string,
done: (err: unknown, user?: Partial<db.User>, info?: IVerifyOptions) => void,
) => {
try {
const dbModule = await getDb();
const user = await dbModule.findUser(username);
if (!user) {
return done(null, undefined, { message: 'Incorrect username.' });
}
const passwordCorrect = await bcrypt.compare(password, user.password ?? '');
if (!passwordCorrect) {
return done(null, undefined, { message: 'Incorrect password.' });
}
// Force password reset when using default accounts in production
if (
isProduction() &&
isKnownDefaultCredentialAttempt(username, password) &&
!user.mustChangePassword
) {
user.mustChangePassword = true;
await dbModule.updateUser({
username: user.username,
mustChangePassword: true,
});
}
return done(null, user);
} catch (error: unknown) {
return done(error);
}
},
),
);
passport.serializeUser((user: Partial<db.User>, done) => {
done(null, user.username);
});
passport.deserializeUser(async (username: string, done) => {
try {
const dbModule = await getDb();
const user = await dbModule.findUser(username);
done(null, user);
} catch (error: unknown) {
done(error, null);
}
});
return passport;
};
/**
* Create the default admin and regular test users.
*/
export const createDefaultAdmin = async () => {
const createIfNotExists = async (
username: string,
password: string,
email: string,
type: string,
isAdmin: boolean,
) => {
const user = await db.findUser(username);
if (!user) {
await db.createUser(username, password, email, type, isAdmin, '', isProduction());
}
};
for (const u of DEFAULT_LOCAL_USERS) {
await createIfNotExists(u.username, u.password, u.email, u.gitAccount, u.admin);
}
};