| margaretha | 0e8f4e7 | 2018-04-05 14:11:52 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.controller; |
| 2 | |
| margaretha | 5f5d3ed | 2023-08-30 23:48:52 +0200 | [diff] [blame] | 3 | import java.net.URI; |
| 4 | import java.net.URISyntaxException; |
| margaretha | a2ce63d | 2018-06-28 10:11:43 +0200 | [diff] [blame] | 5 | import java.time.ZonedDateTime; |
| margaretha | 4993eb7 | 2023-09-27 10:54:34 +0200 | [diff] [blame] | 6 | import java.util.List; |
| margaretha | a2ce63d | 2018-06-28 10:11:43 +0200 | [diff] [blame] | 7 | |
| margaretha | 0e8f4e7 | 2018-04-05 14:11:52 +0200 | [diff] [blame] | 8 | import org.springframework.beans.factory.annotation.Autowired; |
| 9 | import org.springframework.stereotype.Controller; |
| 10 | |
| margaretha | b8a9d4e | 2023-10-25 12:00:10 +0200 | [diff] [blame] | 11 | import com.nimbusds.oauth2.sdk.AccessTokenResponse; |
| margaretha | 5f5d3ed | 2023-08-30 23:48:52 +0200 | [diff] [blame] | 12 | import com.nimbusds.oauth2.sdk.AuthorizationErrorResponse; |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 13 | import com.nimbusds.oauth2.sdk.AuthorizationGrant; |
| 14 | import com.nimbusds.oauth2.sdk.ClientCredentialsGrant; |
| margaretha | 5f5d3ed | 2023-08-30 23:48:52 +0200 | [diff] [blame] | 15 | import com.nimbusds.oauth2.sdk.OAuth2Error; |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 16 | import com.nimbusds.oauth2.sdk.ParseException; |
| 17 | import com.nimbusds.oauth2.sdk.Scope; |
| 18 | import com.nimbusds.oauth2.sdk.TokenRequest; |
| 19 | import com.nimbusds.oauth2.sdk.auth.ClientAuthentication; |
| 20 | import com.nimbusds.oauth2.sdk.auth.ClientAuthenticationMethod; |
| 21 | import com.nimbusds.oauth2.sdk.auth.ClientSecretBasic; |
| 22 | import com.nimbusds.oauth2.sdk.auth.ClientSecretPost; |
| 23 | import com.nimbusds.oauth2.sdk.id.ClientID; |
| margaretha | 5f5d3ed | 2023-08-30 23:48:52 +0200 | [diff] [blame] | 24 | |
| margaretha | 2df0660 | 2018-11-14 19:10:30 +0100 | [diff] [blame] | 25 | import de.ids_mannheim.korap.constant.OAuth2Scope; |
| margaretha | 0e8f4e7 | 2018-04-05 14:11:52 +0200 | [diff] [blame] | 26 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| margaretha | 3507469 | 2021-03-26 18:11:59 +0100 | [diff] [blame] | 27 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| margaretha | 4993eb7 | 2023-09-27 10:54:34 +0200 | [diff] [blame] | 28 | import de.ids_mannheim.korap.oauth2.dto.OAuth2TokenDto; |
| margaretha | 5f5d3ed | 2023-08-30 23:48:52 +0200 | [diff] [blame] | 29 | import de.ids_mannheim.korap.oauth2.service.OAuth2AuthorizationService; |
| margaretha | 835178d | 2018-08-15 19:04:03 +0200 | [diff] [blame] | 30 | import de.ids_mannheim.korap.oauth2.service.OAuth2ScopeService; |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 31 | import de.ids_mannheim.korap.oauth2.service.OAuth2TokenService; |
| margaretha | 07402f4 | 2018-05-07 19:07:45 +0200 | [diff] [blame] | 32 | import de.ids_mannheim.korap.security.context.TokenContext; |
| margaretha | 4993eb7 | 2023-09-27 10:54:34 +0200 | [diff] [blame] | 33 | import de.ids_mannheim.korap.utils.ParameterChecker; |
| margaretha | f839dde | 2018-04-16 17:52:57 +0200 | [diff] [blame] | 34 | import de.ids_mannheim.korap.web.OAuth2ResponseHandler; |
| margaretha | 398f472 | 2019-01-09 19:07:20 +0100 | [diff] [blame] | 35 | import de.ids_mannheim.korap.web.filter.APIVersionFilter; |
| margaretha | 07402f4 | 2018-05-07 19:07:45 +0200 | [diff] [blame] | 36 | import de.ids_mannheim.korap.web.filter.AuthenticationFilter; |
| margaretha | db5da37 | 2023-09-01 11:02:52 +0200 | [diff] [blame] | 37 | import de.ids_mannheim.korap.web.filter.BlockingFilter; |
| margaretha | 96c309d | 2023-08-16 12:24:12 +0200 | [diff] [blame] | 38 | import de.ids_mannheim.korap.web.utils.ResourceFilters; |
| margaretha | 5f5d3ed | 2023-08-30 23:48:52 +0200 | [diff] [blame] | 39 | import jakarta.servlet.http.HttpServletRequest; |
| margaretha | 93bfbea | 2023-11-06 21:09:21 +0100 | [diff] [blame] | 40 | import jakarta.validation.constraints.NotEmpty; |
| margaretha | 751608c | 2023-09-26 08:48:30 +0200 | [diff] [blame] | 41 | import jakarta.ws.rs.Consumes; |
| 42 | import jakarta.ws.rs.FormParam; |
| margaretha | 96c309d | 2023-08-16 12:24:12 +0200 | [diff] [blame] | 43 | import jakarta.ws.rs.GET; |
| margaretha | 751608c | 2023-09-26 08:48:30 +0200 | [diff] [blame] | 44 | import jakarta.ws.rs.POST; |
| margaretha | 96c309d | 2023-08-16 12:24:12 +0200 | [diff] [blame] | 45 | import jakarta.ws.rs.Path; |
| 46 | import jakarta.ws.rs.Produces; |
| 47 | import jakarta.ws.rs.QueryParam; |
| 48 | import jakarta.ws.rs.core.Context; |
| 49 | import jakarta.ws.rs.core.MediaType; |
| margaretha | 751608c | 2023-09-26 08:48:30 +0200 | [diff] [blame] | 50 | import jakarta.ws.rs.core.MultivaluedMap; |
| margaretha | 96c309d | 2023-08-16 12:24:12 +0200 | [diff] [blame] | 51 | import jakarta.ws.rs.core.Response; |
| 52 | import jakarta.ws.rs.core.SecurityContext; |
| margaretha | 751608c | 2023-09-26 08:48:30 +0200 | [diff] [blame] | 53 | import jakarta.ws.rs.core.UriBuilder; |
| margaretha | 0e8f4e7 | 2018-04-05 14:11:52 +0200 | [diff] [blame] | 54 | |
| margaretha | 398f472 | 2019-01-09 19:07:20 +0100 | [diff] [blame] | 55 | /** |
| 56 | * OAuth2Controller describes OAuth2 web API for authorization |
| 57 | * for both internal (e.g Kalamar) and external (third party) clients. |
| 58 | * |
| 59 | * Possible authorization scopes are listed in {@link OAuth2Scope} For |
| 60 | * more information about OAuth2 authorization mechanisms, see RFC |
| 61 | * 6749. |
| 62 | * |
| 63 | * @author margaretha |
| 64 | * |
| 65 | */ |
| margaretha | 0e8f4e7 | 2018-04-05 14:11:52 +0200 | [diff] [blame] | 66 | @Controller |
| margaretha | ee0cbfe | 2018-08-28 17:47:14 +0200 | [diff] [blame] | 67 | @Path("{version}/oauth2") |
| margaretha | db5da37 | 2023-09-01 11:02:52 +0200 | [diff] [blame] | 68 | @ResourceFilters({ APIVersionFilter.class, AuthenticationFilter.class, |
| 69 | BlockingFilter.class }) |
| margaretha | 45ba733 | 2023-01-31 11:39:52 +0100 | [diff] [blame] | 70 | @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8") |
| margaretha | 0e8f4e7 | 2018-04-05 14:11:52 +0200 | [diff] [blame] | 71 | public class OAuth2Controller { |
| 72 | |
| 73 | @Autowired |
| margaretha | f839dde | 2018-04-16 17:52:57 +0200 | [diff] [blame] | 74 | private OAuth2ResponseHandler responseHandler; |
| margaretha | db5da37 | 2023-09-01 11:02:52 +0200 | [diff] [blame] | 75 | |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 76 | @Autowired |
| 77 | private OAuth2TokenService tokenService; |
| margaretha | 0e8f4e7 | 2018-04-05 14:11:52 +0200 | [diff] [blame] | 78 | @Autowired |
| margaretha | 5f5d3ed | 2023-08-30 23:48:52 +0200 | [diff] [blame] | 79 | private OAuth2AuthorizationService authorizationService; |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 80 | |
| margaretha | 835178d | 2018-08-15 19:04:03 +0200 | [diff] [blame] | 81 | @Autowired |
| 82 | private OAuth2ScopeService scopeService; |
| margaretha | fb027f9 | 2018-04-23 20:00:13 +0200 | [diff] [blame] | 83 | |
| margaretha | a452c5e | 2018-04-25 22:48:09 +0200 | [diff] [blame] | 84 | /** |
| 85 | * Requests an authorization code. |
| margaretha | fb027f9 | 2018-04-23 20:00:13 +0200 | [diff] [blame] | 86 | * |
| margaretha | a452c5e | 2018-04-25 22:48:09 +0200 | [diff] [blame] | 87 | * Kustvakt supports authorization only with Kalamar as the |
| 88 | * authorization web-frontend or user interface. Thus |
| margaretha | 07402f4 | 2018-05-07 19:07:45 +0200 | [diff] [blame] | 89 | * authorization code request requires user authentication |
| margaretha | c750cbb | 2018-12-11 12:47:02 +0100 | [diff] [blame] | 90 | * using authorization header. |
| margaretha | a452c5e | 2018-04-25 22:48:09 +0200 | [diff] [blame] | 91 | * |
| 92 | * <br /><br /> |
| 93 | * RFC 6749: |
| 94 | * If the client omits the scope parameter when requesting |
| 95 | * authorization, the authorization server MUST either process the |
| 96 | * request using a pre-defined default value or fail the request |
| 97 | * indicating an invalid scope. |
| 98 | * |
| margaretha | 07402f4 | 2018-05-07 19:07:45 +0200 | [diff] [blame] | 99 | * @param request |
| 100 | * HttpServletRequest |
| 101 | * @param form |
| 102 | * form parameters |
| margaretha | a452c5e | 2018-04-25 22:48:09 +0200 | [diff] [blame] | 103 | * @return a redirect URL |
| margaretha | fb027f9 | 2018-04-23 20:00:13 +0200 | [diff] [blame] | 104 | */ |
| margaretha | 751608c | 2023-09-26 08:48:30 +0200 | [diff] [blame] | 105 | @Deprecated |
| 106 | @POST |
| 107 | @Path("authorize") |
| 108 | @Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
| 109 | public Response requestAuthorizationCode ( |
| 110 | @Context HttpServletRequest request, |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 111 | @Context SecurityContext context, @FormParam("scope") String scope, |
| margaretha | 751608c | 2023-09-26 08:48:30 +0200 | [diff] [blame] | 112 | @FormParam("state") String state, |
| 113 | @FormParam("client_id") String clientId, |
| 114 | @FormParam("redirect_uri") String redirectUri, |
| 115 | MultivaluedMap<String, String> form) { |
| 116 | |
| 117 | TokenContext tokenContext = (TokenContext) context.getUserPrincipal(); |
| 118 | String username = tokenContext.getUsername(); |
| 119 | ZonedDateTime authTime = tokenContext.getAuthenticationTime(); |
| 120 | |
| 121 | URI requestURI; |
| 122 | UriBuilder builder = UriBuilder.fromPath(request.getRequestURI()); |
| 123 | for (String key : form.keySet()) { |
| 124 | builder.queryParam(key, form.get(key).toArray()); |
| 125 | } |
| 126 | requestURI = builder.build(); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 127 | |
| margaretha | 751608c | 2023-09-26 08:48:30 +0200 | [diff] [blame] | 128 | try { |
| 129 | scopeService.verifyScope(tokenContext, OAuth2Scope.AUTHORIZE); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 130 | URI uri = authorizationService.requestAuthorizationCode(requestURI, |
| 131 | clientId, redirectUri, scope, state, username, authTime); |
| margaretha | 751608c | 2023-09-26 08:48:30 +0200 | [diff] [blame] | 132 | return responseHandler.sendRedirect(uri); |
| 133 | } |
| 134 | catch (KustvaktException e) { |
| 135 | e = authorizationService.checkRedirectUri(e, clientId, redirectUri); |
| 136 | if (e.getRedirectUri() != null) { |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 137 | AuthorizationErrorResponse errorResponse = authorizationService |
| 138 | .createAuthorizationError(e, state); |
| margaretha | 751608c | 2023-09-26 08:48:30 +0200 | [diff] [blame] | 139 | return responseHandler.sendRedirect(errorResponse.toURI()); |
| 140 | } |
| 141 | else { |
| 142 | throw responseHandler.throwit(e, state); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 143 | } |
| margaretha | 751608c | 2023-09-26 08:48:30 +0200 | [diff] [blame] | 144 | } |
| 145 | } |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 146 | |
| margaretha | d67b427 | 2022-04-11 17:34:19 +0200 | [diff] [blame] | 147 | @GET |
| 148 | @Path("authorize") |
| 149 | public Response requestAuthorizationCode ( |
| 150 | @Context HttpServletRequest request, |
| 151 | @Context SecurityContext context, |
| margaretha | 5f5d3ed | 2023-08-30 23:48:52 +0200 | [diff] [blame] | 152 | @QueryParam("response_type") String responseType, |
| margaretha | ffb8950 | 2022-04-20 12:03:16 +0200 | [diff] [blame] | 153 | @QueryParam("client_id") String clientId, |
| 154 | @QueryParam("redirect_uri") String redirectUri, |
| margaretha | 5f5d3ed | 2023-08-30 23:48:52 +0200 | [diff] [blame] | 155 | @QueryParam("scope") String scope, |
| 156 | @QueryParam("state") String state) { |
| margaretha | d67b427 | 2022-04-11 17:34:19 +0200 | [diff] [blame] | 157 | |
| 158 | TokenContext tokenContext = (TokenContext) context.getUserPrincipal(); |
| 159 | String username = tokenContext.getUsername(); |
| 160 | ZonedDateTime authTime = tokenContext.getAuthenticationTime(); |
| 161 | |
| margaretha | 5f5d3ed | 2023-08-30 23:48:52 +0200 | [diff] [blame] | 162 | URI requestURI; |
| 163 | try { |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 164 | requestURI = new URI( |
| 165 | request.getRequestURI() + "?" + request.getQueryString()); |
| margaretha | 5f5d3ed | 2023-08-30 23:48:52 +0200 | [diff] [blame] | 166 | } |
| 167 | catch (URISyntaxException e) { |
| 168 | KustvaktException ke = new KustvaktException( |
| 169 | StatusCodes.INVALID_REQUEST, "Failed parsing request URI.", |
| 170 | OAuth2Error.INVALID_REQUEST_URI); |
| 171 | throw responseHandler.throwit(ke, state); |
| 172 | } |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 173 | |
| margaretha | d67b427 | 2022-04-11 17:34:19 +0200 | [diff] [blame] | 174 | try { |
| 175 | scopeService.verifyScope(tokenContext, OAuth2Scope.AUTHORIZE); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 176 | URI uri = authorizationService.requestAuthorizationCode(requestURI, |
| 177 | clientId, redirectUri, scope, state, username, authTime); |
| margaretha | d67b427 | 2022-04-11 17:34:19 +0200 | [diff] [blame] | 178 | return responseHandler.sendRedirect(uri); |
| 179 | } |
| margaretha | d67b427 | 2022-04-11 17:34:19 +0200 | [diff] [blame] | 180 | catch (KustvaktException e) { |
| margaretha | ffb8950 | 2022-04-20 12:03:16 +0200 | [diff] [blame] | 181 | e = authorizationService.checkRedirectUri(e, clientId, redirectUri); |
| margaretha | 5f5d3ed | 2023-08-30 23:48:52 +0200 | [diff] [blame] | 182 | if (e.getRedirectUri() != null) { |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 183 | AuthorizationErrorResponse errorResponse = authorizationService |
| 184 | .createAuthorizationError(e, state); |
| margaretha | 5f5d3ed | 2023-08-30 23:48:52 +0200 | [diff] [blame] | 185 | return responseHandler.sendRedirect(errorResponse.toURI()); |
| 186 | } |
| 187 | else { |
| 188 | throw responseHandler.throwit(e, state); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 189 | } |
| margaretha | d67b427 | 2022-04-11 17:34:19 +0200 | [diff] [blame] | 190 | } |
| 191 | } |
| margaretha | fb027f9 | 2018-04-23 20:00:13 +0200 | [diff] [blame] | 192 | |
| margaretha | a452c5e | 2018-04-25 22:48:09 +0200 | [diff] [blame] | 193 | /** |
| 194 | * Grants a client an access token, namely a string used in |
| 195 | * authenticated requests representing user authorization for |
| margaretha | dc51507 | 2018-08-03 17:01:19 +0200 | [diff] [blame] | 196 | * the client to access user resources. An additional refresh |
| margaretha | 3507469 | 2021-03-26 18:11:59 +0100 | [diff] [blame] | 197 | * token strictly associated to the access token is also granted |
| 198 | * for confidential clients. Both public and confidential clients |
| 199 | * may issue multiple access tokens. |
| margaretha | a048627 | 2018-04-12 19:59:31 +0200 | [diff] [blame] | 200 | * |
| margaretha | a452c5e | 2018-04-25 22:48:09 +0200 | [diff] [blame] | 201 | * <br /><br /> |
| 202 | * |
| margaretha | 3507469 | 2021-03-26 18:11:59 +0100 | [diff] [blame] | 203 | * Confidential clients may request refresh access token using |
| 204 | * this endpoint. This request will grant a new access token. |
| 205 | * |
| 206 | * Usually the given refresh token is not changed and can be used |
| 207 | * until it expires. However, currently there is a limitation of |
| 208 | * one access token per one refresh token. Thus, the given refresh |
| 209 | * token will be revoked, and a new access token and a new refresh |
| 210 | * token will be returned. |
| margaretha | dc51507 | 2018-08-03 17:01:19 +0200 | [diff] [blame] | 211 | * |
| 212 | * <br /><br /> |
| 213 | * |
| 214 | * Client credentials for authentication can be provided either as |
| 215 | * an authorization header with Basic authentication scheme or as |
| 216 | * form parameters in the request body. |
| 217 | * |
| 218 | * <br /><br /> |
| 219 | * |
| 220 | * OAuth2 specification describes various ways of requesting an |
| 221 | * access token. Kustvakt supports: |
| margaretha | a452c5e | 2018-04-25 22:48:09 +0200 | [diff] [blame] | 222 | * <ul> |
| 223 | * <li> Authorization code grant: obtains authorization from a |
| 224 | * third party application. Required parameters: grant_type, |
| 225 | * code, client_id, redirect_uri (if specified in the |
| 226 | * authorization request), client_secret (if the client is |
| 227 | * confidential or issued a secret). |
| 228 | * </li> |
| 229 | * <li> Resource owner password grant: strictly for clients that |
| 230 | * are parts of KorAP. Clients use user credentials, e.g. Kalamar |
| 231 | * (front-end) with login form. Required parameters: grant_type, |
| 232 | * username, password, client_id, client_secret (if the client is |
| 233 | * confidential or issued a secret). Optional parameters: scope. |
| 234 | * </li> |
| 235 | * <li> Client credentials grant: strictly for clients that are |
| 236 | * parts of KorAP. Clients access their own resources, not on |
| 237 | * behalf of a user. Required parameters: grant_type, client_id, |
| 238 | * client_secret. Optional parameters: scope. |
| 239 | * </li> |
| 240 | * </ul> |
| 241 | * |
| margaretha | a452c5e | 2018-04-25 22:48:09 +0200 | [diff] [blame] | 242 | * RFC 6749: The value of the scope parameter is expressed as a |
| 243 | * list of space-delimited, case-sensitive strings defined by the |
| 244 | * authorization server. |
| 245 | * |
| 246 | * @param request |
| 247 | * the request |
| margaretha | a452c5e | 2018-04-25 22:48:09 +0200 | [diff] [blame] | 248 | * @param form |
| 249 | * form parameters in a map |
| 250 | * @return a JSON object containing an access token, a refresh |
| 251 | * token, a token type and the token expiration in seconds |
| 252 | * if successful, an error code and an error description |
| 253 | * otherwise. |
| margaretha | a048627 | 2018-04-12 19:59:31 +0200 | [diff] [blame] | 254 | */ |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 255 | @POST |
| 256 | @Path("token") |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 257 | @ResourceFilters({ APIVersionFilter.class }) |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 258 | @Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
| 259 | public Response requestAccessToken (@Context HttpServletRequest request, |
| margaretha | 93bfbea | 2023-11-06 21:09:21 +0100 | [diff] [blame] | 260 | @NotEmpty @FormParam("grant_type") String grantType, |
| 261 | @NotEmpty @FormParam("client_id") String clientId, |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 262 | @FormParam("client_secret") String clientSecret, |
| 263 | MultivaluedMap<String, String> form) { |
| 264 | |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 265 | try { |
| 266 | URI requestURI; |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 267 | UriBuilder builder = UriBuilder |
| 268 | .fromPath(request.getRequestURL().toString()); |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 269 | for (String key : form.keySet()) { |
| 270 | builder.queryParam(key, form.get(key).toArray()); |
| 271 | } |
| 272 | requestURI = builder.build(); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 273 | |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 274 | try { |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 275 | AuthorizationGrant authGrant = AuthorizationGrant.parse(form); |
| 276 | |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 277 | ClientAuthentication clientAuth = null; |
| 278 | String authorizationHeader = request.getHeader("Authorization"); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 279 | if (authorizationHeader != null |
| 280 | && !authorizationHeader.isEmpty()) { |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 281 | clientAuth = ClientSecretBasic.parse(authorizationHeader); |
| 282 | } |
| 283 | else if (authGrant instanceof ClientCredentialsGrant) { |
| 284 | // this doesn't allow public clients |
| 285 | clientAuth = ClientSecretPost.parse(form); |
| 286 | } |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 287 | |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 288 | TokenRequest tokenRequest = null; |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 289 | if (clientAuth != null) { |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 290 | ClientAuthenticationMethod method = clientAuth.getMethod(); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 291 | if (method.equals( |
| 292 | ClientAuthenticationMethod.CLIENT_SECRET_BASIC)) { |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 293 | ClientSecretBasic basic = (ClientSecretBasic) clientAuth; |
| 294 | clientSecret = basic.getClientSecret().getValue(); |
| 295 | clientId = basic.getClientID().getValue(); |
| 296 | } |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 297 | else if (method.equals( |
| 298 | ClientAuthenticationMethod.CLIENT_SECRET_POST)) { |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 299 | ClientSecretPost post = (ClientSecretPost) clientAuth; |
| 300 | clientSecret = post.getClientSecret().getValue(); |
| 301 | clientId = post.getClientID().getValue(); |
| 302 | } |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 303 | |
| 304 | tokenRequest = new TokenRequest(requestURI, clientAuth, |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 305 | AuthorizationGrant.parse(form), |
| 306 | Scope.parse(form.getFirst("scope"))); |
| 307 | } |
| 308 | else { |
| 309 | // requires ClientAuthentication for client_credentials grant |
| 310 | tokenRequest = new TokenRequest(requestURI, |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 311 | new ClientID(clientId), |
| 312 | AuthorizationGrant.parse(form), |
| 313 | Scope.parse(form.getFirst("scope"))); |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 314 | } |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 315 | |
| 316 | AccessTokenResponse r = tokenService.requestAccessToken( |
| 317 | tokenRequest, clientId, clientSecret); |
| margaretha | b8a9d4e | 2023-10-25 12:00:10 +0200 | [diff] [blame] | 318 | return responseHandler.createResponse(r); |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 319 | } |
| 320 | catch (ParseException | IllegalArgumentException e) { |
| 321 | throw new KustvaktException(StatusCodes.INVALID_REQUEST, |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 322 | e.getMessage(), OAuth2Error.INVALID_REQUEST); |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 323 | } |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 324 | |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 325 | } |
| 326 | catch (KustvaktException e) { |
| 327 | throw responseHandler.throwit(e); |
| 328 | } |
| margaretha | 7ac20b1 | 2023-09-27 09:40:16 +0200 | [diff] [blame] | 329 | } |
| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame] | 330 | |
| margaretha | dc51507 | 2018-08-03 17:01:19 +0200 | [diff] [blame] | 331 | /** |
| margaretha | 1ef36bd | 2018-08-14 18:17:05 +0200 | [diff] [blame] | 332 | * Revokes either an access token or a refresh token. Revoking a |
| 333 | * refresh token also revokes all access token associated with the |
| 334 | * refresh token. |
| margaretha | dc51507 | 2018-08-03 17:01:19 +0200 | [diff] [blame] | 335 | * |
| 336 | * RFC 7009 |
| 337 | * Client authentication for confidential client |
| 338 | * |
| 339 | * @param request |
| 340 | * @param form |
| margaretha | 1ef36bd | 2018-08-14 18:17:05 +0200 | [diff] [blame] | 341 | * containing |
| 342 | * client_id, |
| 343 | * client_secret (required for confidential clients), |
| 344 | * token, |
| 345 | * token_type (optional) |
| margaretha | dc51507 | 2018-08-03 17:01:19 +0200 | [diff] [blame] | 346 | * @return 200 if token invalidation is successful or the given |
| 347 | * token is invalid |
| 348 | */ |
| margaretha | 4993eb7 | 2023-09-27 10:54:34 +0200 | [diff] [blame] | 349 | @POST |
| 350 | @Path("revoke") |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 351 | @ResourceFilters({ APIVersionFilter.class }) |
| margaretha | 4993eb7 | 2023-09-27 10:54:34 +0200 | [diff] [blame] | 352 | @Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
| 353 | public Response revokeAccessToken (@Context HttpServletRequest request, |
| 354 | @FormParam("client_id") String clientId, |
| 355 | @FormParam("client_secret") String clientSecret, |
| 356 | @FormParam("token") String token, |
| 357 | @FormParam("token_type") String tokenType) { |
| 358 | |
| 359 | try { |
| 360 | ParameterChecker.checkStringValue("client_id", clientId); |
| 361 | ParameterChecker.checkStringValue("token", token); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 362 | tokenService.revokeToken(clientId, clientSecret, token, tokenType); |
| 363 | |
| margaretha | 4993eb7 | 2023-09-27 10:54:34 +0200 | [diff] [blame] | 364 | return Response.ok("SUCCESS").build(); |
| 365 | } |
| 366 | catch (KustvaktException e) { |
| 367 | throw responseHandler.throwit(e); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | @POST |
| 372 | @Path("revoke/super") |
| 373 | @Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
| 374 | public Response revokeTokenViaSuperClient (@Context SecurityContext context, |
| 375 | @Context HttpServletRequest request, |
| 376 | @FormParam("super_client_id") String superClientId, |
| 377 | @FormParam("super_client_secret") String superClientSecret, |
| 378 | @FormParam("token") String token) { |
| 379 | |
| 380 | try { |
| 381 | ParameterChecker.checkStringValue("super_client_id", superClientId); |
| 382 | ParameterChecker.checkStringValue("super_client_secret", |
| 383 | superClientSecret); |
| 384 | ParameterChecker.checkStringValue("token", token); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 385 | |
| 386 | TokenContext tokenContext = (TokenContext) context |
| 387 | .getUserPrincipal(); |
| margaretha | 4993eb7 | 2023-09-27 10:54:34 +0200 | [diff] [blame] | 388 | String username = tokenContext.getUsername(); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 389 | |
| margaretha | 4993eb7 | 2023-09-27 10:54:34 +0200 | [diff] [blame] | 390 | tokenService.revokeTokensViaSuperClient(username, superClientId, |
| 391 | superClientSecret, token); |
| 392 | return Response.ok("SUCCESS").build(); |
| 393 | } |
| 394 | catch (KustvaktException e) { |
| 395 | throw responseHandler.throwit(e); |
| 396 | } |
| 397 | } |
| margaretha | 3507469 | 2021-03-26 18:11:59 +0100 | [diff] [blame] | 398 | |
| margaretha | 7497adf | 2019-11-26 13:13:57 +0100 | [diff] [blame] | 399 | /** |
| 400 | * Revokes all tokens of a client for the authenticated user from |
| 401 | * a super client. This service is not part of the OAUTH2 |
| 402 | * specification. It requires user authentication via |
| 403 | * authorization header, and super client |
| 404 | * via URL-encoded form parameters. |
| 405 | * |
| 406 | * @param request |
| 407 | * @param form |
| 408 | * containing client_id, super_client_id, |
| 409 | * super_client_secret |
| 410 | * @return 200 if token invalidation is successful or the given |
| 411 | * token is invalid |
| 412 | */ |
| margaretha | 4993eb7 | 2023-09-27 10:54:34 +0200 | [diff] [blame] | 413 | @POST |
| 414 | @Path("revoke/super/all") |
| 415 | @Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
| 416 | public Response revokeAllClientTokensViaSuperClient ( |
| 417 | @Context SecurityContext context, |
| 418 | @Context HttpServletRequest request, |
| 419 | @FormParam("client_id") String clientId, |
| 420 | @FormParam("super_client_id") String superClientId, |
| 421 | @FormParam("super_client_secret") String superClientSecret) { |
| 422 | |
| 423 | TokenContext tokenContext = (TokenContext) context.getUserPrincipal(); |
| 424 | String username = tokenContext.getUsername(); |
| 425 | |
| 426 | try { |
| 427 | ParameterChecker.checkStringValue("super_client_id", superClientId); |
| 428 | ParameterChecker.checkStringValue("super_client_secret", |
| 429 | superClientSecret); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 430 | |
| margaretha | 4993eb7 | 2023-09-27 10:54:34 +0200 | [diff] [blame] | 431 | tokenService.revokeAllClientTokensViaSuperClient(username, |
| 432 | superClientId, superClientSecret, clientId); |
| 433 | return Response.ok("SUCCESS").build(); |
| 434 | } |
| 435 | catch (KustvaktException e) { |
| 436 | throw responseHandler.throwit(e); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | @POST |
| 441 | @Path("token/list") |
| 442 | @Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 443 | public List<OAuth2TokenDto> listUserToken (@Context SecurityContext context, |
| margaretha | 4993eb7 | 2023-09-27 10:54:34 +0200 | [diff] [blame] | 444 | @FormParam("super_client_id") String superClientId, |
| 445 | @FormParam("super_client_secret") String superClientSecret, |
| 446 | @FormParam("client_id") String clientId, // optional |
| 447 | @FormParam("token_type") String tokenType) { |
| 448 | |
| 449 | TokenContext tokenContext = (TokenContext) context.getUserPrincipal(); |
| 450 | String username = tokenContext.getUsername(); |
| 451 | |
| 452 | try { |
| 453 | if (tokenType.equals("access_token")) { |
| 454 | return tokenService.listUserAccessToken(username, superClientId, |
| 455 | superClientSecret, clientId); |
| 456 | } |
| 457 | else if (tokenType.equals("refresh_token")) { |
| 458 | return tokenService.listUserRefreshToken(username, |
| 459 | superClientId, superClientSecret, clientId); |
| 460 | } |
| 461 | else { |
| 462 | throw new KustvaktException(StatusCodes.MISSING_PARAMETER, |
| 463 | "Missing token_type parameter value", |
| 464 | OAuth2Error.INVALID_REQUEST); |
| 465 | } |
| 466 | } |
| 467 | catch (KustvaktException e) { |
| 468 | throw responseHandler.throwit(e); |
| 469 | } |
| 470 | |
| 471 | } |
| margaretha | 0e8f4e7 | 2018-04-05 14:11:52 +0200 | [diff] [blame] | 472 | } |