Skip to content
Snippets Groups Projects
Commit 7320a6e1 authored by 천 진강's avatar 천 진강
Browse files

fix: project token으로 project role 확인하도록 수정

parent caac306f
Branches
No related tags found
3 merge requests!15Feat/certificate,!6Feat/forwarding 포트포워딩 CRUD,!5Feat/auth 어드민 권한 확인 로직, 개인 토큰 사용하도록 변경
...@@ -15,6 +15,7 @@ import org.springframework.beans.factory.annotation.Value; ...@@ -15,6 +15,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.util.*; import java.util.*;
...@@ -133,11 +134,59 @@ public class AuthService { ...@@ -133,11 +134,59 @@ public class AuthService {
return token; return token;
} }
private String getProjectToken(String unscopedToken, String projectId) {
String url = keystone + "/auth/tokens";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
String requestBody = "{\n" +
" \"auth\": {\n" +
" \"identity\": {\n" +
" \"methods\": [\n" +
" \"token\"\n" +
" ],\n" +
" \"token\": {\n" +
" \"id\": \"" + unscopedToken +"\"\n" +
" }\n" +
" },\n" +
" \"scope\": {\n" +
" \"project\": {\n" +
" \"id\": \""+ projectId +"\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
HttpEntity<String> requestEntity;
ResponseEntity<Map> res;
try {
requestEntity = new HttpEntity<>(requestBody, headers);
res = restTemplate.postForEntity(url, requestEntity, Map.class);
} catch (HttpClientErrorException.Forbidden e) {
return unscopedToken;
}
catch (RuntimeException e) {
e.printStackTrace();
throw new CustomException(ErrorCode.INVALID_TOKEN);
}
Map<String, Object> resToken = (Map<String, Object>) res.getBody().get("token");
Map<String, Object> resUser = (Map<String, Object>) resToken.get("user");
String userId = (String) resUser.get("id");
String token = res.getHeaders().getFirst("X-Subject-Token");
return token;
}
// 특정 사용자의 특정 프로젝트 내 최고 권한 반환 // 특정 사용자의 특정 프로젝트 내 최고 권한 반환
public Map<String, String> getBestRoleWithinProject(String token, String projectId) throws JsonProcessingException { public Map<String, String> getBestRoleWithinProject(String token, String projectId) throws JsonProcessingException {
return getBestRoleWithinProject(Map.of( return getBestRoleWithinProject(Map.of(
"id", validateTokenAndGetUserId(token), "id", validateTokenAndGetUserId(token),
"token", token), "token", getProjectToken(token, projectId)),
projectId); projectId);
} }
...@@ -224,6 +273,7 @@ public class AuthService { ...@@ -224,6 +273,7 @@ public class AuthService {
try { try {
res = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class); res = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
} catch (HttpClientErrorException.NotFound e) { } catch (HttpClientErrorException.NotFound e) {
System.out.println("validate");
throw new CustomException(ErrorCode.INVALID_TOKEN); throw new CustomException(ErrorCode.INVALID_TOKEN);
} }
return objectMapper.readTree(res.getBody()).path("token").path("user").path("id").asText(); return objectMapper.readTree(res.getBody()).path("token").path("user").path("id").asText();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment