forked from indigo-iam/iam
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3620ac
commit de574c8
Showing
69 changed files
with
749 additions
and
858 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
iam-login-service/src/main/java/it/infn/mw/iam/core/userinfo/IamUserInfoInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/** | ||
* Copyright (c) Istituto Nazionale di Fisica Nucleare (INFN). 2016-2019 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package it.infn.mw.iam.core.userinfo; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.mitre.openid.connect.model.UserInfo; | ||
import org.mitre.openid.connect.service.UserInfoService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.authentication.AuthenticationTrustResolver; | ||
import org.springframework.security.authentication.AuthenticationTrustResolverImpl; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.oauth2.provider.OAuth2Authentication; | ||
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonPrimitive; | ||
import com.google.gson.JsonSerializationContext; | ||
import com.google.gson.JsonSerializer; | ||
|
||
public class IamUserInfoInterceptor extends HandlerInterceptorAdapter { | ||
|
||
public static final String USERINFO_ATTR_NAME = "userInfo"; | ||
public static final String USERINFO_JSON_ATTR_NAME = "userInfoJson"; | ||
|
||
private final Gson gsonBuilder; | ||
private final UserInfoService userInfoService; | ||
private final AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl(); | ||
|
||
@Autowired | ||
public IamUserInfoInterceptor(UserInfoService userInfoService) { | ||
this.userInfoService = userInfoService; | ||
gsonBuilder = new GsonBuilder() | ||
.registerTypeHierarchyAdapter(GrantedAuthority.class, new JsonSerializer<GrantedAuthority>() { | ||
@Override | ||
public JsonElement serialize(GrantedAuthority src, Type typeOfSrc, | ||
JsonSerializationContext context) { | ||
return new JsonPrimitive(src.getAuthority()); | ||
} | ||
}) | ||
.create(); | ||
} | ||
|
||
private void resolveUserInfo(Authentication auth, HttpServletRequest request) { | ||
UserInfo user = userInfoService.getByUsername(auth.getName()); | ||
|
||
if (user != null) { | ||
request.setAttribute(USERINFO_ATTR_NAME, user); | ||
request.setAttribute(USERINFO_JSON_ATTR_NAME, user.toJson()); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) | ||
throws Exception { | ||
|
||
Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | ||
|
||
if (auth instanceof Authentication) { | ||
request.setAttribute("userAuthorities", gsonBuilder.toJson(auth.getAuthorities())); | ||
} | ||
|
||
if (!trustResolver.isAnonymous(auth)) { | ||
if (auth instanceof OAuth2Authentication) { | ||
|
||
OAuth2Authentication oauth = (OAuth2Authentication) auth; | ||
if (oauth.getUserAuthentication() != null) { | ||
resolveUserInfo(oauth.getUserAuthentication(), request); | ||
} | ||
} else if (auth != null && auth.getName() != null) { | ||
|
||
resolveUserInfo(auth, request); | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.