package com.yami.shop.security.multishop.config;

import com.yami.shop.security.multishop.model.YamiShopUser;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * token增强
 * @author LGH
 */
@Component
public class AdminTokenEnhancer implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        final Map<String, Object> additionalInfo = new HashMap<>(8);
        YamiShopUser yamiShopUser = (YamiShopUser) authentication.getUserAuthentication().getDetails();
        additionalInfo.put("shopId", yamiShopUser.getShopId());
        additionalInfo.put("userId", yamiShopUser.getUserId());
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
        return accessToken;
    }
}
