package com.cim.idm.utils;

import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

@Component
public class TokenManager {
    private ConcurrentHashMap<String, Long> tokenStore = new ConcurrentHashMap<>();
    private long tokenValidity = TimeUnit.HOURS.toMillis(1); // 1小时有效期

    public String generateToken() {
        String token = UUID.randomUUID().toString();
        long expirationTime = System.currentTimeMillis() + tokenValidity;
        tokenStore.put(token, expirationTime);
        return token;
    }

    public boolean isValidToken(String token) {
        Long expirationTime = tokenStore.get(token);
        if (expirationTime == null) {
            return false;
        }
        return System.currentTimeMillis() < expirationTime;
    }

    public void removeToken(String token) {
        tokenStore.remove(token);
    }


}
