| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.controller; |
| 2 | |
| margaretha | b36b1a3 | 2018-06-20 20:13:07 +0200 | [diff] [blame] | 3 | import java.net.MalformedURLException; |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 4 | import java.net.URI; |
| margaretha | b36b1a3 | 2018-06-20 20:13:07 +0200 | [diff] [blame] | 5 | import java.net.URL; |
| margaretha | a2ce63d | 2018-06-28 10:11:43 +0200 | [diff] [blame] | 6 | import java.time.ZonedDateTime; |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 7 | import java.util.Map; |
| 8 | |
| 9 | import javax.servlet.http.HttpServletRequest; |
| 10 | import javax.ws.rs.Consumes; |
| margaretha | 1929596 | 2018-06-26 16:00:47 +0200 | [diff] [blame] | 11 | import javax.ws.rs.GET; |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 12 | import javax.ws.rs.POST; |
| 13 | import javax.ws.rs.Path; |
| 14 | import javax.ws.rs.Produces; |
| 15 | import javax.ws.rs.core.Context; |
| 16 | import javax.ws.rs.core.MediaType; |
| 17 | import javax.ws.rs.core.MultivaluedMap; |
| 18 | import javax.ws.rs.core.Response; |
| 19 | import javax.ws.rs.core.Response.ResponseBuilder; |
| margaretha | b36b1a3 | 2018-06-20 20:13:07 +0200 | [diff] [blame] | 20 | import javax.ws.rs.core.Response.Status; |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 21 | import javax.ws.rs.core.SecurityContext; |
| 22 | |
| 23 | import org.springframework.beans.factory.annotation.Autowired; |
| 24 | import org.springframework.stereotype.Controller; |
| 25 | |
| margaretha | b36b1a3 | 2018-06-20 20:13:07 +0200 | [diff] [blame] | 26 | import com.nimbusds.oauth2.sdk.AccessTokenResponse; |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 27 | import com.nimbusds.oauth2.sdk.ParseException; |
| margaretha | da3c785 | 2018-06-14 20:35:11 +0200 | [diff] [blame] | 28 | import com.nimbusds.oauth2.sdk.ResponseMode; |
| margaretha | b36b1a3 | 2018-06-20 20:13:07 +0200 | [diff] [blame] | 29 | import com.nimbusds.oauth2.sdk.TokenRequest; |
| 30 | import com.nimbusds.oauth2.sdk.http.HTTPRequest.Method; |
| margaretha | da3c785 | 2018-06-14 20:35:11 +0200 | [diff] [blame] | 31 | import com.nimbusds.oauth2.sdk.id.State; |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 32 | import com.sun.jersey.spi.container.ResourceFilters; |
| 33 | |
| 34 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| margaretha | 9c78e1a | 2018-06-27 14:12:35 +0200 | [diff] [blame] | 35 | import de.ids_mannheim.korap.oauth2.openid.OpenIdConfiguration; |
| margaretha | b36b1a3 | 2018-06-20 20:13:07 +0200 | [diff] [blame] | 36 | import de.ids_mannheim.korap.oauth2.openid.OpenIdHttpRequestWrapper; |
| margaretha | 1929596 | 2018-06-26 16:00:47 +0200 | [diff] [blame] | 37 | import de.ids_mannheim.korap.oauth2.openid.service.JWKService; |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 38 | import de.ids_mannheim.korap.oauth2.openid.service.OpenIdAuthorizationService; |
| margaretha | 9c78e1a | 2018-06-27 14:12:35 +0200 | [diff] [blame] | 39 | import de.ids_mannheim.korap.oauth2.openid.service.OpenIdConfigService; |
| margaretha | b36b1a3 | 2018-06-20 20:13:07 +0200 | [diff] [blame] | 40 | import de.ids_mannheim.korap.oauth2.openid.service.OpenIdTokenService; |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 41 | import de.ids_mannheim.korap.security.context.TokenContext; |
| margaretha | da3c785 | 2018-06-14 20:35:11 +0200 | [diff] [blame] | 42 | import de.ids_mannheim.korap.web.OpenIdResponseHandler; |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 43 | import de.ids_mannheim.korap.web.filter.AuthenticationFilter; |
| 44 | import de.ids_mannheim.korap.web.filter.BlockingFilter; |
| 45 | import de.ids_mannheim.korap.web.utils.MapUtils; |
| 46 | |
| 47 | @Controller |
| 48 | @Path("/oauth2/openid") |
| 49 | public class OAuth2WithOpenIdController { |
| 50 | |
| 51 | @Autowired |
| 52 | private OpenIdAuthorizationService authzService; |
| margaretha | da3c785 | 2018-06-14 20:35:11 +0200 | [diff] [blame] | 53 | @Autowired |
| margaretha | b36b1a3 | 2018-06-20 20:13:07 +0200 | [diff] [blame] | 54 | private OpenIdTokenService tokenService; |
| 55 | @Autowired |
| margaretha | 1929596 | 2018-06-26 16:00:47 +0200 | [diff] [blame] | 56 | private JWKService jwkService; |
| 57 | @Autowired |
| margaretha | 9c78e1a | 2018-06-27 14:12:35 +0200 | [diff] [blame] | 58 | private OpenIdConfigService configService; |
| margaretha | a2ce63d | 2018-06-28 10:11:43 +0200 | [diff] [blame] | 59 | |
| margaretha | 9c78e1a | 2018-06-27 14:12:35 +0200 | [diff] [blame] | 60 | @Autowired |
| margaretha | da3c785 | 2018-06-14 20:35:11 +0200 | [diff] [blame] | 61 | private OpenIdResponseHandler openIdResponseHandler; |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Required parameters for OpenID authentication requests: |
| 65 | * |
| 66 | * <ul> |
| 67 | * <li>scope: MUST contain "openid" for OpenID Connect |
| margaretha | a2ce63d | 2018-06-28 10:11:43 +0200 | [diff] [blame] | 68 | * requests</li> |
| 69 | * <li>response_type: only "code" is supported</li> |
| 70 | * <li>client_id: client identifier given by Kustvakt during |
| 71 | * client registration</li> |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 72 | * <li>redirect_uri: MUST match a pre-registered redirect uri |
| margaretha | a2ce63d | 2018-06-28 10:11:43 +0200 | [diff] [blame] | 73 | * during client registration</li> |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 74 | * </ul> |
| 75 | * |
| 76 | * Other parameters: |
| 77 | * |
| 78 | * <ul> |
| margaretha | da3c785 | 2018-06-14 20:35:11 +0200 | [diff] [blame] | 79 | * <li>state (recommended): Opaque value used to maintain state |
| 80 | * between the request and the callback.</li> |
| 81 | * <li>response_mode (optional) : mechanism to be used for |
| margaretha | a2ce63d | 2018-06-28 10:11:43 +0200 | [diff] [blame] | 82 | * returning parameters, only "query" is supported</li> |
| margaretha | da3c785 | 2018-06-14 20:35:11 +0200 | [diff] [blame] | 83 | * <li>nonce (optional): String value used to associate a Client |
| 84 | * session with an ID Token, |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 85 | * and to mitigate replay attacks. </li> |
| margaretha | da3c785 | 2018-06-14 20:35:11 +0200 | [diff] [blame] | 86 | * <li>display (optional): specifies how the Authorization Server |
| 87 | * displays the authentication and consent user interface |
| 88 | * pages. Options: page (default), popup, touch, wap. This |
| 89 | * parameter is more relevant for Kalamar. </li> |
| 90 | * <li>prompt (optional): specifies if the Authorization Server |
| 91 | * prompts the End-User for reauthentication and consent. Defined |
| 92 | * values: none, login, consent, select_account </li> |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 93 | * <li>max_age (optional): maximum Authentication Age.</li> |
| margaretha | da3c785 | 2018-06-14 20:35:11 +0200 | [diff] [blame] | 94 | * <li>ui_locales (optional): preferred languages and scripts for |
| 95 | * the user interface represented as a space-separated list of |
| 96 | * BCP47 [RFC5646] </li> |
| 97 | * <li>id_token_hint (optional): ID Token previously issued by the |
| 98 | * Authorization Server being passed as a hint</li> |
| 99 | * <li>login_hint (optional): hint to the Authorization Server |
| 100 | * about the login identifier the End-User might use to log |
| 101 | * in</li> |
| 102 | * <li>acr_values (optional): requested Authentication Context |
| 103 | * Class Reference values. </li> |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 104 | * </ul> |
| 105 | * |
| margaretha | 1929596 | 2018-06-26 16:00:47 +0200 | [diff] [blame] | 106 | * @see "OpenID Connect Core 1.0 specification" |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 107 | * |
| 108 | * @param request |
| 109 | * @param context |
| 110 | * @param form |
| 111 | * @return a redirect to client redirect uri |
| 112 | */ |
| 113 | @POST |
| 114 | @Path("authorize") |
| 115 | @ResourceFilters({ AuthenticationFilter.class, BlockingFilter.class }) |
| 116 | @Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
| 117 | @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8") |
| 118 | public Response requestAuthorizationCode ( |
| 119 | @Context HttpServletRequest request, |
| 120 | @Context SecurityContext context, |
| 121 | MultivaluedMap<String, String> form) { |
| 122 | |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 123 | TokenContext tokenContext = (TokenContext) context.getUserPrincipal(); |
| 124 | String username = tokenContext.getUsername(); |
| margaretha | a2ce63d | 2018-06-28 10:11:43 +0200 | [diff] [blame] | 125 | ZonedDateTime authTime = tokenContext.getAuthenticationTime(); |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 126 | |
| margaretha | da3c785 | 2018-06-14 20:35:11 +0200 | [diff] [blame] | 127 | Map<String, String> map = MapUtils.toMap(form); |
| 128 | State state = authzService.retrieveState(map); |
| 129 | ResponseMode responseMode = authzService.retrieveResponseMode(map); |
| 130 | |
| 131 | boolean isAuthentication = false; |
| 132 | if (map.containsKey("scope") && map.get("scope").contains("openid")) { |
| 133 | isAuthentication = true; |
| 134 | } |
| 135 | |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 136 | URI uri = null; |
| 137 | try { |
| margaretha | da3c785 | 2018-06-14 20:35:11 +0200 | [diff] [blame] | 138 | if (isAuthentication) { |
| 139 | authzService.checkRedirectUriParam(map); |
| 140 | } |
| 141 | uri = authzService.requestAuthorizationCode(map, username, |
| margaretha | a2ce63d | 2018-06-28 10:11:43 +0200 | [diff] [blame] | 142 | isAuthentication, authTime); |
| margaretha | da3c785 | 2018-06-14 20:35:11 +0200 | [diff] [blame] | 143 | } |
| 144 | catch (ParseException e) { |
| margaretha | 249a0aa | 2018-06-28 22:25:14 +0200 | [diff] [blame^] | 145 | return openIdResponseHandler.createErrorResponse(e, state); |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 146 | } |
| 147 | catch (KustvaktException e) { |
| margaretha | da3c785 | 2018-06-14 20:35:11 +0200 | [diff] [blame] | 148 | return openIdResponseHandler.createAuthorizationErrorResponse(e, |
| 149 | isAuthentication, e.getRedirectUri(), state, responseMode); |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 150 | } |
| margaretha | da3c785 | 2018-06-14 20:35:11 +0200 | [diff] [blame] | 151 | |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 152 | ResponseBuilder builder = Response.temporaryRedirect(uri); |
| 153 | return builder.build(); |
| 154 | } |
| margaretha | da3c785 | 2018-06-14 20:35:11 +0200 | [diff] [blame] | 155 | |
| margaretha | b36b1a3 | 2018-06-20 20:13:07 +0200 | [diff] [blame] | 156 | |
| 157 | @POST |
| 158 | @Path("token") |
| 159 | @Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
| 160 | @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8") |
| 161 | public Response requestAccessToken ( |
| 162 | @Context HttpServletRequest servletRequest, |
| 163 | MultivaluedMap<String, String> form) { |
| 164 | |
| 165 | Map<String, String> map = MapUtils.toMap(form); |
| 166 | Method method = Method.valueOf(servletRequest.getMethod()); |
| 167 | URL url = null; |
| 168 | try { |
| 169 | url = new URL(servletRequest.getRequestURL().toString()); |
| 170 | } |
| 171 | catch (MalformedURLException e) { |
| 172 | // TODO Auto-generated catch block |
| 173 | e.printStackTrace(); |
| 174 | } |
| 175 | |
| 176 | try { |
| 177 | OpenIdHttpRequestWrapper httpRequest = |
| 178 | new OpenIdHttpRequestWrapper(method, url); |
| 179 | httpRequest.toHttpRequest(servletRequest, map); |
| 180 | |
| 181 | TokenRequest tokenRequest = TokenRequest.parse(httpRequest); |
| 182 | AccessTokenResponse tokenResponse = |
| 183 | tokenService.requestAccessToken(tokenRequest); |
| 184 | return openIdResponseHandler.createResponse(tokenResponse, |
| 185 | Status.OK); |
| 186 | } |
| 187 | catch (ParseException e) { |
| margaretha | 249a0aa | 2018-06-28 22:25:14 +0200 | [diff] [blame^] | 188 | return openIdResponseHandler.createErrorResponse(e, null); |
| margaretha | b36b1a3 | 2018-06-20 20:13:07 +0200 | [diff] [blame] | 189 | } |
| 190 | catch (KustvaktException e) { |
| margaretha | 249a0aa | 2018-06-28 22:25:14 +0200 | [diff] [blame^] | 191 | return openIdResponseHandler.createTokenErrorResponse(e); |
| margaretha | b36b1a3 | 2018-06-20 20:13:07 +0200 | [diff] [blame] | 192 | } |
| margaretha | b36b1a3 | 2018-06-20 20:13:07 +0200 | [diff] [blame] | 193 | } |
| margaretha | 1929596 | 2018-06-26 16:00:47 +0200 | [diff] [blame] | 194 | |
| 195 | /** |
| 196 | * Retrieves Kustvakt public keys of JWK (Json Web Key) set |
| 197 | * format. |
| 198 | * |
| 199 | * @return json string representation of the public keys |
| 200 | * |
| 201 | * @see "RFC 8017 regarding RSA specifications" |
| 202 | * @see "RFC 7517 regarding JWK (Json Web Key) and JWK Set" |
| 203 | */ |
| 204 | @GET |
| margaretha | 9c78e1a | 2018-06-27 14:12:35 +0200 | [diff] [blame] | 205 | @Path("jwks") |
| margaretha | 1929596 | 2018-06-26 16:00:47 +0200 | [diff] [blame] | 206 | @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8") |
| margaretha | 9c78e1a | 2018-06-27 14:12:35 +0200 | [diff] [blame] | 207 | public String requestPublicKeys () { |
| margaretha | 1929596 | 2018-06-26 16:00:47 +0200 | [diff] [blame] | 208 | return jwkService.generatePublicKeySetJson(); |
| 209 | } |
| margaretha | 9c78e1a | 2018-06-27 14:12:35 +0200 | [diff] [blame] | 210 | |
| 211 | /** |
| 212 | * When supporting discovery, must be available at |
| 213 | * {issuer_uri}/.well-known/openid-configuration |
| margaretha | a2ce63d | 2018-06-28 10:11:43 +0200 | [diff] [blame] | 214 | * |
| 215 | * @return |
| margaretha | 9c78e1a | 2018-06-27 14:12:35 +0200 | [diff] [blame] | 216 | * |
| 217 | * @return |
| 218 | */ |
| 219 | @GET |
| 220 | @Path("config") |
| 221 | @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8") |
| 222 | public OpenIdConfiguration requestOpenIdConfig () { |
| 223 | return configService.retrieveOpenIdConfigInfo(); |
| 224 | } |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 225 | } |