Search in sources :

Example 1 with Verifier

use of com.disney.http.auth.server.Verifier 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 Verifier

use of com.disney.http.auth.server.Verifier 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)

Example 3 with Verifier

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

the class VerifierFactory method processPolicy.

private PolicyVerifierImpl processPolicy(@SuppressWarnings("rawtypes") final Map policy, Class<Script> scriptClass) throws MalformedURLException, URISyntaxException, InstantiationException, IllegalAccessException, ClassNotFoundException {
    PolicyVerifierImpl verifier = new PolicyVerifierImpl();
    processCommon(verifier, policy, scriptClass);
    verifier.setPolicyLoader(new Callable<Verifier>() {

        String curPolicy = null;

        Callable<Verifier> curLoader = null;

        public Verifier call() throws Exception {
            String loc = resolve(policy, "policy", String.class);
            if (!loc.equals(curPolicy)) {
                // location has changed, update loaded policy
                URI policyURI = null;
                if (loc.startsWith("/")) {
                    URL url = viewResolver.getServletContext().getResource(loc);
                    if (url != null) {
                        policyURI = url.toURI();
                    } else {
                        ViewPolicyLoader vpl = new ViewPolicyLoader();
                        vpl.setLocation(loc);
                        vpl.setViewResolver(viewResolver);
                        curLoader = vpl;
                    }
                } else {
                    policyURI = new URI(loc);
                }
                if (policyURI != null) {
                    Number ttl = (Number) policy.get("ttl");
                    // set right loader based on location: view, file, HTTP url or servlet url ...
                    URIParcel<Verifier> parcel = new URIParcel<Verifier>(Verifier.class, policyURI);
                    if (ttl != null) {
                        parcel.setRefresh(ttl.longValue());
                    }
                    curLoader = parcel;
                }
                curPolicy = loc;
            }
            return curLoader.call();
        }
    });
    return verifier;
}
Also used : URIParcel(com.disney.uriparcel.URIParcel) PolicyVerifierImpl(com.disney.http.auth.server.policy.PolicyVerifierImpl) AbstractVerifier(com.disney.http.auth.server.AbstractVerifier) Verifier(com.disney.http.auth.server.Verifier) URI(java.net.URI) 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) URL(java.net.URL)

Example 4 with Verifier

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

the class PolicyVerifierImpl method doVerifyInternal.

@Override
protected VerifierResult doVerifyInternal(ServerAuthorizationRequest request) throws Exception {
    Verifier vf = getPolicyLoader().call();
    if (vf != null) {
        return vf.verify(request);
    }
    VerifierResult vr = new VerifierResult();
    vr.setMessage("No verifier found in loaded auth policy");
    return vr;
}
Also used : VerifierResult(com.disney.http.auth.server.VerifierResult) Verifier(com.disney.http.auth.server.Verifier) AbstractVerifier(com.disney.http.auth.server.AbstractVerifier)

Aggregations

AbstractVerifier (com.disney.http.auth.server.AbstractVerifier)4 Verifier (com.disney.http.auth.server.Verifier)4 VerifierChain (com.disney.http.auth.server.VerifierChain)2 VerifierResult (com.disney.http.auth.server.VerifierResult)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 CertificateException (java.security.cert.CertificateException)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 ArrayList (java.util.ArrayList)2 ServerAuthorizationRequest (com.disney.http.auth.server.ServerAuthorizationRequest)1 PolicyVerifierImpl (com.disney.http.auth.server.policy.PolicyVerifierImpl)1 URIParcel (com.disney.uriparcel.URIParcel)1 Closure (groovy.lang.Closure)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1