Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
proxy-manager-backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
aolda
proxy-manager-backend
Commits
caac306f
Commit
caac306f
authored
4 months ago
by
천 진강
Browse files
Options
Downloads
Patches
Plain Diff
feat: 어드민 권한 확인 로직 추가
parent
cf23a243
No related branches found
No related tags found
4 merge requests
!15
Feat/certificate
,
!6
Feat/forwarding 포트포워딩 CRUD
,
!5
Feat/auth 어드민 권한 확인 로직, 개인 토큰 사용하도록 변경
,
!4
feat: 어드민 권한 확인 로직 추가
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/com/aolda/itda/dto/auth/LoginResponseDTO.java
+1
-1
1 addition, 1 deletion
src/main/java/com/aolda/itda/dto/auth/LoginResponseDTO.java
src/main/java/com/aolda/itda/service/AuthService.java
+77
-6
77 additions, 6 deletions
src/main/java/com/aolda/itda/service/AuthService.java
with
78 additions
and
7 deletions
src/main/java/com/aolda/itda/dto/auth/LoginResponseDTO.java
+
1
−
1
View file @
caac306f
...
@@ -13,5 +13,5 @@ import java.util.List;
...
@@ -13,5 +13,5 @@ import java.util.List;
@Builder
@Builder
public
class
LoginResponseDTO
{
public
class
LoginResponseDTO
{
private
Boolean
isAdmin
;
private
Boolean
isAdmin
;
private
List
<
ProjectIdAndNameDTO
>
lis
ts
;
private
List
<
ProjectIdAndNameDTO
>
projec
ts
;
}
}
This diff is collapsed.
Click to expand it.
src/main/java/com/aolda/itda/service/AuthService.java
+
77
−
6
View file @
caac306f
...
@@ -38,15 +38,16 @@ public class AuthService {
...
@@ -38,15 +38,16 @@ public class AuthService {
String
userId
=
user
.
get
(
"id"
);
String
userId
=
user
.
get
(
"id"
);
String
token
=
user
.
get
(
"token"
);
String
token
=
user
.
get
(
"token"
);
String
systemToken
=
getSystemToken
(
userId
,
loginRequestDTO
.
getPassword
());
if
(
userId
==
null
||
token
==
null
)
{
if
(
userId
==
null
||
token
==
null
)
{
throw
new
CustomException
(
ErrorCode
.
INVALID_USER_INFO
);
throw
new
CustomException
(
ErrorCode
.
INVALID_USER_INFO
);
}
}
response
.
addHeader
(
"X-Subject-Token"
,
token
);
response
.
addHeader
(
"X-Subject-Token"
,
systemToken
!=
null
?
systemToken
:
token
);
return
LoginResponseDTO
.
builder
()
return
LoginResponseDTO
.
builder
()
.
isAdmin
(
false
)
.
isAdmin
(
systemToken
!=
null
)
.
lis
ts
(
getProjectsWithUser
(
user
))
.
projec
ts
(
getProjectsWithUser
(
user
))
.
build
();
.
build
();
}
}
...
@@ -88,6 +89,50 @@ public class AuthService {
...
@@ -88,6 +89,50 @@ public class AuthService {
"token"
,
token
);
"token"
,
token
);
}
}
private
String
getSystemToken
(
String
id
,
String
password
)
{
String
url
=
keystone
+
"/auth/tokens"
;
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
MediaType
.
APPLICATION_JSON
);
String
requestBody
=
"{\n"
+
" \"auth\": {\n"
+
" \"identity\": {\n"
+
" \"methods\": [\n"
+
" \"password\"\n"
+
" ],\n"
+
" \"password\": {\n"
+
" \"user\": {\n"
+
" \"id\": \""
+
id
+
"\",\n"
+
" \"password\": \""
+
password
+
"\"\n"
+
" }\n"
+
" }\n"
+
" },\n"
+
" \"scope\": {\n"
+
" \"system\": {\n"
+
" \"all\": true\n"
+
" }\n"
+
" }\n"
+
" }\n"
+
"}"
;
HttpEntity
<
String
>
requestEntity
;
ResponseEntity
<
Map
>
res
;
try
{
requestEntity
=
new
HttpEntity
<>(
requestBody
,
headers
);
res
=
restTemplate
.
postForEntity
(
url
,
requestEntity
,
Map
.
class
);
}
catch
(
RuntimeException
e
)
{
return
null
;
}
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
(
...
@@ -107,7 +152,7 @@ public class AuthService {
...
@@ -107,7 +152,7 @@ public class AuthService {
String
url
=
keystone
+
"/role_assignments?user.id="
+
userId
+
"&effective&include_names=true&scope.project.id="
+
projectId
;
String
url
=
keystone
+
"/role_assignments?user.id="
+
userId
+
"&effective&include_names=true&scope.project.id="
+
projectId
;
HttpHeaders
headers
=
new
HttpHeaders
();
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
set
(
"X-Auth-Token"
,
getAdminT
oken
()
);
headers
.
set
(
"X-Auth-Token"
,
t
oken
);
HttpEntity
<
String
>
requestEntity
=
new
HttpEntity
<>(
headers
);
HttpEntity
<
String
>
requestEntity
=
new
HttpEntity
<>(
headers
);
ResponseEntity
<
String
>
res
=
restTemplate
.
exchange
(
url
,
HttpMethod
.
GET
,
requestEntity
,
String
.
class
);
ResponseEntity
<
String
>
res
=
restTemplate
.
exchange
(
url
,
HttpMethod
.
GET
,
requestEntity
,
String
.
class
);
...
@@ -151,7 +196,7 @@ public class AuthService {
...
@@ -151,7 +196,7 @@ public class AuthService {
String
url
=
keystone
+
"/users/"
+
userId
+
"/projects"
;
String
url
=
keystone
+
"/users/"
+
userId
+
"/projects"
;
HttpHeaders
headers
=
new
HttpHeaders
();
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
set
(
"X-Auth-Token"
,
getAdminT
oken
()
);
headers
.
set
(
"X-Auth-Token"
,
t
oken
);
HttpEntity
<
String
>
requestEntity
=
new
HttpEntity
<>(
headers
);
HttpEntity
<
String
>
requestEntity
=
new
HttpEntity
<>(
headers
);
ResponseEntity
<
String
>
res
=
restTemplate
.
exchange
(
url
,
HttpMethod
.
GET
,
requestEntity
,
String
.
class
);
ResponseEntity
<
String
>
res
=
restTemplate
.
exchange
(
url
,
HttpMethod
.
GET
,
requestEntity
,
String
.
class
);
...
@@ -172,7 +217,7 @@ public class AuthService {
...
@@ -172,7 +217,7 @@ public class AuthService {
private
String
validateTokenAndGetUserId
(
String
token
)
throws
JsonProcessingException
{
private
String
validateTokenAndGetUserId
(
String
token
)
throws
JsonProcessingException
{
String
url
=
keystone
+
"/auth/tokens"
;
String
url
=
keystone
+
"/auth/tokens"
;
HttpHeaders
headers
=
new
HttpHeaders
();
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
set
(
"X-Auth-Token"
,
getAdminT
oken
()
);
headers
.
set
(
"X-Auth-Token"
,
t
oken
);
headers
.
set
(
"X-Subject-Token"
,
token
);
headers
.
set
(
"X-Subject-Token"
,
token
);
HttpEntity
<
String
>
requestEntity
=
new
HttpEntity
<>(
headers
);
HttpEntity
<
String
>
requestEntity
=
new
HttpEntity
<>(
headers
);
ResponseEntity
<
String
>
res
;
ResponseEntity
<
String
>
res
;
...
@@ -184,4 +229,30 @@ public class AuthService {
...
@@ -184,4 +229,30 @@ public class AuthService {
return
objectMapper
.
readTree
(
res
.
getBody
()).
path
(
"token"
).
path
(
"user"
).
path
(
"id"
).
asText
();
return
objectMapper
.
readTree
(
res
.
getBody
()).
path
(
"token"
).
path
(
"user"
).
path
(
"id"
).
asText
();
}
}
private
Boolean
isAdmin
(
Map
<
String
,
String
>
user
)
throws
JsonProcessingException
{
String
url
=
keystone
+
"/role_assignments?user.id="
+
user
.
get
(
"id"
)
+
"&scope.system&include_names"
;
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
set
(
"X-Auth-Token"
,
user
.
get
(
"token"
));
HttpEntity
<
String
>
requestEntity
=
new
HttpEntity
<>(
headers
);
ResponseEntity
<
String
>
res
;
try
{
res
=
restTemplate
.
exchange
(
url
,
HttpMethod
.
GET
,
requestEntity
,
String
.
class
);
}
catch
(
RuntimeException
e
)
{
e
.
printStackTrace
();
System
.
out
.
println
(
"runtime"
);
return
false
;
}
JsonNode
node
=
objectMapper
.
readTree
(
res
.
getBody
()).
path
(
"role_assignments"
);
String
system_all
=
node
.
path
(
"scope"
).
path
(
"system"
).
path
(
"all"
).
asText
();
String
role
=
node
.
path
(
"role"
).
path
(
"name"
).
asText
();
System
.
out
.
println
(
"role: "
+
role
);
if
(
system_all
.
equals
(
"true"
)
&&
role
.
equals
(
"admin"
))
{
System
.
out
.
println
(
system_all
);
return
true
;
}
System
.
out
.
println
(
"hi"
);
return
false
;
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment