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

org.eu.vooo.commons.security.entity.XToken Maven / Gradle / Ivy

The newest version!
package org.eu.vooo.commons.security.entity;

import org.eu.vooo.commons.security.dto.impl.XLoginUserResDTO;
import org.eu.vooo.commons.security.util.JJWTUtils;

import java.io.Serializable;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Objects;

/**
 * @author [email protected]
 */
public class XToken implements Serializable {

    private String token;

    public XToken() {

    }

    private XToken(Builder builder) {
        this.token = builder.token;
    }

    public String getToken() {
        return token;
    }

    public static class Builder {

        private String privateKey;

        private String token;

        private Duration duration;

        private XLoginUserResDTO loginUser;

        public Builder privateKey(String privateKey) {
            this.privateKey = privateKey;
            return this;
        }

        public Builder duration(Duration duration) {
            this.duration = duration;
            return this;
        }

        public Builder loginUser(XLoginUserResDTO loginUserResDTO) {
            this.loginUser = loginUserResDTO;
            return this;
        }

        public XToken build() {
            this.token = JJWTUtils.generate(loginUser.getTenantId()
                    + ":" + loginUser.getAccessKey()
                    + ":" + loginUser.getUsername(), this.privateKey);
            if (Objects.nonNull(this.loginUser)) {
                this.loginUser.setToken(this.token);
                this.loginUser.setLoginTime(LocalDateTime.now());
                this.loginUser.setLoginTimestamp(this.loginUser.getLoginTime()
                        .atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
                this.loginUser.setExpiredTime(this.loginUser.getLoginTime().plus(this.duration.toMillis(),
                        ChronoUnit.MILLIS));
                this.loginUser.setExpiredTimestamp(this.loginUser.getExpiredTime()
                        .atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
            }
            return new XToken(this);
        }

        public void renew(XLoginUserResDTO loginUser) {
            loginUser.setExpiredTime(LocalDateTime.now().plus(duration.toMillis(), ChronoUnit.MILLIS));
            loginUser.setExpiredTimestamp(loginUser.getExpiredTime()
                    .atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
        }
    }

    public static class ParseBuilder {

        public String token;

        private String publicKey;

        public ParseBuilder token(String token) {
            this.token = token;
            return this;
        }

        public ParseBuilder publicKey(String publicKey) {
            this.publicKey = publicKey;
            return this;
        }

        public String build() {
            return JJWTUtils.parse(token, publicKey);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy