Search in sources :

Example 1 with VerifierChain

use of com.disney.http.auth.server.VerifierChain in project groovity by disney.

the class VerifierFactory method createVerifier.

@SuppressWarnings("rawtypes")
public Verifier createVerifier(List auths, Class<Script> scriptClass) throws InstantiationException, IllegalAccessException, ClassNotFoundException, MalformedURLException, URISyntaxException, NoSuchAlgorithmException, InvalidKeySpecException, CertificateException {
    ArrayList<Verifier> verifiers = new ArrayList<Verifier>(auths.size());
    for (Object auth : auths) {
        if (auth instanceof Map) {
            Map conf = (Map) auth;
            Object policy = conf.get("policy");
            if (policy != null) {
                verifiers.add(processPolicy(conf, scriptClass));
            } else {
                String type = (String) conf.get("type");
                if ("signature".equals(type)) {
                    verifiers.add(processSignature(conf, scriptClass));
                } else if ("basic".equals(type)) {
                    verifiers.add(processBasic(conf, scriptClass));
                } else if ("digest".equals(type)) {
                    verifiers.add(processDigest(conf, scriptClass));
                } else {
                    throw new IllegalArgumentException("Unkown auth type: " + type);
                }
            }
        } else if (auth instanceof CharSequence) {
            verifiers.add((Verifier) fallbackConstruct(auth, scriptClass));
        } else if (auth instanceof Closure) {
            verifiers.add(new Verifier() {

                @Override
                public VerifierResult verify(ServerAuthorizationRequest request) throws Exception {
                    Object result = ((Closure) auth).call(request);
                    if (!(result instanceof VerifierResult)) {
                        result = DefaultTypeTransformation.castToType(result, VerifierResult.class);
                    }
                    return (VerifierResult) result;
                }
            });
        }
    }
    return new VerifierChain(verifiers);
}
Also used : Closure(groovy.lang.Closure) VerifierChain(com.disney.http.auth.server.VerifierChain) ArrayList(java.util.ArrayList) AbstractVerifier(com.disney.http.auth.server.AbstractVerifier) Verifier(com.disney.http.auth.server.Verifier) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) MalformedURLException(java.net.MalformedURLException) CertificateException(java.security.cert.CertificateException) VerifierResult(com.disney.http.auth.server.VerifierResult) Map(java.util.Map) HashMap(java.util.HashMap) ServerAuthorizationRequest(com.disney.http.auth.server.ServerAuthorizationRequest)

Example 2 with VerifierChain

use of com.disney.http.auth.server.VerifierChain in project groovity by disney.

the class XmlPolicyParser method parsePolicy.

public static Verifier parsePolicy(InputSource source, ServletContext servletContext) throws SAXException, ParserConfigurationException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchAlgorithmException, InvalidKeySpecException, URISyntaxException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    factory.setNamespaceAware(false);
    factory.setIgnoringElementContentWhitespace(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setEntityResolver(new EntityResolver() {

        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            // System.out.println("Getting entity for "+systemId);
            if (systemId.endsWith("auth.dtd")) {
                return new InputSource(XmlPolicyParser.class.getResourceAsStream("/auth.dtd"));
            }
            return null;
        }
    });
    Document doc = builder.parse(source);
    List<Verifier> configs = new ArrayList<Verifier>();
    NodeList cnodes = doc.getDocumentElement().getChildNodes();
    for (int i = 0; i < cnodes.getLength(); i++) {
        Node cnode = cnodes.item(i);
        if (cnode instanceof Element) {
            Element cel = (Element) cnode;
            if (cel.getNodeName().equals("basic")) {
                configs.add(processBasic(cel));
            } else if (cel.getNodeName().equals("signature")) {
                configs.add(processSignature(cel, servletContext));
            } else if (cel.getNodeName().equals("digest")) {
                configs.add(processDigest(cel));
            }
        }
    }
    if (configs.size() == 0) {
        return null;
    }
    if (configs.size() == 1) {
        return configs.get(0);
    }
    return new VerifierChain(configs);
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) VerifierChain(com.disney.http.auth.server.VerifierChain) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.w3c.dom.Document) Verifier(com.disney.http.auth.server.Verifier) AbstractVerifier(com.disney.http.auth.server.AbstractVerifier) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Aggregations

AbstractVerifier (com.disney.http.auth.server.AbstractVerifier)2 Verifier (com.disney.http.auth.server.Verifier)2 VerifierChain (com.disney.http.auth.server.VerifierChain)2 ArrayList (java.util.ArrayList)2 ServerAuthorizationRequest (com.disney.http.auth.server.ServerAuthorizationRequest)1 VerifierResult (com.disney.http.auth.server.VerifierResult)1 Closure (groovy.lang.Closure)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 CertificateException (java.security.cert.CertificateException)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1