commit | author | age
|
177043
|
1 |
/* |
U |
2 |
neon-auth - Authentication Extensions to Neon |
|
3 |
Copyright (C) 2024 Ulrich Hilger |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.neon.auth; |
|
19 |
|
|
20 |
import com.sun.net.httpserver.Headers; |
|
21 |
import com.sun.net.httpserver.HttpExchange; |
|
22 |
import de.uhilger.neon.HttpResponder; |
|
23 |
import java.io.IOException; |
|
24 |
|
|
25 |
/** |
|
26 |
* BasisKlasse fuer Login und Refresh |
|
27 |
* |
|
28 |
* @author Ulrich Hilger |
|
29 |
*/ |
|
30 |
public class BearerService { |
|
31 |
|
|
32 |
public static final String CACHE_CONTROL = "Cache-Control"; |
|
33 |
public static final String NO_STORE = "no-store"; |
|
34 |
public static final String PRAGMA = "Pragma"; |
|
35 |
public static final String NO_CACHE = "no-cache"; |
|
36 |
public static final String BEARER_CONTENT_TYPE = "application/json;charset=UTF-8"; |
|
37 |
|
|
38 |
/** |
|
39 |
* Die Antwort des Authenticators auf eine Login-Anfrage verarbeiten |
|
40 |
* @param exchange das Objekt mit Informationen zu HTTP-Anfrage und -Antwort |
|
41 |
* @param response die Antwort des Autehnticators |
|
42 |
* @throws IOException |
|
43 |
*/ |
|
44 |
protected void handleLoginResponse(HttpExchange exchange, LoginResponse response) throws IOException { |
|
45 |
if(response != null) { |
|
46 |
setLoginHeader(exchange); |
|
47 |
HttpResponder r = new HttpResponder(); |
|
48 |
r.antwortSenden(exchange, 200, response.toJson()); |
|
49 |
} else { |
|
50 |
HttpResponder r = new HttpResponder(); |
|
51 |
r.antwortSenden(exchange, 406, "Login failed."); |
|
52 |
} |
|
53 |
} |
|
54 |
|
|
55 |
protected void setLoginHeader(HttpExchange exchange) { |
|
56 |
Headers headers = exchange.getResponseHeaders(); |
|
57 |
headers.add(HttpResponder.CONTENT_TYPE, BEARER_CONTENT_TYPE); |
|
58 |
headers.add(CACHE_CONTROL, NO_STORE); |
|
59 |
headers.add(PRAGMA, NO_CACHE); |
|
60 |
} |
|
61 |
|
|
62 |
|
|
63 |
} |