|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright 2017 The MITRE Corporation |
| 3 | + * and the MIT Internet Trust Consortium |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + *******************************************************************************/ |
| 17 | + |
| 18 | +package org.mitre.openid.connect.web; |
| 19 | + |
| 20 | +import java.text.ParseException; |
| 21 | + |
| 22 | +import javax.servlet.http.HttpServletRequest; |
| 23 | + |
| 24 | +import org.mitre.jwt.assertion.AssertionValidator; |
| 25 | +import org.mitre.jwt.assertion.impl.SelfAssertionValidator; |
| 26 | +import org.mitre.openid.connect.service.UserInfoService; |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | +import org.springframework.beans.factory.annotation.Autowired; |
| 30 | +import org.springframework.security.core.Authentication; |
| 31 | +import org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper; |
| 32 | +import org.springframework.stereotype.Controller; |
| 33 | +import org.springframework.ui.Model; |
| 34 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 35 | +import org.springframework.web.bind.annotation.RequestParam; |
| 36 | + |
| 37 | +import com.google.common.base.Strings; |
| 38 | +import com.nimbusds.jwt.JWT; |
| 39 | +import com.nimbusds.jwt.JWTParser; |
| 40 | + |
| 41 | +/** |
| 42 | + * @author jricher |
| 43 | + * |
| 44 | + */ |
| 45 | +@Controller |
| 46 | +public class EndSessionEndpoint { |
| 47 | + |
| 48 | +public static final String URL = "endsession"; |
| 49 | + |
| 50 | +private static Logger logger = LoggerFactory.getLogger(EndSessionEndpoint.class); |
| 51 | + |
| 52 | +private AssertionValidator validator = new SelfAssertionValidator(); |
| 53 | + |
| 54 | +@Autowired |
| 55 | +private UserInfoService userInfoService; |
| 56 | + |
| 57 | +@RequestMapping(value = "/" + URL) |
| 58 | +public String endSession(@RequestParam (value = "id_token_hint", required = false) String idTokenHint, |
| 59 | + @RequestParam (value = "post_logout_redirect_uri", required = false) String postLogoutRedirectUri, |
| 60 | + @RequestParam (value = "state", required = false) String state, |
| 61 | + HttpServletRequest request, |
| 62 | + Authentication auth, Model m) { |
| 63 | + |
| 64 | +// are we logged in or not? |
| 65 | +if (auth == null || !request.isUserInRole("ROLE_USER")) { |
| 66 | +// we're not logged in, process the logout |
| 67 | +return null; |
| 68 | +} else { |
| 69 | +// we are logged in, need to prompt the user before we log out |
| 70 | + |
| 71 | +// parse the ID token hint to see if it's valid |
| 72 | +if (!Strings.isNullOrEmpty(idTokenHint)) { |
| 73 | +try { |
| 74 | +JWT idToken = JWTParser.parse(idTokenHint); |
| 75 | + |
| 76 | +if (validator.isValid(idToken)) { |
| 77 | +// we issued this ID token, figure out who it's for |
| 78 | +String subject = idToken.getJWTClaimsSet().getSubject(); |
| 79 | + |
| 80 | +userInfoService.getByUsername(subject); |
| 81 | + |
| 82 | +} |
| 83 | +} catch (ParseException e) { |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | +} |
| 88 | + |
| 89 | +// display the end session page |
| 90 | +return "endSession"; |
| 91 | +} |
| 92 | +} |
| 93 | + |
| 94 | +} |
0 commit comments