/* * Copyright (c) 2008-2016 Computer Network Information Center (CNIC), Chinese Academy of Sciences. * * This file is part of Duckling project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package cn.vlabs.umt.oauth.as.request; import java.util.HashMap; import java.util.Map; import java.util.Set; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import cn.vlabs.umt.oauth.common.OAuth; import cn.vlabs.umt.oauth.common.exception.OAuthProblemException; import cn.vlabs.umt.oauth.common.exception.OAuthSystemException; import cn.vlabs.umt.oauth.common.utils.OAuthUtils; import cn.vlabs.umt.oauth.common.validators.OAuthValidator; /** * * * */ public abstract class OAuthRequest { private Logger log = LoggerFactory.getLogger(OAuthRequest.class); protected HttpServletRequest request; protected OAuthValidator<HttpServletRequest> validator; protected Map<String, Class<? extends OAuthValidator<HttpServletRequest>>> validators = new HashMap<String, Class<? extends OAuthValidator<HttpServletRequest>>>(); public OAuthRequest(HttpServletRequest request) throws OAuthSystemException, OAuthProblemException { this.request = request; validate(); } public OAuthRequest() { } protected void validate() throws OAuthSystemException, OAuthProblemException { try { validator = initValidator(); validator.validateMethod(request); validator.validateContentType(request); validator.validateRequiredParameters(request); } catch (OAuthProblemException e) { try { String redirectUri = request.getParameter(OAuth.OAUTH_REDIRECT_URI); if (!OAuthUtils.isEmpty(redirectUri)) { e.setRedirectUri(redirectUri); } } catch (Exception ex) { if (log.isDebugEnabled()) { log.debug("Cannot read redirect_url from the request: {}", new Object[] {ex.getMessage()}); } } throw e; } } protected abstract OAuthValidator<HttpServletRequest> initValidator() throws OAuthProblemException, OAuthSystemException; public String getParam(String name) { return request.getParameter(name); } public String getClientId() { return getParam(OAuth.OAUTH_CLIENT_ID); } public String getRedirectURI() { return getParam(OAuth.OAUTH_REDIRECT_URI); } public String getClientSecret() { return getParam(OAuth.OAUTH_CLIENT_SECRET); } public Set<String> getScopes() { String scopes = getParam(OAuth.OAUTH_SCOPE); return OAuthUtils.decodeScopes(scopes); } }