AuthUserController.java

/*
 * Copyright (c) 2007-2026 MetaSolutions AB
 *
 * Licensed 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.entrystore.rest.springboot.controller;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.entrystore.rest.springboot.filter.CacheControlFilter;
import org.entrystore.rest.springboot.model.api.DeleteAuthTokenRequestBody;
import org.entrystore.rest.springboot.model.api.GetAuthUserResponse;
import org.entrystore.rest.springboot.model.auth.SessionInfo;
import org.entrystore.rest.springboot.security.AuthTokenCookies;
import org.entrystore.rest.springboot.service.TokenService;
import org.entrystore.rest.springboot.service.UserService;
import org.entrystore.rest.springboot.util.SessionIdExceptions;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor
public class AuthUserController {

	private final UserService userService;
	private final TokenService tokenService;
	private final AuthTokenCookies authTokenCookies;

	@Operation(summary = "Provides basic information about the currently logged-in user.")
	@GetMapping(path = "/auth/user", produces = MediaType.APPLICATION_JSON_VALUE)
	public GetAuthUserResponse userInfo(
			@RequestHeader(name = HttpHeaders.ACCEPT_LANGUAGE, required = false) String acceptLanguage,
			HttpServletRequest request
	) {
		// Read the existing session without creating one: a guest must not get a session (and thus no
		// auth_token cookie / Cache-Control). The expiry is only reported for authenticated, non-guest users.
		HttpSession session = request.getSession(false);
		return userService.getUserInfo(acceptLanguage, session != null ? expiryOf(session) : null);
	}

	/** @return when the session's login ends, or null if a concurrent request has just ended the session */
	private Instant expiryOf(HttpSession session) {
		try {
			return authTokenCookies.expiry(session);
		} catch (IllegalStateException e) {
			if (!SessionIdExceptions.foundIn(e)) {
				throw e;
			}
			return null;
		}
	}

	@Operation(summary = "Lists the active sessions of the currently logged-in user by session handle; "
			+ "the session making the request is marked as current.")
	@GetMapping(path = "/auth/tokens", produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, SessionInfo>> tokensInfo(HttpServletRequest request) {
		// Without creating a session: an HTTP Basic request has none, and then no listed session is current
		HttpSession session = request.getSession(false);
		return ResponseEntity.ok()
				.header(HttpHeaders.CACHE_CONTROL, CacheControlFilter.CACHE_CONTROL_CREDENTIALS)
				.body(tokenService.getTokens(session != null ? session.getId() : null));
	}

	@Operation(summary = "Ends the session of the currently logged-in user that has the given session handle.")
	@DeleteMapping(path = "/auth/tokens", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Void> deleteToken(
			@Valid @RequestBody DeleteAuthTokenRequestBody body
	) {

		tokenService.deleteToken(body.token());

		return ResponseEntity
				.noContent()
				.build();
	}
}