| | |
| | | private long refreshSeconds; |
| | | |
| | | private long refreshExpire; |
| | | |
| | | |
| | | private long lastLoginTime; |
| | | |
| | | private long loginTimeLimit; |
| | | |
| | | public BearerAuthenticator() { |
| | | key = Keys.secretKeyFor(SignatureAlgorithm.HS256); |
| | | } |
| | | lastLoginTime = 0; |
| | | } |
| | | |
| | | @Override |
| | | public Result authenticate(HttpExchange exchange) { |
| | |
| | | * @return Token oder null, wenn die Anmeldung misslang |
| | | */ |
| | | public LoginResponse login(HttpExchange e, String userId, String password) { |
| | | if (realm.isValid(userId, password)) { |
| | | logger.info(userId + " logged in from IP." + e.getRemoteAddress()); |
| | | LoginResponse r = new LoginResponse(); |
| | | String token = createToken(userId, expireSeconds); |
| | | r.setToken(token); |
| | | r.setRefreshToken(createToken(userId, refreshExpire)); |
| | | r.setExpiresIn(expireSeconds); |
| | | return r; |
| | | if(canLogin()) { |
| | | if (realm.isValid(userId, password)) { |
| | | logger.info(userId + " logged in from IP." + e.getRemoteAddress()); |
| | | LoginResponse r = new LoginResponse(); |
| | | String token = createToken(userId, expireSeconds); |
| | | r.setToken(token); |
| | | r.setRefreshToken(createToken(userId, refreshExpire)); |
| | | r.setExpiresIn(expireSeconds); |
| | | return r; |
| | | } else { |
| | | logger.info("Invalid log in attempt for " + userId + " from IP " + e.getRemoteAddress()); |
| | | return null; |
| | | } |
| | | } else { |
| | | logger.info("Invalid log in attempt for " + userId + " from IP " + e.getRemoteAddress()); |
| | | logger.info("Log in attempt for " + userId + " from IP " + e.getRemoteAddress() + " rejected due to time limit."); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | private boolean canLogin() { |
| | | boolean doLogin = false; |
| | | if(loginTimeLimit > 0 ) { |
| | | Date now = new Date(); |
| | | long nowTimeMillis = now.getTime(); |
| | | long diff = nowTimeMillis - lastLoginTime; |
| | | if(loginTimeLimit < diff) { |
| | | lastLoginTime = nowTimeMillis; |
| | | doLogin = true; |
| | | } |
| | | } else { |
| | | doLogin = true; |
| | | } |
| | | return doLogin; |
| | | } |
| | | |
| | | public LoginResponse refresh(String refreshToken) { |
| | |
| | | public void setRefreshExpireSeconds(long seconds) { |
| | | this.refreshExpire = seconds; |
| | | } |
| | | |
| | | public long getLoginTimeLimit() { |
| | | return loginTimeLimit / 1000; |
| | | } |
| | | |
| | | /** |
| | | * Anzahl Sekunden, die zwischen einzelnen Logins gewartet werden soll |
| | | * |
| | | * @param loginTimeLimit Wartezeit bis zum nächsten Login in Sekunden, |
| | | * 0 = keine Verzoegerung |
| | | */ |
| | | public void setLoginTimeLimit(long loginTimeLimit) { |
| | | this.loginTimeLimit = loginTimeLimit * 1000; |
| | | } |
| | | |
| | | |
| | | } |