-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthService.java
78 lines (58 loc) · 2.42 KB
/
AuthService.java
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
package se.mow_e.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import se.mow_e.components.EncoderComponent;
@Service
public class AuthService {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private JwtService jwtService;
@Autowired
private UserDetailsManager manager;
@Autowired
private EncoderComponent encoder;
public String createToken(String username, String password){
// Authenticate user
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(username, password)
);
// Generate JWT token
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
return jwtService.generateToken(userDetails);
}
public String createUser(String username, String password, boolean isAdmin){
if(manager.userExists(username)){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "User exists");
}
User.UserBuilder user = User.builder()
.username(username)
.password(encoder.passwordEncoder().encode(password));
if(isAdmin){
user.roles("USER","ADMIN");
} else {
user.roles("USER");
}
UserDetails buildedUser = user.build();
manager.createUser(buildedUser);
return jwtService.generateToken(buildedUser);
}
public void deleteUser(String username) {
manager.deleteUser(username);
}
public boolean userExists(String username) {
return manager.userExists(username);
}
public UserDetails getUserByUsername(String username) throws UsernameNotFoundException {
return manager.loadUserByUsername(username);
}
}