001/*
002 * (C) Copyright 2014-2018 Nuxeo (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.nuxeo.ecm.platform.oauth2.Constants.CLIENT_ID_PARAM;
022import static org.nuxeo.ecm.platform.oauth2.Constants.REDIRECT_URI_PARAM;
023import static org.nuxeo.ecm.platform.oauth2.Constants.REDIRECT_URL_PARAM;
024
025import java.io.UnsupportedEncodingException;
026import java.net.URLDecoder;
027
028import javax.servlet.http.HttpServletRequest;
029
030import org.apache.commons.lang3.StringUtils;
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    protected String clientId;
039
040    protected String redirectURI;
041
042    public OAuth2Request() {
043    }
044
045    public OAuth2Request(HttpServletRequest request) {
046        clientId = request.getParameter(CLIENT_ID_PARAM);
047        redirectURI = decodeParameter(request, REDIRECT_URI_PARAM);
048        // Fallback for non-RFC compliant client
049        if (StringUtils.isBlank(redirectURI)) {
050            redirectURI = decodeParameter(request, REDIRECT_URL_PARAM);
051        }
052    }
053
054    public static String decodeParameter(HttpServletRequest request, String parameterName) {
055        String value = request.getParameter(parameterName);
056        try {
057            if (StringUtils.isNotBlank(value)) {
058                return URLDecoder.decode(value, "UTF-8");
059            }
060        } catch (UnsupportedEncodingException e) {
061            // Nothing to do.
062        }
063        return value;
064    }
065
066    public String getRedirectURI() {
067        return redirectURI;
068    }
069
070    public String getClientId() {
071        return clientId;
072    }
073}