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

ru.foodtechlab.lib.auth.service.domain.token.entity.RefreshTokenEntity Maven / Gradle / Ivy

There is a newer version: 4.1.0
Show newest version
package ru.foodtechlab.lib.auth.service.domain.token.entity;

import com.rcore.domain.security.model.CredentialDetails;
import com.rcore.domain.security.model.RefreshTokenData;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import ru.foodtechlab.abe.domain.entities.BaseDeleteEntity;
import ru.foodtechlab.lib.auth.service.domain.credential.entity.CredentialEntity;

import java.time.Instant;
import java.util.stream.Collectors;

/**
 * Токен для обновления токена авторизации
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class RefreshTokenEntity extends BaseDeleteEntity {

    private CredentialEntity credential;
    private Long ttl;
    private Instant expireAt = Instant.now();
    private Status status = Status.ACTIVE;

    private RefreshTokenEntity createFromToken;
    private CreateFrom createFromType;
    private String salt;
    private String authSessionId;

    public enum CreateFrom {
        LOGIN, REFRESH;
    }

    public enum Status {
        ACTIVE, INACTIVE, EXPIRED, REFRESHED
    }

    public Boolean isActive() {
        if (!status.equals(Status.ACTIVE)) return false;
        return !Instant.now().isAfter(expireAt);
    }

    public Boolean isExpired() {
        return this.status == RefreshTokenEntity.Status.EXPIRED || Instant.now().isAfter(expireAt);
    }

    public void deactivate() {
        this.status = Status.INACTIVE;
    }

    public void expire() {
        this.status = Status.EXPIRED;
    }

    public RefreshTokenData toRefreshTokenData() {
        return new RefreshTokenData(
                this.getId(),
                this.getCredential().getId(),
                this.getCredential().getRoles()
                        .stream()
                        .map(role -> new CredentialDetails.Role(role.getRole().getId(), role.getRole().getCode()))
                        .collect(Collectors.toList()),
                this.getCreatedAt(),
                this.getExpireAt());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy