| | |
| | | import com.sun.net.httpserver.HttpHandler; |
| | | import de.uhilger.httpserver.auth.TokenAuthenticator; |
| | | import de.uhilger.httpserver.auth.realm.User; |
| | | import java.io.BufferedReader; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.io.InputStreamReader; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.Locale; |
| | |
| | | /* Der Logger fuer diesen JWTLoginHandler */ |
| | | //private static final Logger logger = Logger.getLogger(LoginHandler.class.getName()); |
| | | |
| | | public static final String ATTR_JWT_AUTH = "jwtauth"; |
| | | public static final String ATTR_AUTHENTICATOR = "authenticator"; |
| | | |
| | | |
| | | //protected String ctx; |
| | |
| | | public void handle(HttpExchange exchange) throws IOException { |
| | | User nutzer = getUser(exchange); |
| | | HttpContext context = exchange.getHttpContext(); |
| | | Object o = context.getAttributes().get(ATTR_JWT_AUTH); |
| | | Object o = context.getAttributes().get(ATTR_AUTHENTICATOR); |
| | | if (o instanceof TokenAuthenticator) { |
| | | TokenAuthenticator jwtAuth = (TokenAuthenticator) o; |
| | | String token = jwtAuth.anmelden(nutzer.getName(), nutzer.getPassword()); |
| | | TokenAuthenticator auth = (TokenAuthenticator) o; |
| | | String token = auth.anmelden(nutzer.getName(), nutzer.getPassword()); |
| | | if (token != null) { |
| | | loginResponse(exchange, jwtAuth, token); |
| | | loginResponse(exchange, auth, token); |
| | | } else { |
| | | // Nutzer und Kennwort passen nicht |
| | | } |
| | |
| | | |
| | | protected void setAuthenticatedHeader(HttpExchange exchange, Authenticator auth, String token) { |
| | | if(auth instanceof TokenAuthenticator) { |
| | | TokenAuthenticator jwtAuth = (TokenAuthenticator) auth; |
| | | TokenAuthenticator tAuth = (TokenAuthenticator) auth; |
| | | // angemeldet, Token als Antwort zurueckgeben |
| | | Headers respHeaders = exchange.getResponseHeaders(); |
| | | // JWT=[cookie-inhalt]; Expires=Thu, 21 Oct 2021 07:28:00 GMT; Secure; HttpOnly |
| | |
| | | new SimpleDateFormat(TokenAuthenticator.HEADER_DATE_PATTERN, Locale.US); |
| | | Date exp = Date.from(new Date().toInstant().plusSeconds(TokenAuthenticator.TOKEN_EXPIRATION)); |
| | | respHeaders.add(TokenAuthenticator.SET_COOKIE_HEADER, |
| | | jwtAuth.cookieBilden(TokenAuthenticator.JWT_INDICATOR, token, exp)); |
| | | tAuth.cookieBilden(TokenAuthenticator.JWT_INDICATOR, token, exp)); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | protected abstract User getUser(HttpExchange exchange) throws IOException; |
| | | |
| | | |
| | | /* |
| | | protected String bodyLesen(HttpExchange exchange) throws IOException { |
| | | StringBuilder sb = new StringBuilder(); |
| | | InputStream is = exchange.getRequestBody(); |
| | |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | */ |
| | | |
| | | } |