package com.dsideal.sso.Util;

import com.jfinal.kit.PropKit;
import com.jfinal.plugin.redis.Redis;

import java.util.Map;

public class SsoLoginStore {
    public static Map<String, String> get(String sessionId) {
        String redisKey = redisKey(sessionId);
        Map<String,String> x = Redis.use().call(redis -> (redis.hgetAll(redisKey)));
        if (x != null) {
            x.put("session_id", sessionId);
            return x;
        }
        return null;
    }

    public static void put(String sessionId, Map<String, String> loginMap) {
        String redisKey = redisKey(sessionId);
        Redis.use().call(redis -> (redis.hmset(redisKey, loginMap)));
    }

    public static void remove(String sessionId) {
        String redisKey = redisKey(sessionId);
        Redis.use().call(redis -> (redis.del(redisKey)));
    }

    private static String redisKey(String sessionId) {
        return PropKit.get("sso.sessionid").concat("#").concat(sessionId);
    }
}
