// 
// 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.PrivateKey;
import com.nimbusds.jose.jwk.KeyConverter;
import java.util.LinkedList;
import com.nimbusds.jose.jwk.JWKSelector;
import java.util.Collections;
import java.security.Key;
import java.util.List;
import com.nimbusds.jose.jwk.JWKMatcher;
import com.nimbusds.jose.JWEHeader;
import java.util.Objects;
import com.nimbusds.jose.jwk.source.JWKSource;
import com.nimbusds.jose.EncryptionMethod;
import com.nimbusds.jose.JWEAlgorithm;
import com.nimbusds.jose.shaded.jcip.ThreadSafe;

@ThreadSafe
public class JWEDecryptionKeySelector<C extends SecurityContext> extends AbstractJWKSelectorWithSource<C> implements JWEKeySelector<C>
{
    private final JWEAlgorithm jweAlg;
    private final EncryptionMethod jweEnc;
    
    public JWEDecryptionKeySelector(final JWEAlgorithm jweAlg, final EncryptionMethod jweEnc, final JWKSource<C> jwkSource) {
        super(jwkSource);
        this.jweAlg = Objects.requireNonNull(jweAlg);
        this.jweEnc = Objects.requireNonNull(jweEnc);
    }
    
    public JWEAlgorithm getExpectedJWEAlgorithm() {
        return this.jweAlg;
    }
    
    public EncryptionMethod getExpectedJWEEncryptionMethod() {
        return this.jweEnc;
    }
    
    protected JWKMatcher createJWKMatcher(final JWEHeader jweHeader) {
        if (!this.getExpectedJWEAlgorithm().equals(jweHeader.getAlgorithm())) {
            return null;
        }
        if (!this.getExpectedJWEEncryptionMethod().equals(jweHeader.getEncryptionMethod())) {
            return null;
        }
        return JWKMatcher.forJWEHeader(jweHeader);
    }
    
    @Override
    public List<Key> selectJWEKeys(final JWEHeader jweHeader, final C context) throws KeySourceException {
        if (!this.jweAlg.equals(jweHeader.getAlgorithm()) || !this.jweEnc.equals(jweHeader.getEncryptionMethod())) {
            return Collections.emptyList();
        }
        final JWKMatcher jwkMatcher = this.createJWKMatcher(jweHeader);
        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 PrivateKey || key instanceof SecretKey) {
                sanitizedKeyList.add(key);
            }
        }
        return sanitizedKeyList;
    }
}
