/
auth.ts
103 lines (99 loc) · 2.84 KB
/
auth.ts
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
import { Service, Inject } from 'typedi';
import winston from 'winston';
import { createRandomString, getFileContentByName } from '../config/util';
import config from '../config';
import * as fs from 'fs';
import _ from 'lodash';
import jwt from 'jsonwebtoken';
@Service()
export default class AuthService {
constructor(@Inject('logger') private logger: winston.Logger) {}
public async login(payloads: {
username: string;
password: string;
ip: string;
address: string;
}): Promise<any> {
if (!fs.existsSync(config.authConfigFile)) {
return this.initAuthInfo();
}
let { username, password, ip, address } = payloads;
const content = fs.readFileSync(config.authConfigFile, 'utf8');
const timestamp = Date.now();
if (content) {
const {
username: cUsername,
password: cPassword,
retries = 0,
lastlogon,
lastip,
lastaddr,
} = JSON.parse(content);
if (
(cUsername === 'admin' && cPassword === 'adminadmin') ||
!cUsername ||
!cPassword
) {
return this.initAuthInfo();
}
if (retries > 2 && Date.now() - lastlogon < Math.pow(3, retries) * 1000) {
return {
code: 410,
message: `失败次数过多,请${Math.round(
(Math.pow(3, retries) * 1000 - Date.now() + lastlogon) / 1000,
)}秒后重试`,
data: Math.round(
(Math.pow(3, retries) * 1000 - Date.now() + lastlogon) / 1000,
),
};
}
if (username === cUsername && password === cPassword) {
const data = createRandomString(50, 100);
let token = jwt.sign({ data }, config.secret as any, {
expiresIn: 60 * 60 * 24 * 3,
algorithm: 'HS384',
});
fs.writeFileSync(
config.authConfigFile,
JSON.stringify({
...JSON.parse(content),
token,
lastlogon: timestamp,
retries: 0,
lastip: ip,
lastaddr: address,
}),
);
return { code: 200, data: { token, lastip, lastaddr, lastlogon } };
} else {
fs.writeFileSync(
config.authConfigFile,
JSON.stringify({
...JSON.parse(content),
retries: retries + 1,
lastlogon: timestamp,
lastip: ip,
lastaddr: address,
}),
);
return { code: 400, message: config.authError };
}
} else {
return this.initAuthInfo();
}
}
private initAuthInfo() {
const newPassword = createRandomString(16, 22);
fs.writeFileSync(
config.authConfigFile,
JSON.stringify({
username: 'admin',
password: newPassword,
}),
);
return {
code: 100,
message: '已初始化密码,请前往auth.json查看并重新登录',
};
}
}