Skip to content

Commit fa75d21

Browse files
authored
KNOX-3295: Set default type to JWT in issued knox token headers (#1195)
* KNOX-3295: Set default type to JWT in issued knox token headers * KNOX-3295: Simplify new unit tests
1 parent 2fed77b commit fa75d21

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

gateway-service-knoxtoken/src/main/java/org/apache/knox/gateway/service/knoxtoken/TokenResource.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public class TokenResource {
121121
protected static final String BEARER = "Bearer";
122122
private static final String TOKEN_PARAM_PREFIX = "knox.token.";
123123
protected static final String TOKEN_TTL_PARAM = TOKEN_PARAM_PREFIX + "ttl";
124-
private static final String TOKEN_TYPE_PARAM = TOKEN_PARAM_PREFIX + "type";
124+
public static final String TOKEN_TYPE_PARAM = TOKEN_PARAM_PREFIX + "type";
125125
private static final String TOKEN_AUDIENCES_PARAM = TOKEN_PARAM_PREFIX + "audiences";
126126
public static final String TOKEN_INCLUDE_GROUPS_IN_JWT_ALLOWED = TOKEN_PARAM_PREFIX + "include.groups.allowed";
127127
private static final String TOKEN_TARGET_URL = TOKEN_PARAM_PREFIX + "target.url";
@@ -272,7 +272,9 @@ public void init() throws AliasServiceException, ServiceLifecycleException, KeyL
272272
this.tokenIssuer = StringUtils.isBlank(context.getInitParameter(KNOX_TOKEN_ISSUER))
273273
? JWTokenAttributes.DEFAULT_ISSUER
274274
: context.getInitParameter(KNOX_TOKEN_ISSUER);
275-
this.tokenType = context.getInitParameter(TOKEN_TYPE_PARAM);
275+
this.tokenType = StringUtils.isBlank(context.getInitParameter(TOKEN_TYPE_PARAM))
276+
? JWTokenAttributes.DEFAULT_TYPE
277+
: context.getInitParameter(TOKEN_TYPE_PARAM);
276278

277279
tokenTTLAsText = getTokenTTLAsText();
278280

gateway-service-knoxtoken/src/test/java/org/apache/knox/gateway/service/knoxtoken/TokenServiceResourceTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ public void testGetToken() throws Exception {
307307
// Verify the token
308308
JWT parsedToken = new JWTToken(accessToken);
309309
assertEquals("alice", parsedToken.getSubject());
310+
assertTrue(parsedToken.getHeader().contains("\"typ\":\"JWT\""));
310311
assertTrue(authority.verifyToken(parsedToken));
311312
}
312313

@@ -514,6 +515,32 @@ public void testSignatureAlgorithm() throws Exception {
514515
assertTrue(parsedToken.getHeader().contains("RS512"));
515516
}
516517

518+
@Test
519+
public void testCustomTokenType() throws Exception {
520+
final Map<String, String> contextExpectations = new HashMap<>();
521+
contextExpectations.put(TokenResource.TOKEN_TYPE_PARAM, "custom-type");
522+
configureCommonExpectations(contextExpectations);
523+
524+
TokenResource tr = new TokenResource();
525+
tr.request = request;
526+
tr.context = context;
527+
tr.init();
528+
529+
// Issue a token
530+
Response retResponse = tr.doGet();
531+
532+
assertEquals(200, retResponse.getStatus());
533+
534+
// Parse the response
535+
String retString = retResponse.getEntity().toString();
536+
String accessToken = getTagValue(retString, "access_token");
537+
assertNotNull(accessToken);
538+
539+
// Verify the token
540+
JWT parsedToken = new JWTToken(accessToken);
541+
assertTrue(parsedToken.getHeader().contains("\"typ\":\"custom-type\""));
542+
}
543+
517544
@Test
518545
public void testDefaultTTL() throws Exception {
519546
final Map<String, String> contextExpectations = new HashMap<>();

gateway-spi/src/main/java/org/apache/knox/gateway/services/security/token/JWTokenAttributes.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
public class JWTokenAttributes {
2727
public static final String DEFAULT_ISSUER = "KNOXSSO";
28+
public static final String DEFAULT_TYPE = "JWT";
2829
private final String userName;
2930
private final List<String> audiences;
3031
private final String algorithm;

0 commit comments

Comments
 (0)