/* neon-auth - Authentication Extensions to Neon Copyright (C) 2024 Ulrich Hilger This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ package de.uhilger.neon.auth; import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; import de.uhilger.neon.HttpResponder; import java.io.IOException; /** * BasisKlasse fuer Login und Refresh * * @author Ulrich Hilger */ public class BearerService { public static final String CACHE_CONTROL = "Cache-Control"; public static final String NO_STORE = "no-store"; public static final String PRAGMA = "Pragma"; public static final String NO_CACHE = "no-cache"; public static final String BEARER_CONTENT_TYPE = "application/json;charset=UTF-8"; /** * Die Antwort des Authenticators auf eine Login-Anfrage verarbeiten * @param exchange das Objekt mit Informationen zu HTTP-Anfrage und -Antwort * @param response die Antwort des Autehnticators * @throws IOException */ protected void handleLoginResponse(HttpExchange exchange, LoginResponse response) throws IOException { if(response != null) { setLoginHeader(exchange); HttpResponder r = new HttpResponder(); r.antwortSenden(exchange, 200, response.toJson()); } else { HttpResponder r = new HttpResponder(); r.antwortSenden(exchange, 406, "Login failed."); } } protected void setLoginHeader(HttpExchange exchange) { Headers headers = exchange.getResponseHeaders(); headers.add(HttpResponder.CONTENT_TYPE, BEARER_CONTENT_TYPE); headers.add(CACHE_CONTROL, NO_STORE); headers.add(PRAGMA, NO_CACHE); } }