package com.nantian.sc.cloud.common.security.component;

import com.nantian.sc.cloud.common.core.constant.SecurityConstants;
import com.nantian.sc.cloud.common.security.service.ScUser;
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 java.util.HashMap;
import java.util.Map;

/**
 *
 * @date 2019-09-17
 * <p>
 * token增强，客户端模式不增强。
 */
public class ScTokenEnhancer implements TokenEnhancer {
	/**
	 * Provides an opportunity for customization of an access token (e.g. through its additional information map) during
	 * the process of creating a new token for use by a client.
	 *
	 * @param accessToken    the current access token with its expiration and refresh token
	 * @param authentication the current authentication including client and user details
	 * @return a new token enhanced with additional information
	 */
	@Override
	public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
		if (SecurityConstants.CLIENT_CREDENTIALS
				.equals(authentication.getOAuth2Request().getGrantType())) {
			return accessToken;
		}

		final Map<String, Object> additionalInfo = new HashMap<>(8);
		ScUser scUser = (ScUser) authentication.getUserAuthentication().getPrincipal();
		additionalInfo.put(SecurityConstants.DETAILS_USER_ID, scUser.getId());
		additionalInfo.put(SecurityConstants.DETAILS_USERNAME, scUser.getUsername());
		additionalInfo.put(SecurityConstants.DETAILS_DEPT_ID, scUser.getDeptId());
		additionalInfo.put(SecurityConstants.DETAILS_TENANT_ID, scUser.getTenantId());
		additionalInfo.put(SecurityConstants.DETAILS_LICENSE, SecurityConstants.SC_LICENSE);
		additionalInfo.put(SecurityConstants.ACTIVE, Boolean.TRUE);
		additionalInfo.put(SecurityConstants.EPC, scUser.getEnterpriseCode());
		additionalInfo.put(SecurityConstants.USER_REAL_NAME, scUser.getUserRealName());
		additionalInfo.put(SecurityConstants.USER_DEPT, scUser.getUserDept());
		((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
		return accessToken;
	}
}
