commit | author | age
|
9ee357
|
1 |
/* |
U |
2 |
jwtTest - JSON Web Token Testimplementierung |
|
3 |
Copyright (C) 2021 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.httpserver.auth.handler; |
|
19 |
|
|
20 |
import com.sun.net.httpserver.Authenticator; |
|
21 |
import com.sun.net.httpserver.Headers; |
|
22 |
import com.sun.net.httpserver.HttpContext; |
|
23 |
import com.sun.net.httpserver.HttpExchange; |
|
24 |
import com.sun.net.httpserver.HttpHandler; |
|
25 |
import de.uhilger.httpserver.auth.TokenAuthenticator; |
|
26 |
import de.uhilger.httpserver.auth.realm.User; |
|
27 |
import java.io.IOException; |
|
28 |
import java.text.SimpleDateFormat; |
|
29 |
import java.util.Date; |
|
30 |
import java.util.Locale; |
|
31 |
|
|
32 |
/** |
|
33 |
* |
|
34 |
* @author Ulrich Hilger |
|
35 |
* @version 1, 30.05.2021 |
|
36 |
*/ |
|
37 |
public abstract class LoginHandler implements HttpHandler { |
|
38 |
|
|
39 |
/* Der Logger fuer diesen JWTLoginHandler */ |
|
40 |
//private static final Logger logger = Logger.getLogger(LoginHandler.class.getName()); |
|
41 |
|
6e87f8
|
42 |
public static final String ATTR_AUTHENTICATOR = "authenticator"; |
9ee357
|
43 |
|
U |
44 |
|
|
45 |
//protected String ctx; |
|
46 |
|
|
47 |
/** |
|
48 |
* Wenn einfach ein HTML-Formular hierhin geschickt wird, das wie |
|
49 |
* bei Java die Formular-Eingabefelder 'j_username' und 'j_password' |
|
50 |
* enthaelt, kommt im Body folgendes an: |
|
51 |
* j_username=name&j_password=password |
|
52 |
* |
|
53 |
* body koennte auch einen JSON-Ausdruck enthalten wie z.B. |
|
54 |
* {"name": "fred", "password": "secret"} |
|
55 |
* |
|
56 |
* Das ist hier noch nicht implementiert |
|
57 |
* |
|
58 |
* @param exchange |
|
59 |
* @throws IOException |
|
60 |
*/ |
|
61 |
@Override |
|
62 |
public void handle(HttpExchange exchange) throws IOException { |
|
63 |
User nutzer = getUser(exchange); |
|
64 |
HttpContext context = exchange.getHttpContext(); |
6e87f8
|
65 |
Object o = context.getAttributes().get(ATTR_AUTHENTICATOR); |
9ee357
|
66 |
if (o instanceof TokenAuthenticator) { |
6e87f8
|
67 |
TokenAuthenticator auth = (TokenAuthenticator) o; |
U |
68 |
String token = auth.anmelden(nutzer.getName(), nutzer.getPassword()); |
9ee357
|
69 |
if (token != null) { |
6e87f8
|
70 |
loginResponse(exchange, auth, token); |
9ee357
|
71 |
} else { |
U |
72 |
// Nutzer und Kennwort passen nicht |
|
73 |
} |
|
74 |
} else { |
|
75 |
// interner Fehler: Kein passender Authenticator |
|
76 |
} |
|
77 |
} |
|
78 |
|
|
79 |
protected void setAuthenticatedHeader(HttpExchange exchange, Authenticator auth, String token) { |
|
80 |
if(auth instanceof TokenAuthenticator) { |
6e87f8
|
81 |
TokenAuthenticator tAuth = (TokenAuthenticator) auth; |
9ee357
|
82 |
// angemeldet, Token als Antwort zurueckgeben |
U |
83 |
Headers respHeaders = exchange.getResponseHeaders(); |
|
84 |
// JWT=[cookie-inhalt]; Expires=Thu, 21 Oct 2021 07:28:00 GMT; Secure; HttpOnly |
|
85 |
SimpleDateFormat f = |
|
86 |
new SimpleDateFormat(TokenAuthenticator.HEADER_DATE_PATTERN, Locale.US); |
|
87 |
Date exp = Date.from(new Date().toInstant().plusSeconds(TokenAuthenticator.TOKEN_EXPIRATION)); |
|
88 |
respHeaders.add(TokenAuthenticator.SET_COOKIE_HEADER, |
6e87f8
|
89 |
tAuth.cookieBilden(TokenAuthenticator.JWT_INDICATOR, token, exp)); |
9ee357
|
90 |
} |
U |
91 |
} |
|
92 |
|
|
93 |
protected abstract void loginResponse(HttpExchange exchange, Authenticator auth, String token) throws IOException; |
|
94 |
|
|
95 |
protected abstract User getUser(HttpExchange exchange) throws IOException; |
|
96 |
|
6e87f8
|
97 |
/* |
9ee357
|
98 |
protected String bodyLesen(HttpExchange exchange) throws IOException { |
U |
99 |
StringBuilder sb = new StringBuilder(); |
|
100 |
InputStream is = exchange.getRequestBody(); |
|
101 |
BufferedReader in = new BufferedReader(new InputStreamReader(is)); |
|
102 |
String line = in.readLine(); |
|
103 |
while (line != null) { |
|
104 |
sb.append(line); |
|
105 |
line = in.readLine(); |
|
106 |
} |
|
107 |
return sb.toString(); |
|
108 |
} |
6e87f8
|
109 |
*/ |
9ee357
|
110 |
|
U |
111 |
} |