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.google.gson.Gson; |
|
21 |
import com.sun.net.httpserver.Authenticator; |
|
22 |
import com.sun.net.httpserver.HttpExchange; |
|
23 |
import de.uhilger.httpserver.auth.ApiAuthenticator; |
|
24 |
import de.uhilger.httpserver.auth.realm.User; |
6e87f8
|
25 |
import de.uhilger.httpserver.base.handler.HttpHelper; |
9ee357
|
26 |
import de.uhilger.httpserver.base.handler.HttpResponder; |
U |
27 |
import java.io.IOException; |
|
28 |
import java.util.logging.Logger; |
|
29 |
|
|
30 |
/** |
|
31 |
* |
|
32 |
* @author Ulrich Hilger |
|
33 |
* @version 1, 30.05.2021 |
|
34 |
*/ |
|
35 |
public class ApiLoginHandler extends LoginHandler { |
|
36 |
|
|
37 |
private static final Logger logger = Logger.getLogger(ApiLoginHandler.class.getName()); |
|
38 |
|
|
39 |
public ApiLoginHandler() { |
|
40 |
//this.ctx = ctx; |
|
41 |
} |
|
42 |
|
|
43 |
@Override |
|
44 |
protected void loginResponse(HttpExchange exchange, Authenticator auth, String token) throws IOException { |
|
45 |
if(auth instanceof ApiAuthenticator) { |
|
46 |
setAuthenticatedHeader(exchange, auth, token); |
|
47 |
logger.info("response ok"); |
|
48 |
HttpResponder responder = new HttpResponder(); |
|
49 |
responder.antwortSenden(exchange, 200, "ok"); |
|
50 |
} else { |
|
51 |
throw new IOException("Wrong authenticator type, should have been ApiAuthenticator."); |
|
52 |
} |
|
53 |
} |
|
54 |
|
|
55 |
@Override |
|
56 |
protected User getUser(HttpExchange exchange) throws IOException { |
|
57 |
/* |
|
58 |
Wenn ein JSON-Inhalt im Body uebermittelt wird, steht |
|
59 |
dort evtl. etwas wie |
|
60 |
{"name": "fred", "password": "secret"} |
|
61 |
das kann wie folgt gelesen werden |
|
62 |
*/ |
6e87f8
|
63 |
String body = new HttpHelper().bodyLesen(exchange); |
9ee357
|
64 |
Gson gson = new Gson(); |
U |
65 |
User user = gson.fromJson(body, User.class); |
|
66 |
return user; |
|
67 |
} |
|
68 |
|
|
69 |
} |