OAuth-Unterstuetzung fuer jdk.httpserver
ulrich
2021-06-11 4bf4d11154ddb0ecc5057bdcb7485867064a37de
commit | author | age
7ecde3 1 /*
U 2   http-oauth - OAuth Extensions to jdk.httpserver
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.oauth;
19
20 import com.sun.net.httpserver.HttpContext;
21 import com.sun.net.httpserver.HttpExchange;
4bf4d1 22 import de.uhilger.httpserver.base.HttpHelper;
7ecde3 23 import static de.uhilger.httpserver.oauth.BearerLoginHandler.ATTR_AUTHENTICATOR;
U 24 import java.io.IOException;
25
26 /**
9dc286 27  * Einen abgelaufenen Token mit Hilfe eines Refresh Token erneuern
7ecde3 28  * 
9dc286 29  * Gemaess
U 30  * https://www.oauth.com/oauth2-servers/making-authenticated-requests/refreshing-an-access-token/
31  * sieht die HTTP Anfrage zum Refresh wie folgt aus:
32  * 
33  * POST /oauth/token HTTP/1.1
34  * Host: authorization-server.com
35  *
36  * grant_type=refresh_token
37  * &amp;refresh_token=xxxxxxxxxxx
38  * &amp;client_id=xxxxxxxxxx
39  * &amp;client_secret=xxxxxxxxxx
7ecde3 40  * 
U 41  * @author Ulrich Hilger
42  * @version 1, 08.06.2021
43  */
44 public class BearerRefreshHandler extends BearerLoginHandler {
45
46   @Override
47   public void handle(HttpExchange exchange) throws IOException {
48     HttpHelper h = new HttpHelper();
49     String body = h.bodyLesen(exchange);
50     String[] parts = body.split("&");
51     for(String part : parts) {
52       String[] keyVals = part.split("=");
53       if(keyVals[0].equalsIgnoreCase("refresh_token")) {
54         HttpContext context = exchange.getHttpContext();
55         Object o = context.getAttributes().get(ATTR_AUTHENTICATOR);
56         if (o instanceof BearerAuthenticator) {
57           BearerAuthenticator auth = (BearerAuthenticator) o;
58           LoginResponse response = auth.refresh(keyVals[1]);
59           handleLoginResponse(exchange, response);
60         }        
61       } 
62     }            
63   }
64   
65 }