|
@@ -0,0 +1,72 @@
|
|
|
|
|
+package com.example.plantsforyou.appuser;
|
|
|
|
|
+
|
|
|
|
|
+import com.auth0.jwt.JWT;
|
|
|
|
|
+import com.auth0.jwt.JWTVerifier;
|
|
|
|
|
+import com.auth0.jwt.algorithms.Algorithm;
|
|
|
|
|
+import com.auth0.jwt.interfaces.DecodedJWT;
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
+
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
+
|
|
|
|
|
+import static org.springframework.http.HttpHeaders.AUTHORIZATION;
|
|
|
|
|
+import static org.springframework.http.HttpStatus.FORBIDDEN;
|
|
|
|
|
+import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
|
|
|
|
|
+
|
|
|
|
|
+@RestController
|
|
|
|
|
+@RequestMapping("/api/v1/users")
|
|
|
|
|
+@RequiredArgsConstructor
|
|
|
|
|
+public class AppUserController {
|
|
|
|
|
+ private final AppUserService appUserService;
|
|
|
|
|
+
|
|
|
|
|
+ @GetMapping()
|
|
|
|
|
+ public ResponseEntity<List<AppUser>>getUsers(){ return ResponseEntity.ok().body(appUserService.getAppUsers()); }
|
|
|
|
|
+
|
|
|
|
|
+ @GetMapping("/token/refresh")
|
|
|
|
|
+ public void refreshToken(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
|
|
+ String authorizationHeader = request.getHeader(AUTHORIZATION);
|
|
|
|
|
+ if(authorizationHeader != null && authorizationHeader.startsWith("Bearer ")){
|
|
|
|
|
+ try{
|
|
|
|
|
+ String refresh_token = authorizationHeader.substring("Bearer ".length());
|
|
|
|
|
+ Algorithm algorithm = Algorithm.HMAC256("secret".getBytes());
|
|
|
|
|
+ JWTVerifier verifier = JWT.require(algorithm).build();
|
|
|
|
|
+ DecodedJWT decodedJWT = verifier.verify(refresh_token);
|
|
|
|
|
+ String username = decodedJWT.getSubject();
|
|
|
|
|
+ Optional<AppUser> opt = appUserService.getAppUser(username);
|
|
|
|
|
+ AppUser user;
|
|
|
|
|
+ if(opt.isPresent()){
|
|
|
|
|
+ user = opt.get();
|
|
|
|
|
+ String access_token = JWT.create()
|
|
|
|
|
+ .withSubject(user.getEmail())
|
|
|
|
|
+ .withExpiresAt(new Date(System.currentTimeMillis() + 10 * 60 * 1000)) //10 mins
|
|
|
|
|
+ .withIssuer(request.getRequestURL().toString())
|
|
|
|
|
+ .sign(algorithm);
|
|
|
|
|
+ Map<String, String> tokens = new HashMap<>();
|
|
|
|
|
+ tokens.put("access_token", access_token);
|
|
|
|
|
+ tokens.put("refresh_token", refresh_token);
|
|
|
|
|
+ response.setContentType(APPLICATION_JSON_VALUE);
|
|
|
|
|
+ new ObjectMapper().writeValue(response.getOutputStream(), tokens);
|
|
|
|
|
+ }
|
|
|
|
|
+ }catch (Exception exception){
|
|
|
|
|
+ response.setHeader("error", exception.getMessage());
|
|
|
|
|
+ response.setStatus(FORBIDDEN.value());
|
|
|
|
|
+// response.sendError(FORBIDDEN.value());
|
|
|
|
|
+ Map<String, String> error = new HashMap<>();
|
|
|
|
|
+ error.put("error_message", exception.getMessage());
|
|
|
|
|
+ response.setContentType(APPLICATION_JSON_VALUE);
|
|
|
|
|
+ new ObjectMapper().writeValue(response.getOutputStream(), error);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ else{
|
|
|
|
|
+ throw new RuntimeException("Refresh token is missing");
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|