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

ru.foodtechlab.lib.auth.service.domain.token.entity.AccessTokenEntity 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.AccessTokenData;
import com.rcore.domain.security.model.CredentialDetails;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import ru.foodtechlab.abe.domain.entities.BaseDeleteEntity;
import ru.foodtechlab.lib.auth.service.domain.credential.entity.CredentialEntity;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.stream.Collectors;

/**
 * Токен авторизации
 */
@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AccessTokenEntity extends BaseDeleteEntity {
    private CredentialEntity credential;
    private Long ttl;
    private Instant expireAt = Instant.now();
    private RefreshTokenEntity.Status status = RefreshTokenEntity.Status.ACTIVE;
    private RefreshTokenEntity createByRefreshToken;
    private String sign;
    private String authSessionId;

    private static String hash(String st) {
        MessageDigest messageDigest = null;
        byte[] digest = new byte[0];

        try {
            messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.reset();
            messageDigest.update(st.getBytes());
            digest = messageDigest.digest();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }

        BigInteger bigInt = new BigInteger(1, digest);
        String md5Hex = bigInt.toString(16);

        while (md5Hex.length() < 32) {
            md5Hex = "0" + md5Hex;
        }

        return md5Hex;
    }

    public static String sign(String accessTokenId, Long expireAt, RefreshTokenEntity refreshTokenEntity) {
        String signString = refreshTokenEntity.getId() +
                refreshTokenEntity.getCredential().getId() +
                refreshTokenEntity.getExpireAt().toString() +
                refreshTokenEntity.getSalt() +
                expireAt.toString() +
                accessTokenId;

        return hash(signString);
    }

    public Boolean isActive() {
        if (this.status != RefreshTokenEntity.Status.ACTIVE) return false;
        if (Instant.now().isAfter(expireAt)) return false;

        return true;
    }

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

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

    public void refresh() {
        this.status = RefreshTokenEntity.Status.REFRESHED;
    }

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

    public AccessTokenData toAccessTokenData() {
        return new AccessTokenData(
                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