Skip to content

Commit

Permalink
fix: remove multiple tokens from cookies (#2514)
Browse files Browse the repository at this point in the history
Signed-off-by: achmelo <[email protected]>
  • Loading branch information
achmelo authored Jul 21, 2022
1 parent e491d7c commit d5bc187
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class HttpBasicPassTicketScheme implements IAuthenticationScheme {
private final PassTicketService passTicketService;
private final AuthSourceService authSourceService;
private final String cookieName;
private final String patCookieName;

public HttpBasicPassTicketScheme(
PassTicketService passTicketService,
Expand All @@ -54,6 +55,7 @@ public HttpBasicPassTicketScheme(
this.passTicketService = passTicketService;
this.authSourceService = authSourceService;
cookieName = authConfigurationProperties.getCookieProperties().getCookieName();
patCookieName = authConfigurationProperties.getCookieProperties().getCookieNamePAT();
}

@Override
Expand Down Expand Up @@ -102,7 +104,7 @@ public AuthenticationCommand createCommand(Authentication authentication, AuthSo
final String value = "Basic " + encoded;
// passticket is valid only once, therefore this command needs to expire immediately and each call should generate new passticket
long expiration = System.currentTimeMillis();
return new PassTicketCommand(value, cookieName, expiration);
return new PassTicketCommand(value, cookieName, patCookieName, expiration);
}

@Override
Expand All @@ -120,14 +122,16 @@ public static class PassTicketCommand extends AuthenticationCommand {

String authorizationValue;
String cookieName;
String patCookieName;
Long expireAt;

@Override
public void apply(InstanceInfo instanceInfo) {
if (authorizationValue != null) {
final RequestContext context = RequestContext.getCurrentContext();
context.addZuulRequestHeader(HttpHeaders.AUTHORIZATION, authorizationValue);
JwtCommand.removeCookie(context, cookieName);
String[] cookiesToBeRemoved = new String[]{cookieName,patCookieName};
JwtCommand.removeCookie(context, cookiesToBeRemoved);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ public static void setCookie(RequestContext context, String name, String value)
);
}

public static void removeCookie(RequestContext context, String name) {
context.addZuulRequestHeader(COOKIE_HEADER,
CookieUtil.removeCookie(
context.getRequest().getHeader(COOKIE_HEADER),
public static void removeCookie(RequestContext context, String[] names) {
String cookie = context.getRequest().getHeader(COOKIE_HEADER);
for (String name : names) {
cookie = CookieUtil.removeCookie(
cookie,
name
)
);
}
context.addZuulRequestHeader(COOKIE_HEADER,
cookie
);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ public void apply(InstanceInfo instanceInfo) {
final RequestContext context = RequestContext.getCurrentContext();
// add header with SafIdt token to request and remove APIML token from Cookie if exists
context.addZuulRequestHeader(SAF_TOKEN_HEADER, safIdentityToken);
JwtCommand.removeCookie(context, authConfigurationProperties.getCookieProperties().getCookieName());
JwtCommand.removeCookie(context, authConfigurationProperties.getCookieProperties().getCookieNamePAT());
String[] cookiesToBeRemoved = new String[]{authConfigurationProperties.getCookieProperties().getCookieName(), authConfigurationProperties.getCookieProperties().getCookieNamePAT()};
JwtCommand.removeCookie(context, cookiesToBeRemoved);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ public void apply(InstanceInfo instanceInfo) {
final RequestContext context = RequestContext.getCurrentContext();
if (AuthSource.Origin.ZOSMF.equals(authSourceOrigin)) {
// token is generated by z/OSMF, fix set cookies
removeCookie(context, authConfigurationProperties.getCookieProperties().getCookieName());
removeCookie(context, authConfigurationProperties.getCookieProperties().getCookieNamePAT());
String[] cookiesToBeRemoved = new String[]{authConfigurationProperties.getCookieProperties().getCookieName(),authConfigurationProperties.getCookieProperties().getCookieNamePAT()};
removeCookie(context, cookiesToBeRemoved);
setCookie(context, ZosmfService.TokenType.JWT.getCookieName(), cookieValue);
} else if (AuthSource.Origin.ZOWE.equals(authSourceOrigin)) {
// user use Zowe own JWT token, for communication with z/OSMF there should be LTPA token, use it
Expand Down

0 comments on commit d5bc187

Please sign in to comment.