/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.security.api.filter;

import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.security.api.model.YamiUser;
import com.yami.shop.security.api.service.AppLoginService;
import com.yami.shop.security.comment.exception.YamiAuthFilterException;
import com.yami.shop.security.comment.filter.YamiAuthenticationProcessingFilter;
import com.yami.shop.security.comment.token.AuthenticationToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * 自定义登陆filter
 * 统一第三方登陆
 * @author LGH
 */
@Component
public class AppLoginAuthenticationFilter extends YamiAuthenticationProcessingFilter {

    private final List<AppLoginService> appLoginServices;

    @Autowired
    public AppLoginAuthenticationFilter(List<AppLoginService> appLoginServices) {
        super("/appLogin");
        this.appLoginServices = appLoginServices;
    }

    @Override
    protected UserDetails getUserDetails(AuthenticationToken authentication) {
        for (AppLoginService appLoginService : appLoginServices) {
            // 判断登陆方式
            if (Objects.equals(appLoginService.getApp().value(),authentication.getAppType())) {
                return appLoginService.loginAndGetUserDetails(authentication);
            }
        }
        throw new GlobalException("无法获取登陆方式");
    }


}
