| | |
| | | import java.io.IOException; |
| | | import java.security.Key; |
| | | import java.util.Date; |
| | | import java.util.logging.Level; |
| | | import java.util.logging.Logger; |
| | | |
| | | /** |
| | | * Die Klasse Authenticator authentifziert gemäß OAuth-Spezifikation |
| | |
| | | * @version 1, 08.06.2021 |
| | | */ |
| | | public class BearerAuthenticator extends Authenticator { |
| | | |
| | | /** Der Logger dieser Klasse */ |
| | | private static final Logger logger = Logger.getLogger(BearerAuthenticator.class.getName()); |
| | | |
| | | public static final String STR_SLASH = "/"; |
| | | public static final String STR_BLANK = " "; |
| | |
| | | |
| | | @Override |
| | | public Result authenticate(HttpExchange exchange) { |
| | | logger.info(exchange.getRequestURI().toString()); |
| | | //logger.info(exchange.getRequestURI().toString()); |
| | | String jwt = getToken(exchange); |
| | | if(jwt.equals(STR_EMPTY)) { |
| | | return unauthorized(exchange); |
| | |
| | | Result result = new Authenticator.Success(pp); |
| | | return result; |
| | | } catch (Exception ex) { |
| | | logger.log(Level.SEVERE, null, ex); |
| | | //logger.log(Level.SEVERE, null, ex); |
| | | return new Authenticator.Failure(SC_UNAUTHORIZED); |
| | | } |
| | | } else { |
| | |
| | | public LoginResponse login(HttpExchange e, String userId, String password) { |
| | | if(canLogin()) { |
| | | if (realm.isValid(userId, password)) { |
| | | logger.info(userId + " logged in from IP." + e.getRemoteAddress()); |
| | | //logger.info(userId + " logged in from IP." + e.getRemoteAddress()); |
| | | LoginResponse r = new LoginResponse(); |
| | | String token = createToken(userId, expireSeconds); |
| | | r.setToken(token); |
| | |
| | | r.setExpiresIn(expireSeconds); |
| | | return r; |
| | | } else { |
| | | logger.info("Invalid log in attempt for " + userId + " from IP " + e.getRemoteAddress()); |
| | | //logger.info("Invalid log in attempt for " + userId + " from IP " + e.getRemoteAddress()); |
| | | return null; |
| | | } |
| | | } else { |
| | | logger.info("Log in attempt for " + userId + " from IP " + e.getRemoteAddress() + " rejected due to time limit."); |
| | | //logger.info("Log in attempt for " + userId + " from IP " + e.getRemoteAddress() + " rejected due to time limit."); |
| | | return null; |
| | | } |
| | | } |
| | |
| | | try { |
| | | r.antwortSenden(exchange, SC_UNAUTHORIZED, STR_EMPTY); |
| | | } catch (IOException ex) { |
| | | logger.log(Level.SEVERE, null, ex); |
| | | //logger.log(Level.SEVERE, null, ex); |
| | | } |
| | | return new Authenticator.Retry(SC_UNAUTHORIZED); |
| | | } |
| | |
| | | try { |
| | | r.antwortSenden(exchange, SC_UNAUTHORIZED, STR_EMPTY); |
| | | } catch (IOException ex) { |
| | | logger.log(Level.SEVERE, null, ex); |
| | | //logger.log(Level.SEVERE, null, ex); |
| | | } |
| | | return new Authenticator.Retry(SC_UNAUTHORIZED); |
| | | } |