1
1
package com .example .polls .controller ;
2
2
3
3
import com .example .polls .exception .AppException ;
4
+ import com .example .polls .exception .BadRequestException ;
5
+ import com .example .polls .model .JwtRefreshToken ;
4
6
import com .example .polls .model .Role ;
5
7
import com .example .polls .model .RoleName ;
6
8
import com .example .polls .model .User ;
7
- import com .example .polls .payload .ApiResponse ;
8
- import com .example .polls .payload .JwtAuthenticationResponse ;
9
- import com .example .polls .payload .LoginRequest ;
10
- import com .example .polls .payload .SignUpRequest ;
9
+ import com .example .polls .payload .*;
10
+ import com .example .polls .repository .RefreshTokenRepository ;
11
11
import com .example .polls .repository .RoleRepository ;
12
12
import com .example .polls .repository .UserRepository ;
13
13
import com .example .polls .security .JwtTokenProvider ;
14
+ import com .example .polls .security .UserPrincipal ;
14
15
import org .springframework .beans .factory .annotation .Autowired ;
16
+ import org .springframework .beans .factory .annotation .Value ;
15
17
import org .springframework .http .HttpStatus ;
16
18
import org .springframework .http .ResponseEntity ;
17
19
import org .springframework .security .authentication .AuthenticationManager ;
27
29
28
30
import javax .validation .Valid ;
29
31
import java .net .URI ;
32
+ import java .time .Instant ;
33
+ import java .time .temporal .ChronoUnit ;
30
34
import java .util .Collections ;
31
35
32
36
/**
@@ -51,6 +55,12 @@ public class AuthController {
51
55
@ Autowired
52
56
JwtTokenProvider tokenProvider ;
53
57
58
+ @ Autowired
59
+ RefreshTokenRepository refreshTokenRepository ;
60
+
61
+ @ Value ("${app.jwtExpirationInMs}" )
62
+ private long jwtExpirationInMs ;
63
+
54
64
@ PostMapping ("/signin" )
55
65
public ResponseEntity <?> authenticateUser (@ Valid @ RequestBody LoginRequest loginRequest ) {
56
66
@@ -63,8 +73,35 @@ public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest login
63
73
64
74
SecurityContextHolder .getContext ().setAuthentication (authentication );
65
75
66
- String jwt = tokenProvider .generateToken (authentication );
67
- return ResponseEntity .ok (new JwtAuthenticationResponse (jwt ));
76
+ UserPrincipal userPrincipal = (UserPrincipal ) authentication .getPrincipal ();
77
+
78
+ String accessToken = tokenProvider .generateToken (userPrincipal );
79
+ String refreshToken = tokenProvider .generateRefreshToken ();
80
+
81
+ saveRefreshToken (userPrincipal , refreshToken );
82
+
83
+ return ResponseEntity .ok (new JwtAuthenticationResponse (accessToken , refreshToken , jwtExpirationInMs ));
84
+ }
85
+
86
+ @ PostMapping ("/refreshToken" )
87
+ public ResponseEntity <?> refreshAccessToken (@ Valid @ RequestBody RefreshTokenRequest refreshTokenRequest ) {
88
+ return refreshTokenRepository .findById (refreshTokenRequest .getRefreshToken ()).map (jwtRefreshToken -> {
89
+ User user = jwtRefreshToken .getUser ();
90
+ String accessToken = tokenProvider .generateToken (UserPrincipal .create (user ));
91
+ return ResponseEntity .ok (new JwtAuthenticationResponse (accessToken , jwtRefreshToken .getToken (), jwtExpirationInMs ));
92
+ }).orElseThrow (() -> new BadRequestException ("Invalid Refresh Token" ));
93
+ }
94
+
95
+ private void saveRefreshToken (UserPrincipal userPrincipal , String refreshToken ) {
96
+ // Persist Refresh Token
97
+
98
+ JwtRefreshToken jwtRefreshToken = new JwtRefreshToken (refreshToken );
99
+ jwtRefreshToken .setUser (userRepository .getOne (userPrincipal .getId ()));
100
+
101
+ Instant expirationDateTime = Instant .now ().plus (360 , ChronoUnit .DAYS ); // Todo Add this in application.properties
102
+ jwtRefreshToken .setExpirationDateTime (expirationDateTime );
103
+
104
+ refreshTokenRepository .save (jwtRefreshToken );
68
105
}
69
106
70
107
@ PostMapping ("/signup" )
0 commit comments