All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.lsn.spring.auth.entity.ApiTokenImpl Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
package io.lsn.spring.auth.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.lsn.spring.auth.HashGenerator;
import io.lsn.spring.utilities.json.serializer.CustomTimestampDeserializer;
import io.lsn.spring.utilities.json.serializer.CustomTimestampSerializer;

import java.util.*;

/**
 * @author Patryk Szlagowski 
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class ApiTokenImpl implements ApiToken {

    private String token;
    @JsonSerialize(using = CustomTimestampSerializer.class)
    @JsonDeserialize(using = CustomTimestampDeserializer.class)
    private Date validDate;
    private Long userId;
    @JsonSerialize(using = CustomTimestampSerializer.class)
    @JsonDeserialize(using = CustomTimestampDeserializer.class)
    private Date creationTime;

    @Override
    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    @Override
    public Date getValidDate() {
        return validDate;
    }

    public void setValidDate(Date validDate) {
        this.validDate = validDate;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    @Override
    public Date getCreationTime() {
        return creationTime;
    }

    public void setCreationTime(Date creationTime) {
        this.creationTime = creationTime;
    }

    @Override
    public void generate(User user) throws Exception {
        List key = Arrays.asList(user.getUsername(), UUID.randomUUID().toString());
        this.token = HashGenerator.md5(String.join(".", key));
    }

    @Override
    public void calculateDate(Integer minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, minutes);
        this.setValidDate(calendar.getTime());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy