Revize e5500e52
Přidáno uživatelem Jiri Trefil před asi 2 roky(ů)
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/UserController.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.dials.UserModelStatusCodes; |
4 | 4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User; |
5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.OAuthService; |
|
5 | 6 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.UserService; |
6 | 7 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder; |
7 | 8 |
import org.springframework.beans.factory.annotation.Autowired; |
8 | 9 |
import org.springframework.http.HttpStatus; |
9 | 10 |
import org.springframework.http.ResponseEntity; |
11 |
import org.springframework.security.authentication.AuthenticationManager; |
|
10 | 12 |
import org.springframework.web.bind.annotation.*; |
11 | 13 |
|
12 | 14 |
import java.util.HashMap; |
... | ... | |
21 | 23 |
@RequestMapping("v2/user") |
22 | 24 |
public class UserController { |
23 | 25 |
|
26 |
@Autowired |
|
27 |
private AuthenticationManager authenticationManager; |
|
28 |
|
|
24 | 29 |
/** |
25 | 30 |
* Service for work with user management |
26 | 31 |
*/ |
27 | 32 |
@Autowired |
28 | 33 |
private UserService userService; |
29 | 34 |
|
35 |
@Autowired |
|
36 |
private OAuthService authService; |
|
37 |
|
|
30 | 38 |
/** |
31 | 39 |
* Method to register new user in the app |
32 | 40 |
* |
... | ... | |
47 | 55 |
*/ |
48 | 56 |
@PostMapping(value = "/login") |
49 | 57 |
public ResponseEntity<String> loginUser(@RequestBody User user) { |
50 |
UserModelStatusCodes statusCode = userService.loginUser(user); |
|
58 |
UserModelStatusCodes statusCode = userService.verifyUser(user); |
|
59 |
ResponseEntity<String> response = authService.loginUser(user); |
|
51 | 60 |
|
52 | 61 |
String jwtToken = null; |
53 | 62 |
if (statusCode.getStatusCode() == 200) { |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/User.java | ||
---|---|---|
16 | 16 |
*/ |
17 | 17 |
@Transient |
18 | 18 |
private static final int MAX_COLUMN_LENGTH = 255; |
19 |
|
|
20 |
/** |
|
21 |
* Max length of column in the table |
|
22 |
*/ |
|
23 |
@Transient |
|
24 |
private String token; |
|
25 |
|
|
19 | 26 |
/** |
20 | 27 |
* Unique key of table |
21 | 28 |
*/ |
... | ... | |
96 | 103 |
return MAX_COLUMN_LENGTH; |
97 | 104 |
} |
98 | 105 |
|
106 |
/** |
|
107 |
* Getter for password of user |
|
108 |
* @return user password |
|
109 |
*/ |
|
99 | 110 |
public String getPassword() { |
100 | 111 |
return this.password; |
101 | 112 |
} |
113 |
|
|
114 |
/** |
|
115 |
* Getter for token of user |
|
116 |
* @return user token |
|
117 |
*/ |
|
118 |
public String getToken() { |
|
119 |
return this.token; |
|
120 |
} |
|
121 |
|
|
122 |
/** |
|
123 |
* Setter of user token |
|
124 |
* @param token user token |
|
125 |
*/ |
|
126 |
public void setToken(String token) { |
|
127 |
this.token = token; |
|
128 |
} |
|
102 | 129 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/UserService.java | ||
---|---|---|
20 | 20 |
* @param user serialized JSON object representing user |
21 | 21 |
* @return Integer - status code of the operation, ie 1 - successful, 0 - failed, .... |
22 | 22 |
*/ |
23 |
public UserModelStatusCodes loginUser(User user);
|
|
23 |
public UserModelStatusCodes verifyUser(User user);
|
|
24 | 24 |
/** |
25 | 25 |
* Method attempts to log out a user |
26 | 26 |
* @param user serialized JSON object representing user |
... | ... | |
30 | 30 |
|
31 | 31 |
public User getUserByName(String name); |
32 | 32 |
|
33 |
|
|
34 | 33 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/UserServiceImpl.java | ||
---|---|---|
54 | 54 |
//save the user |
55 | 55 |
User u = userRepository.save(new User(name, email, passwordHash)); |
56 | 56 |
//database insert failed for some reason |
57 |
if(u == null) |
|
57 |
if(u == null) {
|
|
58 | 58 |
return UserModelStatusCodes.USER_CREATION_FAILED; |
59 |
} |
|
60 |
|
|
59 | 61 |
//TODO request to OAuth for token - send user info to the oauth app for token |
60 | 62 |
//return okay status code, the user was created |
61 | 63 |
return UserModelStatusCodes.USER_CREATED; |
... | ... | |
90 | 92 |
private String hashPassword(String password) { |
91 | 93 |
//standard java security encryption module |
92 | 94 |
MessageDigest digest = null; |
93 |
try{ |
|
95 |
try {
|
|
94 | 96 |
//try to instance the class - throws an error if algorithm |
95 | 97 |
digest = MessageDigest.getInstance("SHA3-256"); |
96 | 98 |
} |
... | ... | |
112 | 114 |
*/ |
113 | 115 |
|
114 | 116 |
@Override |
115 |
public UserModelStatusCodes loginUser(User user) {
|
|
117 |
public UserModelStatusCodes verifyUser(User user) {
|
|
116 | 118 |
final String name = user.getName(); |
117 | 119 |
final String password = user.getPassword(); |
118 | 120 |
if(name == null || password == null) { |
... | ... | |
135 | 137 |
* @param hash String hash saved in database |
136 | 138 |
* @return true if hashes are the same |
137 | 139 |
*/ |
138 |
boolean comparePassword(String password, String hash){ |
|
140 |
boolean comparePassword(String password, String hash) {
|
|
139 | 141 |
final String passwordHash = this.hashPassword(password); |
140 | 142 |
return hash.equals(passwordHash); |
141 | 143 |
} |
Také k dispozici: Unified diff
#10244 implementace komunikace s autentizacni aplikaci.