package com.yida.data.auth.controller.in;

import com.yida.data.auth.service.impl.RedisClientDetailsService;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.approval.Approval;
import org.springframework.security.oauth2.provider.approval.Approval.ApprovalStatus;
import org.springframework.security.oauth2.provider.approval.TokenApprovalStore;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/in/security")
@RestController
@RequiredArgsConstructor
public class InSecurityController {

    private final TokenApprovalStore tokenApprovalStore;
    private final RedisClientDetailsService redisClientDetailsService;

    @ApiOperation("撤销认证")
    @GetMapping("/revokeApproval")
    public void revokeApproval(String userName, String clientId) {
        ClientDetails client = redisClientDetailsService.loadClientByClientId(clientId);
        List<Approval> approvals = new ArrayList<Approval>();
        for (String scope : client.getScope()) {
            approvals.add(new Approval(userName, clientId, scope, new Date(), ApprovalStatus.APPROVED));
        }
        tokenApprovalStore.revokeApprovals(approvals);
    }
}
