/* jwtTest - JSON Web Token Testimplementierung Copyright (C) 2021 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.httpserver.auth.handler; import com.google.gson.Gson; import com.sun.net.httpserver.Authenticator; import com.sun.net.httpserver.HttpExchange; import de.uhilger.httpserver.auth.ApiAuthenticator; import de.uhilger.httpserver.auth.realm.User; import de.uhilger.httpserver.base.handler.HttpHelper; import de.uhilger.httpserver.base.handler.HttpResponder; import java.io.IOException; import java.util.logging.Logger; /** * * @author Ulrich Hilger * @version 1, 30.05.2021 */ public class ApiLoginHandler extends LoginHandler { private static final Logger logger = Logger.getLogger(ApiLoginHandler.class.getName()); public ApiLoginHandler() { //this.ctx = ctx; } @Override protected void loginResponse(HttpExchange exchange, Authenticator auth, String token) throws IOException { if(auth instanceof ApiAuthenticator) { setAuthenticatedHeader(exchange, auth, token); logger.info("response ok"); HttpResponder responder = new HttpResponder(); responder.antwortSenden(exchange, 200, "ok"); } else { throw new IOException("Wrong authenticator type, should have been ApiAuthenticator."); } } @Override protected User getUser(HttpExchange exchange) throws IOException { /* Wenn ein JSON-Inhalt im Body uebermittelt wird, steht dort evtl. etwas wie {"name": "fred", "password": "secret"} das kann wie folgt gelesen werden */ String body = new HttpHelper().bodyLesen(exchange); Gson gson = new Gson(); User user = gson.fromJson(body, User.class); return user; } }