Repository: knox Updated Branches: refs/heads/master f389cab9e -> cfa28c766
KNOX-923 - Add ClientData to KnoxToken Service to Include in JSON Response Project: http://git-wip-us.apache.org/repos/asf/knox/repo Commit: http://git-wip-us.apache.org/repos/asf/knox/commit/cfa28c76 Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/cfa28c76 Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/cfa28c76 Branch: refs/heads/master Commit: cfa28c76644561b7921eaa3565b09f8fb26d32e4 Parents: f389cab Author: Larry McCay <[email protected]> Authored: Thu Apr 13 23:56:28 2017 -0400 Committer: Larry McCay <[email protected]> Committed: Thu Apr 13 23:56:28 2017 -0400 ---------------------------------------------------------------------- .../service/knoxtoken/TokenResource.java | 28 +++++++++-- .../knoxsso/TokenServiceResourceTest.java | 32 ------------ .../knoxtoken/TokenServiceResourceTest.java | 53 ++++++++++++++++++++ 3 files changed, 78 insertions(+), 35 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/knox/blob/cfa28c76/gateway-service-knoxtoken/src/main/java/org/apache/hadoop/gateway/service/knoxtoken/TokenResource.java ---------------------------------------------------------------------- diff --git a/gateway-service-knoxtoken/src/main/java/org/apache/hadoop/gateway/service/knoxtoken/TokenResource.java b/gateway-service-knoxtoken/src/main/java/org/apache/hadoop/gateway/service/knoxtoken/TokenResource.java index 726f664..3641400 100644 --- a/gateway-service-knoxtoken/src/main/java/org/apache/hadoop/gateway/service/knoxtoken/TokenResource.java +++ b/gateway-service-knoxtoken/src/main/java/org/apache/hadoop/gateway/service/knoxtoken/TokenResource.java @@ -20,6 +20,7 @@ package org.apache.hadoop.gateway.service.knoxtoken; import java.io.IOException; import java.security.Principal; import java.util.ArrayList; +import java.util.Map; import java.util.HashMap; import javax.annotation.PostConstruct; import javax.servlet.ServletContext; @@ -50,11 +51,13 @@ public class TokenResource { private static final String TOKEN_TTL_PARAM = "knox.token.ttl"; private static final String TOKEN_AUDIENCES_PARAM = "knox.token.audiences"; private static final String TOKEN_TARGET_URL = "knox.token.target.url"; + private static final String TOKEN_CLIENT_DATA = "knox.token.client.data"; static final String RESOURCE_PATH = "knoxtoken/api/v1/token"; private static TokenServiceMessages log = MessagesFactory.get( TokenServiceMessages.class ); private long tokenTTL = 30000l; private String[] targetAudiences = null; private String tokenTargetUrl = null; + private Map<String,Object> tokenClientDataMap = null; @Context private HttpServletRequest request; @@ -84,6 +87,13 @@ public class TokenResource { } tokenTargetUrl = context.getInitParameter(TOKEN_TARGET_URL); + + String clientData = context.getInitParameter(TOKEN_CLIENT_DATA); + if (clientData != null) { + tokenClientDataMap = new HashMap<String,Object>(); + String[] tokenClientData = clientData.split(","); + addClientDataToMap(tokenClientData, tokenClientDataMap); + } } @GET @@ -128,24 +138,36 @@ public class TokenResource { if (tokenTargetUrl != null) { map.put(TARGET_URL, tokenTargetUrl); } + if (tokenClientDataMap != null) { + map.putAll(tokenClientDataMap); + } String jsonResponse = JsonUtils.renderAsJsonString(map); - + response.getWriter().write(jsonResponse); return Response.ok().build(); } else { return Response.serverError().build(); } - } catch (TokenServiceException | IOException e) { log.unableToIssueToken(e); } - return Response.ok().entity("{ \"Unable to acquire token.\" }").build(); } + void addClientDataToMap(String[] tokenClientData, + Map<String,Object> map) { + String[] kv = null; + for (int i = 0; i < tokenClientData.length; i++) { + kv = tokenClientData[i].split("="); + if (kv.length == 2) { + map.put(kv[0], kv[1]); + } + } + } + private long getExpiry() { long expiry = 0l; if (tokenTTL == -1) { http://git-wip-us.apache.org/repos/asf/knox/blob/cfa28c76/gateway-service-knoxtoken/src/test/java/org/apache/hadoop/gateway/service/knoxsso/TokenServiceResourceTest.java ---------------------------------------------------------------------- diff --git a/gateway-service-knoxtoken/src/test/java/org/apache/hadoop/gateway/service/knoxsso/TokenServiceResourceTest.java b/gateway-service-knoxtoken/src/test/java/org/apache/hadoop/gateway/service/knoxsso/TokenServiceResourceTest.java deleted file mode 100644 index f9aa676..0000000 --- a/gateway-service-knoxtoken/src/test/java/org/apache/hadoop/gateway/service/knoxsso/TokenServiceResourceTest.java +++ /dev/null @@ -1,32 +0,0 @@ -/** - * 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.hadoop.gateway.service.knoxsso; - -import org.junit.Assert; -import org.junit.Test; - -/** - * - */ -public class TokenServiceResourceTest { - - @Test - public void testTokenService() throws Exception { - Assert.assertTrue(true); - } -} http://git-wip-us.apache.org/repos/asf/knox/blob/cfa28c76/gateway-service-knoxtoken/src/test/java/org/apache/hadoop/gateway/service/knoxtoken/TokenServiceResourceTest.java ---------------------------------------------------------------------- diff --git a/gateway-service-knoxtoken/src/test/java/org/apache/hadoop/gateway/service/knoxtoken/TokenServiceResourceTest.java b/gateway-service-knoxtoken/src/test/java/org/apache/hadoop/gateway/service/knoxtoken/TokenServiceResourceTest.java new file mode 100644 index 0000000..2f81af0 --- /dev/null +++ b/gateway-service-knoxtoken/src/test/java/org/apache/hadoop/gateway/service/knoxtoken/TokenServiceResourceTest.java @@ -0,0 +1,53 @@ +/** + * 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.hadoop.gateway.service.knoxtoken; + +import org.apache.hadoop.gateway.service.knoxtoken.TokenResource; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Map; +import java.util.HashMap; + +/** + * + */ +public class TokenServiceResourceTest { + + @Test + public void testTokenService() throws Exception { + Assert.assertTrue(true); + } + + @Test + public void testClientData() throws Exception { + TokenResource tr = new TokenResource(); + + Map<String,Object> clientDataMap = new HashMap<String,Object>(); + tr.addClientDataToMap("cookie.name=hadoop-jwt,test=value".split(","), clientDataMap); + Assert.assertTrue(clientDataMap.size() == 2); + + clientDataMap = new HashMap<String,Object>(); + tr.addClientDataToMap("cookie.name=hadoop-jwt".split(","), clientDataMap); + Assert.assertTrue(clientDataMap.size() == 1); + + clientDataMap = new HashMap<String,Object>(); + tr.addClientDataToMap("".split(","), clientDataMap); + Assert.assertTrue(clientDataMap.size() == 0); + } +}
