package com.css.txw.sso.util;

import com.css.ggzc.framework.common.util.gy.GyUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * 安全服务工具类
 */
public class SecurityFrameworkUtils {

    private static final String AUTHORIZATION_HEADER = "Authorization";

    private static final String AUTHORIZATION_BEARER = "Bearer";

    private static final String LOGIN_USER_HEADER = "login-user";

    private static final String LOGIN_USER_ID_ATTR = "login-user-id";
    private static final String LOGIN_USER_TYPE_ATTR = "login-user-type";

    private SecurityFrameworkUtils() {}

    public static String obtainAuthorization(String cookeiKey,HttpServletRequest request) {
        String token = "";
        final Cookie[] cookies = request.getCookies();
        if (!GyUtils.isNull(cookies)){
            for (Cookie cookie : cookies) {
                final String name = cookie.getName();
                if (cookeiKey.equals(name)){
                    token = cookie.getValue();
                    break;
                }
            }
        }
        if (GyUtils.isNull(token)){
            token = request.getHeader(AUTHORIZATION_HEADER);
        }

        if (!StringUtils.hasText(token)) {
            return null;
        }
        final int length = token.length();
        return token.substring(length - 32);
    }

}
