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.HttpContext; |
|
21 |
import com.sun.net.httpserver.HttpExchange; |
|
22 |
import de.uhilger.neon.HttpHelper; |
|
23 |
import java.io.IOException; |
|
24 |
|
|
25 |
/** |
|
26 |
* Objekte der Klasse BearerRefrehService erlauben die Erneuerung |
|
27 |
* eines Token mit Hilfe eines BearerAuthenticators, der im |
|
28 |
* HttpContext des HttpExchange erwartet wird |
|
29 |
* |
|
30 |
* @author Ulrich Hilger |
|
31 |
*/ |
|
32 |
public class BearerRefreshService extends BearerService { |
|
33 |
|
|
34 |
public void refresh(HttpExchange exchange) throws IOException { |
|
35 |
HttpHelper h = new HttpHelper(); |
|
36 |
String body = h.bodyLesen(exchange); |
|
37 |
String[] parts = body.split("&"); |
|
38 |
for(String part : parts) { |
|
39 |
String[] keyVals = part.split("="); |
|
40 |
if(keyVals[0].equalsIgnoreCase("refresh_token")) { |
|
41 |
HttpContext context = exchange.getHttpContext(); |
|
42 |
Object o = context.getAuthenticator(); |
|
43 |
if (o instanceof BearerAuthenticator) { |
|
44 |
BearerAuthenticator auth = (BearerAuthenticator) o; |
|
45 |
String userId = auth.validateRefreshToken(keyVals[1]); |
|
46 |
if (userId != null) { |
|
47 |
LoginResponse r = new LoginResponse(); |
|
48 |
long expireSeconds = Long.parseLong((String) context |
|
49 |
.getAttributes().getOrDefault("expireSeconds", "7200")); |
|
50 |
String token = auth.createToken(userId, expireSeconds); |
|
51 |
r.setToken(token); |
|
52 |
r.setRefreshToken(auth.createToken(userId, |
|
53 |
Long.parseLong((String) context.getAttributes() |
|
54 |
.getOrDefault("refreshExpireSeconds", "86400")))); |
|
55 |
r.setExpiresIn(expireSeconds); |
|
56 |
handleLoginResponse(exchange, r); |
|
57 |
} else { |
|
58 |
handleLoginResponse(exchange, null); |
|
59 |
} |
|
60 |
} |
|
61 |
} |
|
62 |
} |
|
63 |
} |
|
64 |
|
|
65 |
/* |
|
66 |
private LoginResponse refresh(HttpExchange exchange, String refreshToken) { |
|
67 |
HttpContext context = exchange.getHttpContext(); |
|
68 |
Map attr = context.getAttributes(); |
|
69 |
Object o = context.getAuthenticator(); |
|
70 |
if (o instanceof BearerAuthenticator) { |
|
71 |
BearerAuthenticator auth = (BearerAuthenticator) o; |
|
72 |
String userId = auth.validateRefreshToken(refreshToken); |
|
73 |
if (userId != null) { |
|
74 |
LoginResponse r = new LoginResponse(); |
|
75 |
long expireSeconds = Long.parseLong((String) attr.getOrDefault("expireSeconds", "7200")); |
|
76 |
String token = auth.createToken(userId, expireSeconds); |
|
77 |
r.setToken(token); |
|
78 |
r.setRefreshToken(auth.createToken(userId, |
|
79 |
Long.parseLong((String) attr.getOrDefault("refreshExpireSeconds", "86400")))); |
|
80 |
r.setExpiresIn(expireSeconds); |
|
81 |
return r; |
|
82 |
} else { |
|
83 |
return null; |
|
84 |
} |
|
85 |
} else { |
|
86 |
return null; |
|
87 |
} |
|
88 |
} |
|
89 |
*/ |
|
90 |
} |