view gwt-client/src/main/java/org/dive4elements/river/client/server/auth/was/Request.java @ 5948:d7b9b3e3c61a

Make instantiation of saml.User easier. Most of the parameters of the constructor can be taken from the Assertion object, so there's no reason to pass them separately. Also, trying to check the validity dates isn't useful for the single sign on case. See comments in the hasExpired method.
author Bernhard Herzog <bh@intevation.de>
date Wed, 08 May 2013 17:56:14 +0200
parents 172338b1407f
children ea9eef426962
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3) 
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details. 
 */

package org.dive4elements.river.client.server.auth.was;

import java.io.UnsupportedEncodingException;
import java.net.URI;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.client.methods.HttpGet;
import org.apache.log4j.Logger;

public class Request extends HttpGet {

    private final static String VERSION = "1.1";
    private final static String REQUEST_SAML_RESPONSE = "GetSAMLResponse";
    private final static String METHOD_AUTH_PASSWORD =
        "urn:opengeospatial:authNMethod:OWS:1.0:password";

    private static Logger logger = Logger.getLogger(Request.class);

    public Request(String uri) {
        String request = uri + "?VERSION=" + VERSION + "&REQUEST=" +
            REQUEST_SAML_RESPONSE + "&METHOD=" + METHOD_AUTH_PASSWORD +
            "&ANONYMOUS=TRUE&CREDENTIALS=";
        this.setURI(URI.create(request));
    }

    public Request(String uri, String user, String pass, String encoding) {
        try {
            String base64user = this.toBase64(user, encoding);
            String base64pass = this.toBase64(pass, encoding);

            String request = uri + "?VERSION=" + VERSION + "&REQUEST=" +
                REQUEST_SAML_RESPONSE + "&METHOD=" + METHOD_AUTH_PASSWORD +
                "&CREDENTIALS=" + base64user + "," + base64pass;

            this.setURI(URI.create(request));
        }
        catch(UnsupportedEncodingException e) {
            logger.error(e);
        }
    }

    private String toBase64(String value, String encoding) throws
        UnsupportedEncodingException {
        if (encoding == null) {
            encoding = "utf-8";
        }
        try {
            return new String(Base64.encodeBase64(value.getBytes(encoding)));
        }
        catch(UnsupportedEncodingException e) {
            logger.warn("Can't encode string with encoding " + encoding +
                    ". Falling back to utf-8. " + e);
            return this.toBase64(value, "utf-8");
        }
    }

}
// vim: set et si fileencoding=utf-8 ts=4 sw=4 tw=80:

http://dive4elements.wald.intevation.org