oauth2UserService() {
27 | return new KickstartOAuth2UserService();
28 | }
29 | }
--------------------------------------------------------------------------------
/src/main/java/com/stewlutions/azuread_springsecurity5_oidc/kickstart/StubController.java:
--------------------------------------------------------------------------------
1 | package com.stewlutions.azuread_springsecurity5_oidc.kickstart;
2 |
3 | import java.text.MessageFormat;
4 |
5 | import org.springframework.web.bind.annotation.RequestMapping;
6 | import org.springframework.web.bind.annotation.RestController;
7 | import org.springframework.web.servlet.ModelAndView;
8 |
9 | import javax.servlet.http.HttpServletRequest;
10 |
11 | import com.microsoft.aad.adal4j.AuthenticationContext;
12 |
13 | import org.springframework.security.core.context.SecurityContextHolder;
14 | import org.springframework.ui.ModelMap;
15 | import org.springframework.web.bind.annotation.PathVariable;
16 |
17 | @RestController
18 | class StubController {
19 |
20 | @RequestMapping("/hello/{name}")
21 | String hello(@PathVariable String name) {
22 | return "Hello, " + name + "!";
23 | }
24 |
25 | @RequestMapping("/claims")
26 | String claims() {
27 | return "Data: " + SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
28 | }
29 |
30 | @RequestMapping("/patient/{id}")
31 | ModelAndView patient(ModelMap model, HttpServletRequest request, @PathVariable String id) {
32 | model.addAttribute("attribute", "forwardWithForwardPrefix");
33 |
34 | String client_id = "";
35 | int state = (int )(Math.random() * 1000000 + 1);
36 | String resource = "client_id_guid_of_fhir";
37 | String base_uri = request.getRequestURL().toString();
38 | base_uri = base_uri.substring(0, base_uri.length() - request.getServletPath().length());
39 | String redirect_uri = base_uri + "/context/authorized";
40 |
41 | String redirect = String.format("https://login.microsoftonline.com/common/oauth2/authorize?client_id=%s&state=%s&resource=%s&redirect_uri=%s&response_type=code&response_mode=post", client_id, state, resource, redirect_uri);
42 | return new ModelAndView("redirect:" + redirect, model);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/com/stewlutions/azuread_springsecurity5_oidc/kickstart/replacements/KickstartOAuth2UserService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2002-2017 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.stewlutions.azuread_springsecurity5_oidc.kickstart.replacements;
17 |
18 | import org.springframework.core.ParameterizedTypeReference;
19 | import org.springframework.security.core.GrantedAuthority;
20 | import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
21 | import org.springframework.security.oauth2.core.OAuth2Error;
22 | import org.springframework.security.oauth2.core.user.DefaultOAuth2User;
23 | import org.springframework.security.oauth2.core.user.OAuth2User;
24 | import org.springframework.security.oauth2.core.user.OAuth2UserAuthority;
25 | import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
26 | import org.springframework.security.oauth2.client.userinfo.OAuth2UserService;
27 | import org.springframework.util.Assert;
28 | import org.springframework.util.StringUtils;
29 |
30 | import java.util.HashSet;
31 | import java.util.Map;
32 | import java.util.Set;
33 |
34 | /**
35 | * An implementation of an {@link OAuth2UserService} that supports standard OAuth 2.0 Provider's.
36 | *
37 | * For standard OAuth 2.0 Provider's, the attribute name used to access the user's name
38 | * from the UserInfo response is required and therefore must be available via
39 | * {@link org.springframework.security.oauth2.client.registration.ClientRegistration.ProviderDetails.UserInfoEndpoint#getUserNameAttributeName() UserInfoEndpoint.getUserNameAttributeName()}.
40 | *
41 | * NOTE: Attribute names are not standardized between providers and therefore will vary.
42 | * Please consult the provider's API documentation for the set of supported user attribute names.
43 | *
44 | * @author Joe Grandja
45 | * @since 5.0
46 | * @see OAuth2UserService
47 | * @see OAuth2UserRequest
48 | * @see OAuth2User
49 | * @see DefaultOAuth2User
50 | */
51 | public class KickstartOAuth2UserService implements OAuth2UserService {
52 | private static final String MISSING_USER_INFO_URI_ERROR_CODE = "missing_user_info_uri";
53 | private static final String MISSING_USER_NAME_ATTRIBUTE_ERROR_CODE = "missing_user_name_attribute";
54 | private KickstartUserInfoResponseClient userInfoResponseClient = new KickstartUserInfoResponseClient();
55 |
56 | @Override
57 | public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
58 | Assert.notNull(userRequest, "userRequest cannot be null");
59 |
60 | if (!StringUtils.hasText(userRequest.getClientRegistration().getProviderDetails().getUserInfoEndpoint().getUri())) {
61 | OAuth2Error oauth2Error = new OAuth2Error(
62 | MISSING_USER_INFO_URI_ERROR_CODE,
63 | "Missing required UserInfo Uri in UserInfoEndpoint for Client Registration: " +
64 | userRequest.getClientRegistration().getRegistrationId(),
65 | null
66 | );
67 | throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
68 | }
69 | String userNameAttributeName = userRequest.getClientRegistration().getProviderDetails().getUserInfoEndpoint().getUserNameAttributeName();
70 | if (!StringUtils.hasText(userNameAttributeName)) {
71 | OAuth2Error oauth2Error = new OAuth2Error(
72 | MISSING_USER_NAME_ATTRIBUTE_ERROR_CODE,
73 | "Missing required \"user name\" attribute name in UserInfoEndpoint for Client Registration: " +
74 | userRequest.getClientRegistration().getRegistrationId(),
75 | null
76 | );
77 | throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
78 | }
79 |
80 | ParameterizedTypeReference