Skip to content

Commit

Permalink
Merge pull request #8 from fine-ants/feat/#4-openid
Browse files Browse the repository at this point in the history
[feat] final 키워드 추가
  • Loading branch information
yonghwankim-dev authored Nov 13, 2023
2 parents 7e9e8c3 + 4aef76b commit ae05b3d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,50 @@
public class AuthorizationCodeRandomGenerator {

public AuthorizationRequest generateAuthorizationRequest() {
String codeVerifier = generateCodeVerifier();
String codeChallenge = generateCodeChallenge(codeVerifier);
final String state = generateState();
final String codeVerifier = generateCodeVerifier();
final String codeChallenge = generateCodeChallenge(codeVerifier);
final String nonce = generateNonce();
return AuthorizationRequest.of(
generateState(),
state,
codeVerifier,
codeChallenge,
generateNonce()
nonce
);
}

public String generateState() {
SecureRandom secureRandom = new SecureRandom();
final SecureRandom secureRandom = new SecureRandom();
return new BigInteger(130, secureRandom).toString();
}

public String generateCodeVerifier() {
SecureRandom secureRandom = new SecureRandom();
byte[] codeVerifier = new byte[32];
final SecureRandom secureRandom = new SecureRandom();
final byte[] codeVerifier = new byte[32];
secureRandom.nextBytes(codeVerifier);
return Base64.getUrlEncoder().withoutPadding().encodeToString(codeVerifier);
}

public String generateNonce() {
SecureRandom secureRandom = new SecureRandom();
byte[] randomBytes = new byte[16];
final SecureRandom secureRandom = new SecureRandom();
final byte[] randomBytes = new byte[16];
secureRandom.nextBytes(randomBytes);
BigInteger nonceValue = new BigInteger(1, randomBytes);
final BigInteger nonceValue = new BigInteger(1, randomBytes);
return nonceValue.toString(16); // 16진수 문자열로 반환
}

public String generateCodeChallenge(String codeVerifier) {
byte[] bytes = codeVerifier.getBytes(StandardCharsets.US_ASCII);
MessageDigest messageDigest;
public String generateCodeChallenge(final String codeVerifier) {
final byte[] bytes = codeVerifier.getBytes(StandardCharsets.US_ASCII);
final String algorithm = "SHA-256";
final MessageDigest messageDigest;
try {
messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest = MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
}
messageDigest.update(bytes, 0, bytes.length);
byte[] digest = messageDigest.digest();
final byte[] digest = messageDigest.digest();
return Base64.getUrlEncoder().withoutPadding().encodeToString(digest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public class InMemoryOauthClientRepository implements OauthClientRepository {
private final Map<String, OauthClient> oauthClientMap;

@Override
public OauthClient findOneBy(String providerName) {
public OauthClient findOneBy(final String providerName) {
if (providerName == null) {
throw new NotFoundResourceException(OauthErrorCode.NOT_FOUND_PROVIDER);
}
OauthClient oauthClient = oauthClientMap.get(providerName);
if (oauthClient == null) {
throw new NotFoundResourceException(OauthErrorCode.NOT_FOUND_PROVIDER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import codesquad.fineants.domain.oauth.client.OauthClient;

public interface OauthClientRepository {
OauthClient findOneBy(String providerName);
OauthClient findOneBy(final String providerName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,33 @@ public class MemberRestController {
private final MemberService memberService;

@PostMapping("/{provider}/url")
public ApiResponse<OauthCreateUrlResponse> authorizationCodeURL(@PathVariable String provider) {
public ApiResponse<OauthCreateUrlResponse> authorizationCodeURL(@PathVariable final String provider) {
return ApiResponse.success(OauthSuccessCode.OK_URL, memberService.createAuthorizationCodeURL(provider));
}

@PostMapping(value = "/{provider}/login")
public ApiResponse<OauthMemberLoginResponse> login(
@PathVariable String provider,
@RequestParam String code,
@RequestParam String redirectUrl,
@RequestParam String state) {
@PathVariable final String provider,
@RequestParam final String code,
@RequestParam final String redirectUrl,
@RequestParam final String state) {
return ApiResponse.success(OauthSuccessCode.OK_LOGIN,
memberService.login(provider, code, redirectUrl, state, LocalDateTime.now()));
}

@PostMapping(value = "/logout")
public ApiResponse<Void> logout(@RequestAttribute String accessToken,
@RequestBody OauthMemberLogoutRequest request) {
public ApiResponse<Void> logout(
@RequestAttribute final String accessToken,
@RequestBody final OauthMemberLogoutRequest request) {
memberService.logout(accessToken, request);
return ApiResponse.success(OauthSuccessCode.OK_LOGOUT);
}

@ResponseStatus(OK)
@PostMapping("/refresh/token")
public ApiResponse<OauthMemberRefreshResponse> refreshAccessToken(@RequestBody OauthMemberRefreshRequest request) {
OauthMemberRefreshResponse response = memberService.refreshAccessToken(request,
LocalDateTime.now());
public ApiResponse<OauthMemberRefreshResponse> refreshAccessToken(
@RequestBody final OauthMemberRefreshRequest request) {
OauthMemberRefreshResponse response = memberService.refreshAccessToken(request, LocalDateTime.now());
return ApiResponse.success(OauthSuccessCode.OK_REFRESH_TOKEN, response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ public OauthMemberRefreshResponse refreshAccessToken(OauthMemberRefreshRequest r
}

@Transactional(readOnly = true)
public OauthCreateUrlResponse createAuthorizationCodeURL(String provider) {
OauthClient oauthClient = oauthClientRepository.findOneBy(provider);
AuthorizationRequest request = authorizationCodeRandomGenerator.generateAuthorizationRequest();
public OauthCreateUrlResponse createAuthorizationCodeURL(final String provider) {
final OauthClient oauthClient = oauthClientRepository.findOneBy(provider);
final AuthorizationRequest request = authorizationCodeRandomGenerator.generateAuthorizationRequest();
authorizationRequestMap.put(request.getState(), request);
return new OauthCreateUrlResponse(
oauthClient.createAuthURL(request),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.function.Function;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
Expand Down Expand Up @@ -76,13 +75,4 @@ private <T> Function<ClientResponse, Mono<T>> getClientResponseMonoFunction(
return clientResponse.bodyToMono(reference);
};
}

@Cacheable(value = "publicKey")
public <T> T getPublicKeyList(String uri, ParameterizedTypeReference<T> reference) {
log.info("call getPublicKeyList");
return webClient.get()
.uri(uri)
.exchangeToMono(getClientResponseMonoFunction(reference))
.block();
}
}

0 comments on commit ae05b3d

Please sign in to comment.