-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.controller.ts
203 lines (193 loc) · 6.67 KB
/
auth.controller.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { Request, Response } from "express";
import { User, UserClass } from '../models/user.model';
import { UserModule } from "../modules/entities/user.module";
import { MongoModule } from "../modules/mongo/mongo.module";
import { jwtkey, expirationTime } from "../config/constants";
import jwt from "jsonwebtoken";
import { hashString } from "../modules/auth";
import { LDAPModule} from "../modules/auth/ldap.module";
import mongoose from "mongoose";
import { printToConsole } from "../modules/util/util.module";
import config from 'config';
export class AuthController {
userModule: UserModule;
ldapModule: LDAPModule;
constructor(mongo: MongoModule, ldap: LDAPModule ){
this.userModule = new UserModule(mongo);
this.ldapModule = ldap;
}
public loginLDAP(req: Request, res: Response): void {
if (config.get('disableLDAP') == "true"){
this.login(req, res);
return
}
const user = req.body.name.trim()
this.ldapModule.verify(user.name, user.password).then(isValid => {
if (!isValid) {
this.login(req, res)
} else {
const token = jwt.sign({"name": user.name}, jwtkey, {
expiresIn: expirationTime
})
res.status(200)
res.send({
"method": "ldap",
"token": token
})
}
}).catch((err:any) => {
printToConsole(err)
this.login(req, res)
})
}
/**
* This function handels the passed data by the User
* while login process
* @param {Request} req data passed in by user
* @param {Response} res data send back to user
*/
public login(req: Request, res: Response): void {
const jsonUser = req.body.name
if (!jsonUser || jsonUser == ""){
res.status(400).send("Username missing")
return
}
const password = req.body.password
if (!password || password == ""){
res.status(400).send("Password missing")
return
}
this.userModule.getUserByName(jsonUser)
.then((user) => {
if (user === null ) {
res.status(403).send("Wrong Credentials")
return
} else {
user._id && this.userModule.removeJWT(user._id)
const salt: string = user.salt!
hashString.encode(password.trim(), salt)
.then((generatedHash) => {
if (generatedHash == user.password) {
const token = jwt.sign({"name": jsonUser}, jwtkey, {
expiresIn: expirationTime
})
res.status(200)
res.send({
"method": "local",
"token": token,
"role" : user.role
})
} else {
res.status(403)
res.send({
"message": "Password wrong",
})
}
})
}
})
.catch((err:any) => {
printToConsole(err)
res.status(403)
res.send({
"message": "Wrong Credentials",
})
})
}
/**
* This function verifies the sent data for
* a registration and creates a new database
* entry if the data is correct.
* @param {Request} req data send by the user
* @param {Response} res data send to the user back
*/
public register(req: Request, res: Response): void {
const salt = hashString.getSalt()
const userName = req.body.name
if (!userName || userName == ""){
res.status(400).send("Username missing")
return
}
const password = req.body.password
if (!password || password == ""){
res.status(400).send("Password missing")
return
}
hashString.encode(password.trim(), salt).then((passwordHash) => {
this.userModule.createUser(
new UserClass(
userName.trim(),
passwordHash,
"user",
salt,
req.body.firstname,
req.body.lastname,
req.body.email,
req.body.searchColor,
req.body.language
)
).then((id) => {
res.status(201).send(id)
}).catch((err: Error) => {
res.status(409)
res.send({
"message" : {
"error" : err.message
}
})
})
})
}
/**
* This function deletes the sessionCookie so that the user
* is not recognised by the Backend anymore
* @param {Request} req data recieved from the user
* @param {Response} res data send back to the user
*/
public async logout(req: Request, res: Response): Promise<void> {
const authHeader = req.headers.authorization
authHeader && jwt.verify(authHeader, jwtkey, async (err: jwt.VerifyErrors | null, decoded: any) => {
if(err) {
res
.status(401)
.send({
success: false,
message: err.message
})
return
}
let userObj: User | null = await this.userModule.getUserByName(decoded.name);
if(userObj) {
printToConsole(authHeader)
const updatedUserObj = userObj
updatedUserObj.jwt = authHeader
printToConsole(updatedUserObj)
userObj._id && await this.userModule.updateUser(userObj._id, updatedUserObj);
res
.status(200)
.send({
'message': 'logged out successfully'
})
} else {
res
.status(400)
.send({
'message': 'error in logging out'
})
}
})
}
/**
* Deletes the user
*/
public delete(req: Request, res: Response){
if(!(req.params.id)){
return res.status(400).send("no id given")
}
return this.userModule.deleteUser(new mongoose.Types.ObjectId(req.params.id)).then((user: User| null)=>
res.status(200).send(user)
).catch((err)=>{
res.status(500).send(err);
})
}
}