// 
// Decompiled by Procyon v0.6.0
// 

package com.nimbusds.jose.proc;

import com.nimbusds.jose.KeySourceException;
import java.util.Iterator;
import com.nimbusds.jose.jwk.JWK;
import javax.crypto.SecretKey;
import java.security.PublicKey;
import com.nimbusds.jose.jwk.KeyConverter;
import java.util.LinkedList;
import com.nimbusds.jose.jwk.JWKSelector;
import java.security.Key;
import java.util.List;
import com.nimbusds.jose.jwk.JWKMatcher;
import com.nimbusds.jose.JWSHeader;
import java.util.Collections;
import java.util.Objects;
import com.nimbusds.jose.jwk.source.JWKSource;
import com.nimbusds.jose.JWSAlgorithm;
import java.util.Set;
import com.nimbusds.jose.shaded.jcip.ThreadSafe;

@ThreadSafe
public class JWSVerificationKeySelector<C extends SecurityContext> extends AbstractJWKSelectorWithSource<C> implements JWSKeySelector<C>
{
    private final Set<JWSAlgorithm> jwsAlgs;
    private final boolean singleJwsAlgConstructorWasCalled;
    
    public JWSVerificationKeySelector(final JWSAlgorithm jwsAlg, final JWKSource<C> jwkSource) {
        super(jwkSource);
        this.jwsAlgs = Collections.singleton((JWSAlgorithm)Objects.requireNonNull((T)jwsAlg));
        this.singleJwsAlgConstructorWasCalled = true;
    }
    
    public JWSVerificationKeySelector(final Set<JWSAlgorithm> jwsAlgs, final JWKSource<C> jwkSource) {
        super(jwkSource);
        if (jwsAlgs.isEmpty()) {
            throw new IllegalArgumentException("The JWS algorithms must not be empty");
        }
        this.jwsAlgs = Collections.unmodifiableSet((Set<? extends JWSAlgorithm>)jwsAlgs);
        this.singleJwsAlgConstructorWasCalled = false;
    }
    
    public boolean isAllowed(final JWSAlgorithm jwsAlg) {
        return this.jwsAlgs.contains(jwsAlg);
    }
    
    @Deprecated
    public JWSAlgorithm getExpectedJWSAlgorithm() {
        if (this.singleJwsAlgConstructorWasCalled) {
            return this.jwsAlgs.iterator().next();
        }
        throw new UnsupportedOperationException("Since this class was constructed with multiple algorithms, the behavior of this method is undefined.");
    }
    
    protected JWKMatcher createJWKMatcher(final JWSHeader jwsHeader) {
        if (!this.isAllowed(jwsHeader.getAlgorithm())) {
            return null;
        }
        return JWKMatcher.forJWSHeader(jwsHeader);
    }
    
    @Override
    public List<Key> selectJWSKeys(final JWSHeader jwsHeader, final C context) throws KeySourceException {
        if (!this.jwsAlgs.contains(jwsHeader.getAlgorithm())) {
            return Collections.emptyList();
        }
        final JWKMatcher jwkMatcher = this.createJWKMatcher(jwsHeader);
        if (jwkMatcher == null) {
            return Collections.emptyList();
        }
        final List<JWK> jwkMatches = this.getJWKSource().get(new JWKSelector(jwkMatcher), context);
        final List<Key> sanitizedKeyList = new LinkedList<Key>();
        for (final Key key : KeyConverter.toJavaKeys(jwkMatches)) {
            if (key instanceof PublicKey || key instanceof SecretKey) {
                sanitizedKeyList.add(key);
            }
        }
        return sanitizedKeyList;
    }
}
