Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
O
openebench-rest-api
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
1
Issues
1
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Packages
Packages
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
INB
ELIXIR
OpenEBench
openebench-rest-api
Commits
b8c0564e
Commit
b8c0564e
authored
Nov 05, 2020
by
redmitry@list.ru
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
put "roles" claim into KC Roles
parent
cd8c8bd4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
0 deletions
+70
-0
src/main/java/es/bsc/inb/elixir/openebench/rest/KeycloakRolesFilter.java
...s/bsc/inb/elixir/openebench/rest/KeycloakRolesFilter.java
+70
-0
No files found.
src/main/java/es/bsc/inb/elixir/openebench/rest/KeycloakRolesFilter.java
0 → 100644
View file @
b8c0564e
/**
* *****************************************************************************
* Copyright (C) 2020 ELIXIR ES, Spanish National Bioinformatics Institute (INB)
* and Barcelona Supercomputing Center (BSC)
*
* Modifications to the initial code base are copyright of their respective
* authors, or their employers as appropriate.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*****************************************************************************
*/
package
es.bsc.inb.elixir.openebench.rest
;
import
java.io.IOException
;
import
java.security.Principal
;
import
java.util.List
;
import
java.util.Map
;
import
javax.ws.rs.container.ContainerRequestContext
;
import
javax.ws.rs.container.ContainerRequestFilter
;
import
javax.ws.rs.core.SecurityContext
;
import
javax.ws.rs.ext.Provider
;
import
org.keycloak.KeycloakPrincipal
;
import
org.keycloak.KeycloakSecurityContext
;
import
org.keycloak.representations.AccessToken
;
/**
* Filter maps token's "roles" claim into the Keycloak Realm Roles, so
* isUserInRole() can work properly.
*
* @author Dmitry Repchevsky
*/
@Provider
public
class
KeycloakRolesFilter
implements
ContainerRequestFilter
{
@Override
public
void
filter
(
ContainerRequestContext
ctx
)
throws
IOException
{
final
SecurityContext
sc
=
ctx
.
getSecurityContext
();
if
(
sc
!=
null
)
{
final
Principal
principal
=
sc
.
getUserPrincipal
();
if
(
principal
instanceof
KeycloakPrincipal
)
{
final
KeycloakPrincipal
kp
=
(
KeycloakPrincipal
)
principal
;
final
KeycloakSecurityContext
ksc
=
kp
.
getKeycloakSecurityContext
();
final
AccessToken
token
=
ksc
.
getToken
();
final
AccessToken
.
Access
access
=
token
.
getRealmAccess
();
final
Map
<
String
,
Object
>
claims
=
token
.
getOtherClaims
();
final
List
roles
=
(
List
)
claims
.
get
(
"roles"
);
if
(
roles
!=
null
)
{
for
(
Object
role
:
roles
)
{
access
.
addRole
(
role
.
toString
());
}
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment