001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Arnaud Kervern
018 */
019package org.nuxeo.ecm.platform.oauth2.request;
020
021import static org.apache.commons.lang.StringUtils.isBlank;
022import static org.apache.commons.lang.StringUtils.isNotBlank;
023
024import java.io.UnsupportedEncodingException;
025import java.net.URLDecoder;
026
027import javax.servlet.http.HttpServletRequest;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031
032/**
033 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a>
034 * @since 5.9.2
035 */
036public abstract class Oauth2Request {
037
038    private static final Log log = LogFactory.getLog(Oauth2Request.class);
039
040    public static final String CLIENT_ID = "client_id";
041
042    public static final String REDIRECT_URI = "redirect_uri";
043
044    public static final String REDIRECT_URL = "redirect_url";
045
046    protected String clientId;
047
048    protected String redirectUri;
049
050    public Oauth2Request() {
051    }
052
053    public Oauth2Request(HttpServletRequest request) {
054        clientId = request.getParameter(CLIENT_ID);
055        redirectUri = decodeParameter(request, REDIRECT_URI);
056        // Fallback for non-RFC compliant client
057        if (isBlank(redirectUri)) {
058            redirectUri = decodeParameter(request, REDIRECT_URL);
059        }
060    }
061
062    public static String decodeParameter(HttpServletRequest request, String parameterName) {
063        String value = request.getParameter(parameterName);
064        try {
065            if (isNotBlank(value)) {
066                return URLDecoder.decode(value, "UTF-8");
067            }
068        } catch (UnsupportedEncodingException e) {
069            // Nothing to do.
070        }
071        return value;
072    }
073
074    public String getRedirectUri() {
075        return redirectUri;
076    }
077
078    public String getClientId() {
079        return clientId;
080    }
081}