Skip to content

Commit

Permalink
Merge pull request #70 from vicheanath/refresh-token
Browse files Browse the repository at this point in the history
Add refresh token
  • Loading branch information
DBunthai authored Feb 9, 2024
2 parents 03cc296 + 518a1e3 commit 0a43f1b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 18 deletions.
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);
}


}

0 comments on commit 0a43f1b

Please sign in to comment.