Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add refresh token #70

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws
)
.permitAll()

.requestMatchers(contextPath + "/auth/token/refresh")
.authenticated()
.requestMatchers(contextPath + "/admins/**")
.hasAuthority("Admin")
.requestMatchers(contextPath + "/owners/**")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.mini.pms.restcontroller;

import com.mini.pms.restcontroller.response.TokenResponse;
import com.mini.pms.service.AuthService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;


@RestController
@RequestMapping("api/v1/token/refresh")
@RequiredArgsConstructor
public class AuthRenewTokenRestController {

private final AuthService authService;

@PostMapping
public TokenResponse refresh(Principal principal) {

return authService.issueAccessToken(principal);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
import com.mini.pms.restcontroller.response.MemberResponse;
import com.mini.pms.restcontroller.response.TokenResponse;
import com.mini.pms.service.AuthService;

import com.mini.pms.service.MemberService;
import com.mini.pms.util.Util;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/v1/auth")
Expand All @@ -31,11 +34,6 @@ public TokenResponse token(@RequestBody AuthRequest authRequest) {
return authService.issueAccessToken(authRequest);
}

// @PostMapping("/token/refresh")
// public String refresh(@RequestBody AuthRequest authRequest) {
// return authService.issueAccessToken(authRequest);
// }

@PostMapping("owner/register")
@ResponseStatus(HttpStatus.CREATED)
public MemberResponse registerOwner(@RequestBody RegisterRequest registerRequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
import com.mini.pms.restcontroller.response.TokenResponse;
import org.springframework.security.core.Authentication;

import java.security.Principal;

public interface AuthService {
Member getAuthenticatedUser();
Authentication authenticate(String email, String password);

String createToken(AuthRequest authRequest, TokenType tokenType, long expired);
String createToken(Authentication auth, String email, TokenType tokenType, long expired);

TokenResponse issueAccessToken(AuthRequest authRequest);

TokenResponse issueAccessToken(Principal principal);

Member registerCustomer(RegisterRequest authRequest);

Member registerOwner(RegisterRequest authRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.util.*;

@Service
Expand Down Expand Up @@ -72,11 +73,11 @@ public Authentication authenticate(String email, String password) {
}

@Override
public String createToken(AuthRequest authRequest, TokenType tokenType, long expired) {
var email = authRequest.getEmail();
var password = authRequest.getPassword();
public String createToken(Authentication auth, String email, TokenType tokenType, long expired) {

var auth = authenticate(email, password);
if (!auth.isAuthenticated()) {
throw new PlatformException("Unauthorized user", HttpStatus.UNAUTHORIZED);
}

var now = new Date();
var expireAt = new Date(now.getTime());
Expand Down Expand Up @@ -105,9 +106,26 @@ public TokenResponse issueAccessToken(AuthRequest authRequest) {

var tokenRes = TokenResponse.builder();

tokenRes.accessToken(createToken(authRequest, TokenType.ACCESS_TOKEN, ACCESS_TOKEN_EXPIRED))
var email = authRequest.getEmail();
var password = authRequest.getPassword();
var auth = authenticate(email, password);
tokenRes.accessToken(createToken(auth, email, TokenType.ACCESS_TOKEN, ACCESS_TOKEN_EXPIRED))
.refreshToken(
createToken(auth, email, TokenType.REFRESH_TOKEN, REFRESH_TOKEN_EXPIRED));

return tokenRes.build();
}

@Override
public TokenResponse issueAccessToken(Principal principal) {

var auth = SecurityContextHolder.getContext().getAuthentication();

var tokenRes = TokenResponse.builder();

tokenRes.accessToken(createToken(auth, principal.getName(), TokenType.ACCESS_TOKEN, ACCESS_TOKEN_EXPIRED))
.refreshToken(
createToken(authRequest, TokenType.REFRESH_TOKEN, REFRESH_TOKEN_EXPIRED));
createToken(auth, principal.getName(), TokenType.REFRESH_TOKEN, REFRESH_TOKEN_EXPIRED));

return tokenRes.build();
}
Expand All @@ -132,16 +150,15 @@ public Member registerAdmin(RegisterRequest authRequest) {
}

private Member register(RegisterRequest authRequest) {
Role role = roleRepo.findByName(authRequest.getRole());
Role role = roleRepo.findByName(authRequest.getRole());
Member member = Member.builder()
.name(authRequest.getName())
.email(authRequest.getEmail())
.status(authRequest.getStatus())
.password(passwordEncoder.encode(authRequest.getPassword()))
.roles(List.of(role))
.build();
return memberRepo.save(member);
return memberRepo.save(member);
}


}
Loading