// Path: core/src/main/java/org/juiser/io/DefaultResource.java
// public class DefaultResource implements Resource {
// 
//     private final InputStream is;
//     private final String name;
// 
//     public DefaultResource(InputStream is, String name) {
//         Assert.notNull(is, "InputStream cannot be null.");
//         Assert.hasText(name, "String name argument cannot be null or empty.");
//         this.is = is;
//         this.name = name;
//     }
// 
//     @Override
//     public InputStream getInputStream() {
//         return this.is;
//     }
// 
//     @Override
//     public String getName() {
//         return this.name;
//     }
// 
//     @Override
//     public String toString() {
//         return "DefaultResource name='" + getName() + "'";
//     }
// }
// 
// Path: core/src/main/java/org/juiser/io/Resource.java
// public interface Resource {
// 
//     InputStream getInputStream();
// 
//     String getName();
// }
// 
// Path: core/src/main/java/org/juiser/io/ResourceLoader.java
// public interface ResourceLoader {
// 
//     Resource getResource(String path) throws IOException;
// }
// 
// Path: core/src/main/java/org/juiser/jwt/AlgorithmFamily.java
// public enum AlgorithmFamily {
// 
//     HMAC,
//     RSA,
//     EC; //Elliptic Curve
// 
//     public static AlgorithmFamily forName(String name) {
// 
//         if (HMAC.name().equalsIgnoreCase(name)) {
//             return HMAC;
//         } else if (RSA.name().equalsIgnoreCase(name)) {
//             return RSA;
//         } else if (EC.name().equalsIgnoreCase(name) ||
//             "ECDSA".equalsIgnoreCase(name) ||
//             "Elliptic Curve".equalsIgnoreCase(name)) {
//             return EC;
//         }
// 
//         //couldn't associate, invalid arg:
//         String msg = "Unrecognized algorithm family name value: " + name;
//         throw new IllegalArgumentException(msg);
//     }
// }


import java.security.PublicKey;
import java.security.interfaces.ECKey;
import java.security.interfaces.RSAKey;
import java.util.function.Function;
import org.juiser.io.DefaultResource;
import org.juiser.io.Resource;
import org.juiser.io.ResourceLoader;
import org.juiser.jwt.AlgorithmFamily;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.impl.TextCodec;
import io.jsonwebtoken.lang.Assert;
import io.jsonwebtoken.lang.Classes;
import io.jsonwebtoken.lang.RuntimeEnvironment;
import io.jsonwebtoken.lang.Strings;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.security.Key;
/*
 * Copyright 2017 Les Hazlewood and the respective Juiser contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.juiser.jwt.config;



/**
 * @since 1.0.0
 */
public class ConfigJwkResolver implements Function<JwkConfig, Key> {

    private final ResourceLoader resourceLoader;

    public ConfigJwkResolver(ResourceLoader resourceLoader) {
        Assert.notNull(resourceLoader);
        this.resourceLoader = resourceLoader;
    }

