Skip to content

Commit 11bff8c

Browse files
sudharshanraja-dbdennygleejamieknight-db
authored
Expire cookie that was set as part of token endpoint during logout (unitycatalog#678)
**PR Checklist** - [x] A description of the changes is added to the description of this PR. - [ ] If there is a related issue, make sure it is linked to this PR. - [ ] If you've fixed a bug or added code that should be tested, add tests! - [ ] If you've added or modified a feature, documentation in `docs` is updated **Description of changes** This change is to expire cookie during logout by exposing logout endpoint , feature that was added as part of unitycatalog#542 and implemented as part of PR unitycatalog#593 --------- Signed-off-by: sudharshanraja-db <[email protected]> Co-authored-by: Denny Lee <[email protected]> Co-authored-by: Jamie Knight <[email protected]>
1 parent 6623469 commit 11bff8c

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

server/src/main/java/io/unitycatalog/server/service/AuthDecorator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public HttpResponse serve(HttpService delegate, ServiceRequestContext ctx, HttpR
6060
String authorizationHeader = req.headers().get(HttpHeaderNames.AUTHORIZATION);
6161
String authorizationCookie =
6262
req.headers().cookies().stream()
63-
.map(Cookie::name)
64-
.filter(name -> name.equals(UC_TOKEN_KEY))
63+
.filter(c -> c.name().equals(UC_TOKEN_KEY))
64+
.map(Cookie::value)
6565
.findFirst()
6666
.orElse(null);
6767

server/src/main/java/io/unitycatalog/server/service/AuthService.java

+24-4
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,30 @@ public HttpResponse grantToken(
167167
String cookieTimeout =
168168
ServerProperties.getInstance().getProperty("server.cookie-timeout", "P5D");
169169
Cookie cookie =
170-
Cookie.secureBuilder(AuthDecorator.UC_TOKEN_KEY, accessToken)
171-
.path("/")
172-
.maxAge(Duration.parse(cookieTimeout).getSeconds())
173-
.build();
170+
createCookie(AuthDecorator.UC_TOKEN_KEY, accessToken, "/", cookieTimeout);
174171
responseHeaders.add(HttpHeaderNames.SET_COOKIE, cookie.toSetCookieHeader());
175172
}
176173
});
177174

178175
return HttpResponse.ofJson(responseHeaders.build(), response);
179176
}
180177

178+
@Post("/logout")
179+
public HttpResponse logout(HttpRequest request) {
180+
return request.headers().cookies().stream()
181+
.filter(c -> c.name().equals(AuthDecorator.UC_TOKEN_KEY))
182+
.findFirst()
183+
.map(
184+
authorizationCookie -> {
185+
Cookie expiredCookie = createCookie(AuthDecorator.UC_TOKEN_KEY, "", "/", "PT0S");
186+
ResponseHeaders headers =
187+
ResponseHeaders.of(
188+
HttpStatus.OK, HttpHeaderNames.SET_COOKIE, expiredCookie.toSetCookieHeader());
189+
return HttpResponse.of(headers);
190+
})
191+
.orElse(HttpResponse.of(HttpStatus.OK));
192+
}
193+
181194
private static void verifyPrincipal(DecodedJWT decodedJWT) {
182195
String subject =
183196
decodedJWT.getClaim(JwtClaim.EMAIL.key()).isMissing()
@@ -201,6 +214,13 @@ private static void verifyPrincipal(DecodedJWT decodedJWT) {
201214
ErrorCode.INVALID_ARGUMENT, "User not allowed: " + subject);
202215
}
203216

217+
private Cookie createCookie(String key, String value, String path, String maxAge) {
218+
return Cookie.secureBuilder(key, value)
219+
.path(path)
220+
.maxAge(Duration.parse(maxAge).getSeconds())
221+
.build();
222+
}
223+
204224
// TODO: This should be probably integrated into the OpenAPI spec.
205225
@ToString
206226
static class OAuthTokenExchangeRequest {

0 commit comments

Comments
 (0)