lmccay commented on code in PR #900: URL: https://github.com/apache/knox/pull/900#discussion_r1564248082
########## gateway-service-knoxtoken/src/main/java/org/apache/knox/gateway/service/knoxtoken/OAuthResource.java: ########## @@ -0,0 +1,133 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.knox.gateway.service.knoxtoken; + +import org.apache.knox.gateway.i18n.messages.MessagesFactory; +import org.apache.knox.gateway.util.JsonUtils; + +import javax.inject.Singleton; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Response; + +import java.time.Duration; +import java.time.format.DateTimeParseException; +import java.util.HashMap; + +import static javax.ws.rs.core.MediaType.APPLICATION_JSON; +import static javax.ws.rs.core.MediaType.APPLICATION_XML; + +@Singleton +@Path(OAuthResource.RESOURCE_PATH) +public class OAuthResource extends TokenResource { + private static TokenServiceMessages log = MessagesFactory.get(TokenServiceMessages.class); + static final String RESOURCE_PATH = "/{serviceName:.*}/v1/{oauthSegment:(oauth|token)}{path:(/tokens)?}"; + public static final String ISSUED_TOKEN_TYPE = "issued_token_type"; + public static final String REFRESH_TOKEN = "refresh_token"; + public static final String ISSUED_TOKEN_TYPE_ACCESS_TOKEN_VALUE = "urn:ietf:params:oauth:token-type:access_token"; + + @Override + @GET + @Produces({ APPLICATION_JSON, APPLICATION_XML }) + public Response doGet() { + return super.doGet(); + } + + @Override + @POST + @Produces({ APPLICATION_JSON, APPLICATION_XML }) + public Response doPost() { + return super.doPost(); + } + + @Override + public Response getAuthenticationToken() { + + Response response = enforceClientCertIfRequired(); + if (response != null) { return response; } + + response = onlyAllowGroupsToBeAddedWhenEnabled(); + if (response != null) { return response; } + + UserContext context = buildUserContext(request); + + response = enforceTokenLimitsAsRequired(context.userName); + if (response != null) { return response; } + + TokenResponse resp = getTokenResponse(context); + // if the responseMap isn't null then the knoxtoken request was successful + // if not then there may have been an error and the underlying response + // builder will communicate those details + if (resp.responseMap != null) { + // let's get the subset of the KnoxToken Response needed for OAuth + String accessToken = resp.responseMap.accessToken; + String passcode = resp.responseMap.passcode; + long expires = (long) resp.responseMap.map.get(EXPIRES_IN); + String tokenType = (String) resp.responseMap.map.get(TOKEN_TYPE); + + // build and return the expected OAuth response + final HashMap<String, Object> map = new HashMap<>(); + map.put(ACCESS_TOKEN, accessToken); + map.put(TOKEN_TYPE, tokenType); + map.put(EXPIRES_IN, expires); + map.put(ISSUED_TOKEN_TYPE, ISSUED_TOKEN_TYPE_ACCESS_TOKEN_VALUE); + // let's use the passcode as the refresh token + map.put(REFRESH_TOKEN, passcode); + String jsonResponse = JsonUtils.renderAsJsonString(map); + return resp.responseBuilder.entity(jsonResponse).build(); + } + // there was an error if we got here - let's surface it appropriately + // TODO: LJM we may need to translate certain errors into OAuth error messages Review Comment: Yes. Haven't determined whether this is the case yet and would like to follow up once I do but didn't want to block the overall feature. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@knox.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org