    @Override
    public Key apply(JwkConfig jwk) {

        Key key = null;

        AlgorithmFamily algorithmFamily = null;
        String algFamilyName = jwk.getAlgFamily();
        if (Strings.hasText(algFamilyName)) {
            try {
                algorithmFamily = AlgorithmFamily.forName(algFamilyName);
            } catch (IllegalArgumentException e) {
                String msg = "Unsupported juiser.header.jwt.jwk.algFamily value: " + algFamilyName + ".  " +
                    "Please use only " + AlgorithmFamily.class.getName() + " enum names: " +
                    Strings.arrayToCommaDelimitedString(AlgorithmFamily.values());
                throw new IllegalArgumentException(msg, e);
            }
        }

        byte[] bytes = null;

        Resource keyResource = getResource(jwk);

        String keyString = jwk.getValue();

        boolean keyStringSpecified = Strings.hasText(keyString);

        if (keyResource != null && keyStringSpecified) {
            String msg = "Both the juiser.header.jwt.key.value and " +
// Path: core/src/main/java/org/juiser/io/DefaultResource.java
// public class DefaultResource implements Resource {
// 
//     private final InputStream is;
//     private final String name;
// 
//     public DefaultResource(InputStream is, String name) {
//         Assert.notNull(is, "InputStream cannot be null.");
//         Assert.hasText(name, "String name argument cannot be null or empty.");
//         this.is = is;
//         this.name = name;
//     }
// 
//     @Override
//     public InputStream getInputStream() {
//         return this.is;
//     }
// 
//     @Override
//     public String getName() {
//         return this.name;
//     }
// 
//     @Override
//     public String toString() {
//         return "DefaultResource name='" + getName() + "'";
//     }
// }
// 
// Path: core/src/main/java/org/juiser/io/Resource.java
// public interface Resource {
// 
//     InputStream getInputStream();
// 
//     String getName();
// }
// 
// Path: core/src/main/java/org/juiser/io/ResourceLoader.java
// public interface ResourceLoader {
// 
//     Resource getResource(String path) throws IOException;
// }
// 
// Path: core/src/main/java/org/juiser/jwt/AlgorithmFamily.java
// public enum AlgorithmFamily {
// 
//     HMAC,
//     RSA,
//     EC; //Elliptic Curve
// 
//     public static AlgorithmFamily forName(String name) {
// 
//         if (HMAC.name().equalsIgnoreCase(name)) {
//             return HMAC;
//         } else if (RSA.name().equalsIgnoreCase(name)) {
//             return RSA;
//         } else if (EC.name().equalsIgnoreCase(name) ||
//             "ECDSA".equalsIgnoreCase(name) ||
//             "Elliptic Curve".equalsIgnoreCase(name)) {
//             return EC;
//         }
// 
//         //couldn't associate, invalid arg:
//         String msg = "Unrecognized algorithm family name value: " + name;
//         throw new IllegalArgumentException(msg);
//     }
// }


// Path: core/src/main/java/org/juiser/jwt/config/ConfigJwkResolver.java
import java.security.PublicKey;
import java.security.interfaces.ECKey;
import java.security.interfaces.RSAKey;
import java.util.function.Function;
import org.juiser.io.DefaultResource;
import org.juiser.io.Resource;
import org.juiser.io.ResourceLoader;
import org.juiser.jwt.AlgorithmFamily;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.impl.TextCodec;
import io.jsonwebtoken.lang.Assert;
import io.jsonwebtoken.lang.Classes;
import io.jsonwebtoken.lang.RuntimeEnvironment;
import io.jsonwebtoken.lang.Strings;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.security.Key;

/*
 * Copyright 2017 Les Hazlewood and the respective Juiser contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.juiser.jwt.config;



/**
 * @since 1.0.0
 */
public class ConfigJwkResolver implements Function<JwkConfig, Key> {

    private final ResourceLoader resourceLoader;

    public ConfigJwkResolver(ResourceLoader resourceLoader) {
        Assert.notNull(resourceLoader);
        this.resourceLoader = resourceLoader;
    }

    @Override
    public Key apply(JwkConfig jwk) {

        Key key = null;

        AlgorithmFamily algorithmFamily = null;
        String algFamilyName = jwk.getAlgFamily();
        if (Strings.hasText(algFamilyName)) {
            try {
                algorithmFamily = AlgorithmFamily.forName(algFamilyName);
            } catch (IllegalArgumentException e) {
                String msg = "Unsupported juiser.header.jwt.jwk.algFamily value: " + algFamilyName + ".  " +
                    "Please use only " + AlgorithmFamily.class.getName() + " enum names: " +
                    Strings.arrayToCommaDelimitedString(AlgorithmFamily.values());
                throw new IllegalArgumentException(msg, e);
            }
        }

        byte[] bytes = null;

        Resource keyResource = getResource(jwk);

        String keyString = jwk.getValue();

        boolean keyStringSpecified = Strings.hasText(keyString);

        if (keyResource != null && keyStringSpecified) {
            String msg = "Both the juiser.header.jwt.key.value and " +
                "juiser.header.jwt.key.resource properties may not be set simultaneously.  " +