Search in sources :

Example 1 with OneTimePassword

use of org.maxkey.password.onetimepwd.OneTimePassword in project MaxKey by dromara.

the class InMemoryOtpTokenStore method store.

@Override
public void store(UserInfo userInfo, String token, String receiver, String type) {
    DateTime currentDateTime = new DateTime();
    OneTimePassword otp = new OneTimePassword();
    otp.setId(userInfo.getUsername() + "_" + type + "_" + token);
    otp.setType(type);
    otp.setUsername(userInfo.getUsername());
    otp.setToken(token);
    otp.setReceiver(receiver);
    otp.setCreateTime(currentDateTime.toString("yyyy-MM-dd HH:mm:ss"));
    optTokenStore.put(otp.getId(), otp);
}
Also used : OneTimePassword(org.maxkey.password.onetimepwd.OneTimePassword) DateTime(org.joda.time.DateTime)

Example 2 with OneTimePassword

use of org.maxkey.password.onetimepwd.OneTimePassword in project MaxKey by dromara.

the class RedisOtpTokenStore method validate.

@Override
public boolean validate(UserInfo userInfo, String token, String type, int interval) {
    RedisConnection conn = connectionFactory.getConnection();
    OneTimePassword otp = (OneTimePassword) conn.getObject(PREFIX + userInfo.getUsername() + "_" + type + "_" + token);
    conn.delete(PREFIX + userInfo.getUsername() + "_" + type + "_" + token);
    conn.close();
    if (otp != null) {
        return true;
    }
    return false;
}
Also used : OneTimePassword(org.maxkey.password.onetimepwd.OneTimePassword) RedisConnection(org.maxkey.persistence.redis.RedisConnection)

Example 3 with OneTimePassword

use of org.maxkey.password.onetimepwd.OneTimePassword in project MaxKey by dromara.

the class RedisOtpTokenStore method store.

@Override
public void store(UserInfo userInfo, String token, String receiver, String type) {
    DateTime currentDateTime = new DateTime();
    OneTimePassword otp = new OneTimePassword();
    otp.setId(userInfo.getUsername() + "_" + type + "_" + token);
    otp.setType(type);
    otp.setUsername(userInfo.getUsername());
    otp.setToken(token);
    otp.setReceiver(receiver);
    otp.setCreateTime(currentDateTime.toString("yyyy-MM-dd HH:mm:ss"));
    RedisConnection conn = connectionFactory.getConnection();
    conn.setexObject(PREFIX + otp.getId(), validitySeconds, otp);
    conn.close();
}
Also used : OneTimePassword(org.maxkey.password.onetimepwd.OneTimePassword) DateTime(org.joda.time.DateTime) RedisConnection(org.maxkey.persistence.redis.RedisConnection)

Example 4 with OneTimePassword

use of org.maxkey.password.onetimepwd.OneTimePassword in project MaxKey by dromara.

the class InMemoryOtpTokenStore method validate.

@Override
public boolean validate(UserInfo userInfo, String token, String type, int interval) {
    OneTimePassword otp = optTokenStore.getIfPresent(userInfo.getUsername() + "_" + type + "_" + token);
    if (otp != null) {
        DateTime currentdateTime = new DateTime();
        DateTime oneCreateTime = DateTime.parse(otp.getCreateTime(), DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
        Duration duration = new Duration(oneCreateTime, currentdateTime);
        int intDuration = Integer.parseInt(duration.getStandardSeconds() + "");
        logger.debug("validate duration " + intDuration);
        logger.debug("validate result " + (intDuration <= interval));
        if (intDuration <= interval) {
            return true;
        }
    }
    return false;
}
Also used : OneTimePassword(org.maxkey.password.onetimepwd.OneTimePassword) Duration(org.joda.time.Duration) DateTime(org.joda.time.DateTime)

Aggregations

OneTimePassword (org.maxkey.password.onetimepwd.OneTimePassword)4 DateTime (org.joda.time.DateTime)3 RedisConnection (org.maxkey.persistence.redis.RedisConnection)2 Duration (org.joda.time.Duration)1