Skip to content

Commit 8bee331

Browse files
committed
include the header X-API-KEY in the session lookup chain
1 parent b57ed5b commit 8bee331

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

logicaldoc-core/src/main/java/com/logicaldoc/core/security/SessionManager.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ public Session getSession(HttpServletRequest request) {
451451
* <li>Request attribute <code>PARAM_SID</code></li>
452452
* <li>Session attribute <code>PARAM_SID</code></li>
453453
* <li>Cookie <code>COOKIE_SID</code></li>
454+
* <li>Header <code>X-API-KEY</code></li>
454455
* <li>Spring SecurityContextHolder</li>
455456
* <li>Client ID</li>
456457
* </ol>
@@ -522,9 +523,26 @@ else if (request.getSession(true).getAttribute(PARAM_SID) != null
522523
else
523524
sid = getSessionIdFromCookie(request);
524525

526+
if (StringUtils.isEmpty(sid))
527+
sid = getSessionFromApiKey(request);
528+
525529
return sid;
526530
}
527531

532+
private String getSessionFromApiKey(HttpServletRequest request) {
533+
try {
534+
if (StringUtils.isNotEmpty(request.getHeader(HEADER_APIKEY))) {
535+
String apiKey = CryptUtil.encryptSHA256(request.getHeader(HEADER_APIKEY));
536+
return getSessions().stream().filter(s -> apiKey.equals(s.getKey())).map(s -> s.getSid()).findFirst()
537+
.orElse(null);
538+
}
539+
} catch (NoSuchAlgorithmException e) {
540+
log.warn(e.getMessage(), e);
541+
}
542+
543+
return null;
544+
}
545+
528546
private String getSessionIdFromCookie(HttpServletRequest request) {
529547
Cookie[] cookies = request.getCookies();
530548
if (cookies != null)

0 commit comments

Comments
 (0)