Return-Path: Delivered-To: apmail-incubator-amber-commits-archive@minotaur.apache.org Received: (qmail 88760 invoked from network); 18 Dec 2010 16:39:10 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 18 Dec 2010 16:39:10 -0000 Received: (qmail 89119 invoked by uid 500); 18 Dec 2010 16:39:10 -0000 Delivered-To: apmail-incubator-amber-commits-archive@incubator.apache.org Received: (qmail 89101 invoked by uid 500); 18 Dec 2010 16:39:10 -0000 Mailing-List: contact amber-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: amber-dev@incubator.apache.org Delivered-To: mailing list amber-commits@incubator.apache.org Received: (qmail 89094 invoked by uid 99); 18 Dec 2010 16:39:10 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 18 Dec 2010 16:39:10 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 18 Dec 2010 16:39:01 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 3D2B323889E1; Sat, 18 Dec 2010 16:38:38 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: svn commit: r1050663 [1/2] - in /incubator/amber/trunk/oauth-2.0/oauth2-common: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/amber/ src/main/java/org/apache/amber/oauth2/ src/main/java/org/apach... Date: Sat, 18 Dec 2010 16:38:37 -0000 To: amber-commits@incubator.apache.org From: tommaso@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20101218163838.3D2B323889E1@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: tommaso Date: Sat Dec 18 16:38:36 2010 New Revision: 1050663 URL: http://svn.apache.org/viewvc?rev=1050663&view=rev Log: [AMBER-11] - import of Leelo OAuth 2.0 implementation - oauth2-common module Added: incubator/amber/trunk/oauth-2.0/oauth2-common/ (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/pom.xml (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/OAuth.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/error/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/error/OAuthError.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/exception/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/exception/OAuthProblemException.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/exception/OAuthSystemException.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/OAuthMessage.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/OAuthResponse.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/types/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/types/GrantType.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/types/ParameterStyle.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/types/ResponseType.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/BodyURLEncodedParametersApplier.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/JSONBodyParametersApplier.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/OAuthParametersApplier.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/QueryParameterApplier.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/WWWAuthHeaderParametersApplier.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/utils/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/utils/JSONUtils.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/utils/OAuthUtils.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/validators/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/validators/AbstractValidator.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/validators/OAuthValidator.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/org/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/org/apache/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/org/apache/amber/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/org/apache/amber/oauth2/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/org/apache/amber/oauth2/common/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/org/apache/amber/oauth2/common/OAuthUtilsTest.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/org/apache/amber/oauth2/common/message/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/org/apache/amber/oauth2/common/message/OAuthResponseTest.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/org/apache/amber/oauth2/common/parameters/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/org/apache/amber/oauth2/common/parameters/BodyURLEncodedParametersApplierTest.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/org/apache/amber/oauth2/common/parameters/JSONBodyParametersApplierTest.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/org/apache/amber/oauth2/common/parameters/QueryParameterApplierTest.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/org/apache/amber/oauth2/common/parameters/WWWAuthHeaderParametersApplierTest.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/org/apache/amber/oauth2/common/utils/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/org/apache/amber/oauth2/common/utils/DummyOAuthMessage.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/org/apache/amber/oauth2/common/utils/JSONUtilsTest.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/org/apache/amber/oauth2/common/utils/OAuthUtilsTest.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/org/apache/amber/oauth2/common/validators/ incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/org/apache/amber/oauth2/common/validators/AbstractValidatorImpl.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-common/src/test/java/org/apache/amber/oauth2/common/validators/AbstractValidatorTest.java (with props) Propchange: incubator/amber/trunk/oauth-2.0/oauth2-common/ ------------------------------------------------------------------------------ --- svn:ignore (added) +++ svn:ignore Sat Dec 18 16:38:36 2010 @@ -0,0 +1 @@ +target Added: incubator/amber/trunk/oauth-2.0/oauth2-common/pom.xml URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-common/pom.xml?rev=1050663&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-common/pom.xml (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-common/pom.xml Sat Dec 18 16:38:36 2010 @@ -0,0 +1,82 @@ + + + + + + amber-oauth2-parent + org.apache.amber + 0.2-SNAPSHOT + + + 4.0.0 + oauth2-common + Apache Amber: OAuth 2.0 Implementation - Common + 0.2-SNAPSHOT + jar + OAuth 2.0 library - Common + + + oauth2-common + + + org.apache.maven.plugins + maven-surefire-plugin + 2.5 + + + **/TestOAuthClient.java + **/TestOAuthClient.java + + + + + org.codehaus.mojo + cobertura-maven-plugin + + + + org/apache/amber/oauth2/common/message/types/**.class + org/apache/amber/oauth2/common/OAuth**.class + + + + + + + + + + org.codehaus.jettison + jettison + ${jettison.version} + + + stax + stax-api + + + + + \ No newline at end of file Propchange: incubator/amber/trunk/oauth-2.0/oauth2-common/pom.xml ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/OAuth.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/OAuth.java?rev=1050663&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/OAuth.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/OAuth.java Sat Dec 18 16:38:36 2010 @@ -0,0 +1,81 @@ +/** + * Copyright 2010 Newcastle University + * + * http://research.ncl.ac.uk/smart/ + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.amber.oauth2.common; + +import org.apache.amber.oauth2.common.message.types.ParameterStyle; + +/** + * @author Maciej Machulak (m.p.machulak@ncl.ac.uk) + * @author Lukasz Moren (lukasz.moren@ncl.ac.uk) + * @author Aad van Moorsel (aad.vanmoorsel@ncl.ac.uk) + */ +public final class OAuth { + + public static final class HttpMethod { + public static final String POST = "POST"; + public static final String GET = "GET"; + public static final String DELETE = "DELETE"; + public static final String PUT = "PUT"; + } + + public static final class HeaderType { + public static final String CONTENT_TYPE = "Content-Type"; + public static final String WWW_AUTHENTICATE = "WWW-Authenticate"; + public static final String AUTHORIZATION = "Authorization"; + } + + public static final class WWWAuthHeader { + public static final String REALM = "realm"; + } + + public static final class ContentType { + public static final String URL_ENCODED = "application/x-www-form-urlencoded"; + public static final String JSON = "application/json"; + } + + public static final String OAUTH_RESPONSE_TYPE = "response_type"; + public static final String OAUTH_CLIENT_ID = "client_id"; + public static final String OAUTH_CLIENT_SECRET = "client_secret"; + public static final String OAUTH_REDIRECT_URI = "redirect_uri"; + public static final String OAUTH_USERNAME = "username"; + public static final String OAUTH_PASSWORD = "password"; + public static final String OAUTH_ASSERTION_TYPE = "assertion_type"; + public static final String OAUTH_ASSERTION = "assertion"; + public static final String OAUTH_SCOPE = "scope"; + public static final String OAUTH_STATE = "state"; + public static final String OAUTH_GRANT_TYPE = "grant_type"; + + public static final String OAUTH_HEADER_NAME = "OAuth"; + + //Authorization response params + public static final String OAUTH_CODE = "code"; + public static final String OAUTH_ACCESS_TOKEN = "access_token"; + public static final String OAUTH_EXPIRES_IN = "expires_in"; + public static final String OAUTH_REFRESH_TOKEN = "refresh_token"; + + public static final String OAUTH_TOKEN = "oauth_token"; + + public static final String OAUTH_TOKEN_DRAFT_0 = "access_token"; + + public static final ParameterStyle DEFAULT_PARAMETER_STYLE = ParameterStyle.HEADER; + public static final String OAUTH_VERSION_DIFFER = "oauth_signature_method"; +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/OAuth.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/error/OAuthError.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/error/OAuthError.java?rev=1050663&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/error/OAuthError.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/error/OAuthError.java Sat Dec 18 16:38:36 2010 @@ -0,0 +1,62 @@ +/** + * Copyright 2010 Newcastle University + * + * http://research.ncl.ac.uk/smart/ + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.amber.oauth2.common.error; + +/** + * @author Maciej Machulak (m.p.machulak@ncl.ac.uk) + * @author Lukasz Moren (lukasz.moren@ncl.ac.uk) + * @author Aad van Moorsel (aad.vanmoorsel@ncl.ac.uk) + */ +public abstract class OAuthError { + + //error response params + public static final String OAUTH_ERROR = "error"; + public static final String OAUTH_ERROR_DESCRIPTION = "error_description"; + public static final String OAUTH_ERROR_URI = "error_uri"; + + public static final class CodeResponse { + public static final String INVALID_REQUEST = "invalid_request"; + public static final String INVALID_CLIENT = "invalid_client"; + public static final String UNAUTHORIZED_CLIENT = "unauthorized_client"; + public static final String REDIRECT_URI_MISMATCH = "redirect_uri_mismatch"; + public static final String ACCESS_DENIED = "access_denied"; + public static final String UNSUPPORTED_RESPONSE_TYPE = "unsupported_response_type"; + public static final String INVALID_SCOPE = "invalid_scope"; + } + + public static final class TokenResponse { + public static final String INVALID_REQUEST = "invalid_request"; + public static final String INVALID_CLIENT = "invalid_client"; + public static final String UNAUTHORIZED_CLIENT = "unauthorized_client"; + public static final String INVALID_GRANT = "invalid_grant"; + public static final String UNSUPPORTED_GRANT_TYPE = "unsupported_grant_type"; + public static final String INVALID_SCOPE = "invalid_scope"; + } + + public static final class ResourceResponse { + public static final String INVALID_REQUEST = "invalid_request"; + public static final String EXPIRED_TOKEN = "expired_token"; + public static final String INSUFFICIENT_SCOPE = "insufficient_scope"; + public static final String INVALID_TOKEN = "invalid_token"; + } + +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/error/OAuthError.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/exception/OAuthProblemException.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/exception/OAuthProblemException.java?rev=1050663&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/exception/OAuthProblemException.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/exception/OAuthProblemException.java Sat Dec 18 16:38:36 2010 @@ -0,0 +1,163 @@ +/** + * Copyright 2010 Newcastle University + * + * http://research.ncl.ac.uk/smart/ + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.amber.oauth2.common.exception; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.amber.oauth2.common.utils.OAuthUtils; + +/** + * @author Maciej Machulak (m.p.machulak@ncl.ac.uk) + * @author Lukasz Moren (lukasz.moren@ncl.ac.uk) + * @author Aad van Moorsel (aad.vanmoorsel@ncl.ac.uk) + */ +public final class OAuthProblemException extends Exception { + + private String error; + private String description; + private String uri; + private String state; + private String scope; + private String redirectUri; + + private Map parameters = new HashMap(); + + private OAuthProblemException(String error) { + this(error, ""); + } + + private OAuthProblemException(String error, String description) { + super(error + " " + description); + this.description = description; + this.error = error; + } + + + public static OAuthProblemException error(String error) { + return new OAuthProblemException(error); + } + + public static OAuthProblemException error(String error, String description) { + return new OAuthProblemException(error, description); + } + + public OAuthProblemException description(String description) { + this.description = description; + return this; + } + + public OAuthProblemException uri(String uri) { + this.uri = uri; + return this; + } + + public OAuthProblemException state(String state) { + this.state = state; + return this; + } + + public OAuthProblemException scope(String scope) { + this.scope = scope; + return this; + } + + public OAuthProblemException setParameter(String name, String value) { + parameters.put(name, value); + return this; + } + + public String getError() { + return error; + } + + public String getDescription() { + return description; + } + + public String getUri() { + return uri; + } + + public String getState() { + return state; + } + + public String getScope() { + return scope; + } + + public String get(String name) { + return parameters.get(name); + } + + public Map getParameters() { + return parameters; + } + + public String getRedirectUri() { + return redirectUri; + } + + public void setRedirectUri(String redirectUri) { + this.redirectUri = redirectUri; + } + + @Override + public String getMessage() { + StringBuffer b = new StringBuffer(); + if (!OAuthUtils.isEmpty(error)) { + b.append(error); + } + + if (!OAuthUtils.isEmpty(description)) { + b.append(", ").append(description); + } + + + if (!OAuthUtils.isEmpty(uri)) { + b.append(", ").append(uri); + } + + + if (!OAuthUtils.isEmpty(state)) { + b.append(", ").append(state); + } + + if (!OAuthUtils.isEmpty(scope)) { + b.append(", ").append(scope); + } + + return b.toString(); + } + + @Override + public String toString() { + return "OAuthProblemException{" + + "description='" + description + '\'' + + ", error='" + error + '\'' + + ", uri='" + uri + '\'' + + ", state='" + state + '\'' + + ", scope='" + scope + '\'' + + '}'; + } +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/exception/OAuthProblemException.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/exception/OAuthSystemException.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/exception/OAuthSystemException.java?rev=1050663&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/exception/OAuthSystemException.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/exception/OAuthSystemException.java Sat Dec 18 16:38:36 2010 @@ -0,0 +1,45 @@ +/** + * Copyright 2010 Newcastle University + * + * http://research.ncl.ac.uk/smart/ + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.amber.oauth2.common.exception; + +/** + * @author Maciej Machulak + * @author Lukasz Moren + */ +public class OAuthSystemException extends Exception { + + public OAuthSystemException() { + super(); //To change body of overridden methods use File | Settings | File Templates. + } + + public OAuthSystemException(String s) { + super(s); //To change body of overridden methods use File | Settings | File Templates. + } + + public OAuthSystemException(Throwable throwable) { + super(throwable); //To change body of overridden methods use File | Settings | File Templates. + } + + public OAuthSystemException(String s, Throwable throwable) { + super(s, throwable); //To change body of overridden methods use File | Settings | File Templates. + } +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/exception/OAuthSystemException.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/OAuthMessage.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/OAuthMessage.java?rev=1050663&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/OAuthMessage.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/OAuthMessage.java Sat Dec 18 16:38:36 2010 @@ -0,0 +1,49 @@ +/** + * Copyright 2010 Newcastle University + * + * http://research.ncl.ac.uk/smart/ + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.amber.oauth2.common.message; + +import java.util.Map; + +/** + * @author Maciej Machulak (m.p.machulak@ncl.ac.uk) + * @author Lukasz Moren (lukasz.moren@ncl.ac.uk) + * @author Aad van Moorsel (aad.vanmoorsel@ncl.ac.uk) + */ +public interface OAuthMessage { + + String getLocationUri(); + + void setLocationUri(String uri); + + String getBody(); + + void setBody(String body); + + String getHeader(String name); + + void addHeader(String name, String header); + + Map getHeaders(); + + void setHeaders(Map headers); + +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/OAuthMessage.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/OAuthResponse.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/OAuthResponse.java?rev=1050663&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/OAuthResponse.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/OAuthResponse.java Sat Dec 18 16:38:36 2010 @@ -0,0 +1,203 @@ +/** + * Copyright 2010 Newcastle University + * + * http://research.ncl.ac.uk/smart/ + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.amber.oauth2.common.message; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.amber.oauth2.common.OAuth; +import org.apache.amber.oauth2.common.error.OAuthError; +import org.apache.amber.oauth2.common.exception.OAuthProblemException; +import org.apache.amber.oauth2.common.exception.OAuthSystemException; +import org.apache.amber.oauth2.common.parameters.BodyURLEncodedParametersApplier; +import org.apache.amber.oauth2.common.parameters.JSONBodyParametersApplier; +import org.apache.amber.oauth2.common.parameters.OAuthParametersApplier; +import org.apache.amber.oauth2.common.parameters.QueryParameterApplier; +import org.apache.amber.oauth2.common.parameters.WWWAuthHeaderParametersApplier; + +/** + * @author Maciej Machulak (m.p.machulak@ncl.ac.uk) + * @author Lukasz Moren (lukasz.moren@ncl.ac.uk) + * @author Aad van Moorsel (aad.vanmoorsel@ncl.ac.uk) + */ +public class OAuthResponse implements OAuthMessage { + + protected int responseStatus; + protected String uri; + protected String body; + + protected Map headers = new HashMap(); + + protected OAuthResponse(String uri, int responseStatus) { + this.uri = uri; + this.responseStatus = responseStatus; + } + + public static OAuthResponseBuilder status(int code) { + return new OAuthResponseBuilder(code); + } + + public static OAuthErrorResponseBuilder errorResponse(int code) { + return new OAuthErrorResponseBuilder(code); + } + + @Override + public String getLocationUri() { + return uri; + } + + @Override + public void setLocationUri(String uri) { + this.uri = uri; + } + + @Override + public String getBody() { + return body; + } + + @Override + public void setBody(String body) { + this.body = body; + } + + @Override + public String getHeader(String name) { + return headers.get(name); + } + + @Override + public Map getHeaders() { + return headers; + } + + @Override + public void setHeaders(Map headers) { + this.headers = headers; + } + + public int getResponseStatus() { + return responseStatus; + } + + @Override + public void addHeader(String name, String header) { + headers.put(name, header); + } + + public static class OAuthResponseBuilder { + + protected OAuthParametersApplier applier; + protected Map parameters = new HashMap(); + protected int responseCode; + protected String location; + + public OAuthResponseBuilder(int responseCode) { + this.responseCode = responseCode; + } + + public OAuthResponseBuilder location(String location) { + this.location = location; + return this; + } + + public OAuthResponseBuilder setScope(String value) { + this.parameters.put(OAuth.OAUTH_SCOPE, value); + return this; + } + + public OAuthResponseBuilder setParam(String key, String value) { + this.parameters.put(key, value); + return this; + } + + public OAuthResponse buildQueryMessage() throws OAuthSystemException { + OAuthResponse msg = new OAuthResponse(location, responseCode); + this.applier = new QueryParameterApplier(); + return (OAuthResponse)applier.applyOAuthParameters(msg, parameters); + } + + public OAuthResponse buildBodyMessage() throws OAuthSystemException { + OAuthResponse msg = new OAuthResponse(location, responseCode); + this.applier = new BodyURLEncodedParametersApplier(); + return (OAuthResponse)applier.applyOAuthParameters(msg, parameters); + } + + public OAuthResponse buildJSONMessage() throws OAuthSystemException { + OAuthResponse msg = new OAuthResponse(location, responseCode); + this.applier = new JSONBodyParametersApplier(); + return (OAuthResponse)applier.applyOAuthParameters(msg, parameters); + } + + public OAuthResponse buildHeaderMessage() throws OAuthSystemException { + OAuthResponse msg = new OAuthResponse(location, responseCode); + this.applier = new WWWAuthHeaderParametersApplier(); + return (OAuthResponse)applier.applyOAuthParameters(msg, parameters); + } + } + + public static class OAuthErrorResponseBuilder extends OAuthResponseBuilder { + + public OAuthErrorResponseBuilder(int responseCode) { + super(responseCode); + } + + public OAuthErrorResponseBuilder error(OAuthProblemException ex) { + this.parameters.put(OAuthError.OAUTH_ERROR, ex.getError()); + this.parameters.put(OAuthError.OAUTH_ERROR_DESCRIPTION, ex.getDescription()); + this.parameters.put(OAuthError.OAUTH_ERROR_URI, ex.getUri()); + this.parameters.put(OAuth.OAUTH_STATE, ex.getState()); + return this; + } + + public OAuthErrorResponseBuilder setError(String error) { + this.parameters.put(OAuthError.OAUTH_ERROR, error); + return this; + } + + public OAuthErrorResponseBuilder setErrorDescription(String desc) { + this.parameters.put(OAuthError.OAUTH_ERROR_DESCRIPTION, desc); + return this; + } + + public OAuthErrorResponseBuilder setErrorUri(String state) { + this.parameters.put(OAuthError.OAUTH_ERROR_URI, state); + return this; + } + + public OAuthErrorResponseBuilder setState(String state) { + this.parameters.put(OAuth.OAUTH_STATE, state); + return this; + } + + public OAuthErrorResponseBuilder setRealm(String realm) { + this.parameters.put(OAuth.WWWAuthHeader.REALM, realm); + return this; + } + + public OAuthErrorResponseBuilder location(String location) { + this.location = location; + return this; + } + } + +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/OAuthResponse.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/types/GrantType.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/types/GrantType.java?rev=1050663&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/types/GrantType.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/types/GrantType.java Sat Dec 18 16:38:36 2010 @@ -0,0 +1,46 @@ +/** + * Copyright 2010 Newcastle University + * + * http://research.ncl.ac.uk/smart/ + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.amber.oauth2.common.message.types; + +/** + * @author Maciej Machulak (m.p.machulak@ncl.ac.uk) + * @author Lukasz Moren (lukasz.moren@ncl.ac.uk) + * @author Aad van Moorsel (aad.vanmoorsel@ncl.ac.uk) + */ +public enum GrantType { + AUTHORIZATION_CODE("authorization_code"), + PASSWORD("password"), + ASSERTION("assertion"), + REFRESH_TOKEN("refresh_token"), + NONE("none"); + + private String grantType; + + GrantType(String grantType) { + this.grantType = grantType; + } + + @Override + public String toString() { + return grantType; + } +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/types/GrantType.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/types/ParameterStyle.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/types/ParameterStyle.java?rev=1050663&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/types/ParameterStyle.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/types/ParameterStyle.java Sat Dec 18 16:38:36 2010 @@ -0,0 +1,44 @@ +/** + * Copyright 2010 Newcastle University + * + * http://research.ncl.ac.uk/smart/ + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.amber.oauth2.common.message.types; + +/** + * @author Maciej Machulak (m.p.machulak@ncl.ac.uk) + * @author Lukasz Moren (lukasz.moren@ncl.ac.uk) + * @author Aad van Moorsel (aad.vanmoorsel@ncl.ac.uk) + */ +public enum ParameterStyle { + BODY("body"), + QUERY("query"), + HEADER("header"); + + private String parameterStyle; + + ParameterStyle(String parameterStyle) { + this.parameterStyle = parameterStyle; + } + + @Override + public String toString() { + return parameterStyle; + } +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/types/ParameterStyle.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/types/ResponseType.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/types/ResponseType.java?rev=1050663&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/types/ResponseType.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/types/ResponseType.java Sat Dec 18 16:38:36 2010 @@ -0,0 +1,45 @@ +/** + * Copyright 2010 Newcastle University + * + * http://research.ncl.ac.uk/smart/ + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.amber.oauth2.common.message.types; + +/** + * @author Maciej Machulak (m.p.machulak@ncl.ac.uk) + * @author Lukasz Moren (lukasz.moren@ncl.ac.uk) + * @author Aad van Moorsel (aad.vanmoorsel@ncl.ac.uk) + */ +public enum ResponseType { + + CODE("code"), + TOKEN("token"), + CODE_AND_TOKEN("code_and_token"); + + private String code; + + ResponseType(String code) { + this.code = code; + } + + @Override + public String toString() { + return code; + } +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/message/types/ResponseType.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/BodyURLEncodedParametersApplier.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/BodyURLEncodedParametersApplier.java?rev=1050663&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/BodyURLEncodedParametersApplier.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/BodyURLEncodedParametersApplier.java Sat Dec 18 16:38:36 2010 @@ -0,0 +1,45 @@ +/** + * Copyright 2010 Newcastle University + * + * http://research.ncl.ac.uk/smart/ + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.amber.oauth2.common.parameters; + +import java.util.Map; + +import org.apache.amber.oauth2.common.exception.OAuthSystemException; +import org.apache.amber.oauth2.common.message.OAuthMessage; +import org.apache.amber.oauth2.common.utils.OAuthUtils; + +/** + * @author Maciej Machulak (m.p.machulak@ncl.ac.uk) + * @author Lukasz Moren (lukasz.moren@ncl.ac.uk) + * @author Aad van Moorsel (aad.vanmoorsel@ncl.ac.uk) + */ +public class BodyURLEncodedParametersApplier implements OAuthParametersApplier { + + public OAuthMessage applyOAuthParameters(OAuthMessage message, Map params) + throws OAuthSystemException { + + String body = OAuthUtils.format(params.entrySet(), "UTF-8"); + message.setBody(body); + return message; + + } +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/BodyURLEncodedParametersApplier.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/JSONBodyParametersApplier.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/JSONBodyParametersApplier.java?rev=1050663&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/JSONBodyParametersApplier.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/JSONBodyParametersApplier.java Sat Dec 18 16:38:36 2010 @@ -0,0 +1,49 @@ +/** + * Copyright 2010 Newcastle University + * + * http://research.ncl.ac.uk/smart/ + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.amber.oauth2.common.parameters; + +import java.util.Map; + +import org.apache.amber.oauth2.common.message.OAuthMessage; +import org.apache.amber.oauth2.common.utils.JSONUtils; +import org.codehaus.jettison.json.JSONException; + +import org.apache.amber.oauth2.common.exception.OAuthSystemException; + +/** + * @author Maciej Machulak (m.p.machulak@ncl.ac.uk) + * @author Lukasz Moren (lukasz.moren@ncl.ac.uk) + * @author Aad van Moorsel (aad.vanmoorsel@ncl.ac.uk) + */ +public class JSONBodyParametersApplier implements OAuthParametersApplier { + public OAuthMessage applyOAuthParameters(OAuthMessage message, Map params) + throws OAuthSystemException { + String json = null; + try { + json = JSONUtils.buildJSON(params); + message.setBody(json); + return message; + } catch (JSONException e) { + throw new OAuthSystemException(e); + } + } +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/JSONBodyParametersApplier.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/OAuthParametersApplier.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/OAuthParametersApplier.java?rev=1050663&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/OAuthParametersApplier.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/OAuthParametersApplier.java Sat Dec 18 16:38:36 2010 @@ -0,0 +1,49 @@ +/** + * Copyright 2010 Newcastle University + * + * http://research.ncl.ac.uk/smart/ + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.amber.oauth2.common.parameters; + +import java.util.Map; + +import org.apache.amber.oauth2.common.exception.OAuthSystemException; +import org.apache.amber.oauth2.common.message.OAuthMessage; + +/** + * Applies given parameters to the OAuth message. + * Provided implementations include OAuth parameters in one of those: + *
    + *
  • HTTP request URI Query
  • + *
  • HTTP request entity-body with application/x-www-form-urlencoded encoding
  • + *
  • HTTP request entity-body with application/json encoding
  • + *
  • HTTP request Authorization/WWW-Authenticate header
  • + *
+ *

+ * Additional implementations can be provided. + * + * @author Maciej Machulak (m.p.machulak@ncl.ac.uk) + * @author Lukasz Moren (lukasz.moren@ncl.ac.uk) + * @author Aad van Moorsel (aad.vanmoorsel@ncl.ac.uk) + */ +public interface OAuthParametersApplier { + + OAuthMessage applyOAuthParameters(OAuthMessage message, Map params) throws + OAuthSystemException; +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/OAuthParametersApplier.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/QueryParameterApplier.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/QueryParameterApplier.java?rev=1050663&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/QueryParameterApplier.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/QueryParameterApplier.java Sat Dec 18 16:38:36 2010 @@ -0,0 +1,77 @@ +/** + * Copyright 2010 Newcastle University + * + * http://research.ncl.ac.uk/smart/ + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.amber.oauth2.common.parameters; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.amber.oauth2.common.OAuth; +import org.apache.amber.oauth2.common.message.OAuthMessage; +import org.apache.amber.oauth2.common.utils.OAuthUtils; + +/** + * @author Maciej Machulak (m.p.machulak@ncl.ac.uk) + * @author Lukasz Moren (lukasz.moren@ncl.ac.uk) + * @author Aad van Moorsel (aad.vanmoorsel@ncl.ac.uk) + */ +public class QueryParameterApplier implements OAuthParametersApplier { + + public OAuthMessage applyOAuthParameters(OAuthMessage message, Map params) { + + String messageUrl = message.getLocationUri(); + if (messageUrl != null) { + boolean containsQuestionMark = messageUrl.contains("?"); + StringBuffer url = new StringBuffer(messageUrl); + + //apply uri fragment component if exist access_toke param + Map fragmentParams = new HashMap(); + if (params.containsKey(OAuth.OAUTH_ACCESS_TOKEN)) { + fragmentParams.put(OAuth.OAUTH_ACCESS_TOKEN, params.remove(OAuth.OAUTH_ACCESS_TOKEN)); + + if (params.containsKey(OAuth.OAUTH_EXPIRES_IN)) { + fragmentParams.put(OAuth.OAUTH_EXPIRES_IN, params.remove(OAuth.OAUTH_EXPIRES_IN)); + } + } + + StringBuffer query = new StringBuffer(OAuthUtils.format(params.entrySet(), "UTF-8")); + String fragmentQuery = ""; + if (fragmentParams.containsKey(OAuth.OAUTH_ACCESS_TOKEN)) { + fragmentQuery = OAuthUtils.format(fragmentParams.entrySet(), "UTF-8"); + } + + if (!OAuthUtils.isEmpty(query.toString())) { + if (containsQuestionMark) { + url.append("&").append(query); + } else { + url.append("?").append(query); + } + } + + if (!OAuthUtils.isEmpty(fragmentQuery)) { + url.append("#").append(fragmentQuery); + } + + message.setLocationUri(url.toString()); + } + return message; + } +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/QueryParameterApplier.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/WWWAuthHeaderParametersApplier.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/WWWAuthHeaderParametersApplier.java?rev=1050663&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/WWWAuthHeaderParametersApplier.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/WWWAuthHeaderParametersApplier.java Sat Dec 18 16:38:36 2010 @@ -0,0 +1,44 @@ +/** + * Copyright 2010 Newcastle University + * + * http://research.ncl.ac.uk/smart/ + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.amber.oauth2.common.parameters; + +import java.util.Map; + +import org.apache.amber.oauth2.common.exception.OAuthSystemException; +import org.apache.amber.oauth2.common.message.OAuthMessage; +import org.apache.amber.oauth2.common.utils.OAuthUtils; +import org.apache.amber.oauth2.common.OAuth; + +/** + * @author Maciej Machulak (m.p.machulak@ncl.ac.uk) + * @author Lukasz Moren (lukasz.moren@ncl.ac.uk) + * @author Aad van Moorsel (aad.vanmoorsel@ncl.ac.uk) + */ +public class WWWAuthHeaderParametersApplier implements OAuthParametersApplier { + + public OAuthMessage applyOAuthParameters(OAuthMessage message, Map params) + throws OAuthSystemException { + String header = OAuthUtils.encodeOAuthHeader(params); + message.addHeader(OAuth.HeaderType.WWW_AUTHENTICATE, header); + return message; + } +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/parameters/WWWAuthHeaderParametersApplier.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/utils/JSONUtils.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/utils/JSONUtils.java?rev=1050663&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/utils/JSONUtils.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/utils/JSONUtils.java Sat Dec 18 16:38:36 2010 @@ -0,0 +1,65 @@ +/** + * Copyright 2010 Newcastle University + * + * http://research.ncl.ac.uk/smart/ + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.amber.oauth2.common.utils; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.codehaus.jettison.json.JSONException; +import org.codehaus.jettison.json.JSONObject; + +/** + * @author Maciej Machulak (m.p.machulak@ncl.ac.uk) + * @author Lukasz Moren (lukasz.moren@ncl.ac.uk) + * @author Aad van Moorsel (aad.vanmoorsel@ncl.ac.uk) + */ +public final class JSONUtils { + + public static String buildJSON(Map params) throws JSONException { + JSONObject jsonObject = new JSONObject(); + for (Map.Entry param : params.entrySet()) { + if (param.getKey() != null && !"".equals(param.getKey()) && param.getValue() != null && !"" + .equals(param.getValue())) { + jsonObject.put(param.getKey(), param.getValue()); + } + } + + return jsonObject.toString(); + } + + public static Map parseJSON(String jsonBody) throws JSONException { + + Map params = new HashMap(); + JSONObject obj = new JSONObject(jsonBody); + Iterator it = obj.keys(); + while (it.hasNext()) { + Object o = it.next(); + if (o instanceof String) { + String key = (String)o; + params.put(key, obj.getString(key)); + } + } + return params; + } + +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/utils/JSONUtils.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/utils/OAuthUtils.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/utils/OAuthUtils.java?rev=1050663&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/utils/OAuthUtils.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/utils/OAuthUtils.java Sat Dec 18 16:38:36 2010 @@ -0,0 +1,435 @@ +/** + * Copyright 2010 Newcastle University + * + * http://research.ncl.ac.uk/smart/ + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.amber.oauth2.common.utils; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.StringTokenizer; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import javax.servlet.http.HttpServletRequest; + +import org.apache.amber.oauth2.common.OAuth; +import org.apache.amber.oauth2.common.error.OAuthError; +import org.apache.amber.oauth2.common.exception.OAuthProblemException; +import org.apache.amber.oauth2.common.exception.OAuthSystemException; + +/** + * Common OAuth Utils class. + *

+ * Some methods based on the Utils class from OAuth V1.0a library available at: + * http://oauth.googlecode.com/svn/code/java/core/ + * + * @author Maciej Machulak (m.p.machulak@ncl.ac.uk) + * @author Lukasz Moren (lukasz.moren@ncl.ac.uk) + * @author Aad van Moorsel (aad.vanmoorsel@ncl.ac.uk) + */ +public final class OAuthUtils { + + private static final String ENCODING = "UTF-8"; + private static final String PARAMETER_SEPARATOR = "&"; + private static final String NAME_VALUE_SEPARATOR = "="; + + public static final String AUTH_SCHEME = "OAuth"; + + private static final Pattern OAUTH_HEADER = Pattern.compile("\\s*(\\w*)\\s+(.*)"); + private static final Pattern NVP = Pattern.compile("(\\S*)\\s*\\=\\s*\"([^\"]*)\""); + + public static final String MULTIPART = "multipart/"; + + /** + * Translates parameters into application/x-www-form-urlencoded String + * + * @param parameters parameters to encode + * @param encoding The name of a supported + * character + * encoding. + * @return Translated string + */ + public static String format( + final Collection> parameters, + final String encoding) { + final StringBuilder result = new StringBuilder(); + for (final Map.Entry parameter : parameters) { + if (!OAuthUtils.isEmpty(parameter.getKey()) + && !OAuthUtils.isEmpty(parameter.getValue())) { + final String encodedName = encode(parameter.getKey(), encoding); + final String value = parameter.getValue(); + final String encodedValue = value != null ? encode(value, encoding) : ""; + if (result.length() > 0) { + result.append(PARAMETER_SEPARATOR); + } + result.append(encodedName); + result.append(NAME_VALUE_SEPARATOR); + result.append(encodedValue); + } + } + return result.toString(); + } + + private static String encode(final String content, final String encoding) { + try { + return URLEncoder.encode(content, + encoding != null ? encoding : "UTF-8"); + } catch (UnsupportedEncodingException problem) { + throw new IllegalArgumentException(problem); + } + } + + /** + * Read data from Input Stream and save it as a String. + * + * @param is InputStream to be read + * @return String that was read from the stream + */ + public static String saveStreamAsString(InputStream is) { + if (is != null) { + InputStreamReader ir = new InputStreamReader(is); + StringBuffer sb = new StringBuffer(); + + char[] buffer = new char[1000]; + try { + while ((ir.read(buffer)) != -1) { + sb.append(buffer); + } + } catch (IOException e) { + e.printStackTrace(); + } + String string = sb.toString(); + return string.trim(); + } + + return null; + } + + /** + * Creates invalid_request exception with given message + * + * @param message error message + * @return OAuthException + */ + public static OAuthProblemException handleOAuthProblemException(String message) { + return OAuthProblemException.error(OAuthError.TokenResponse.INVALID_REQUEST) + .description(message); + } + + /** + * Creates OAuthProblemException that contains set of missing oauth parameters + * + * @param missingParams missing oauth parameters + * @return OAuthProblemException with user friendly message about missing oauth parameters + */ + public static OAuthProblemException handleMissingParameters(Set missingParams) { + StringBuffer sb = new StringBuffer("Missing parameters: "); + if (!OAuthUtils.isEmpty(missingParams)) { + for (String missingParam : missingParams) { + sb.append(missingParam).append(" "); + } + } + return handleOAuthProblemException(sb.toString().trim()); + } + + private static boolean isEmpty(Set missingParams) { + if (missingParams == null || missingParams.size() == 0) { + return true; + } + return false; + } + + public static OAuthProblemException handleNotAllowedParametersOAuthException( + List notAllowedParams) { + StringBuffer sb = new StringBuffer("Not allowed parameters: "); + if (notAllowedParams != null) { + for (String notAllowed : notAllowedParams) { + sb.append(notAllowed).append(" "); + } + } + return handleOAuthProblemException(sb.toString().trim()); + } + + /** + * Parse a form-urlencoded document. + */ + public static Map decodeForm(String form) { + Map params = new HashMap(); + if (!OAuthUtils.isEmpty(form)) { + for (String nvp : form.split("\\&")) { + int equals = nvp.indexOf('='); + String name; + String value; + if (equals < 0) { + name = decodePercent(nvp); + value = null; + } else { + name = decodePercent(nvp.substring(0, equals)); + value = decodePercent(nvp.substring(equals + 1)); + } + params.put(name, value); + } + } + return params; + } + + /** + * Return true if the given Content-Type header means FORM_ENCODED. + */ + public static boolean isFormEncoded(String contentType) { + if (contentType == null) { + return false; + } + int semi = contentType.indexOf(";"); + if (semi >= 0) { + contentType = contentType.substring(0, semi); + } + return OAuth.ContentType.URL_ENCODED.equalsIgnoreCase(contentType.trim()); + } + + public static String decodePercent(String s) { + try { + return URLDecoder.decode(s, ENCODING); + // This implements http://oauth.pbwiki.com/FlexibleDecoding + } catch (java.io.UnsupportedEncodingException wow) { + throw new RuntimeException(wow.getMessage(), wow); + } + } + + /** + * Construct a &-separated list of the given values, percentEncoded. + */ + public static String percentEncode(Iterable values) { + StringBuilder p = new StringBuilder(); + for (Object v : values) { + String stringValue = toString(v); + if (!isEmpty(stringValue)) { + if (p.length() > 0) { + p.append("&"); + } + p.append(OAuthUtils.percentEncode(toString(v))); + } + } + return p.toString(); + } + + public static String percentEncode(String s) { + if (s == null) { + return ""; + } + try { + return URLEncoder.encode(s, ENCODING) + // OAuth encodes some characters differently: + .replace("+", "%20").replace("*", "%2A") + .replace("%7E", "~"); + // This could be done faster with more hand-crafted code. + } catch (UnsupportedEncodingException wow) { + throw new RuntimeException(wow.getMessage(), wow); + } + } + + private static final String toString(Object from) { + return (from == null) ? null : from.toString(); + } + + public static Object instantiateClass(Class clazz) throws OAuthSystemException { + try { + return (Object)clazz.newInstance(); + } catch (Exception e) { + throw new OAuthSystemException(e); + } + } + + public static Object instantiateClassWithParameters(Class clazz, Class[] paramsTypes, + Object[] paramValues) throws OAuthSystemException { + + try { + if (paramsTypes != null && paramValues != null) { + if (!(paramsTypes.length == paramValues.length)) { + throw new IllegalArgumentException("Number of types and values must be equal"); + } + + if (paramsTypes.length == 0 && paramValues.length == 0) { + return clazz.newInstance(); + } + Constructor clazzConstructor = clazz.getConstructor(paramsTypes); + return clazzConstructor.newInstance(paramValues); + } + return clazz.newInstance(); + + } catch (NoSuchMethodException e) { + throw new OAuthSystemException(e); + } catch (InstantiationException e) { + throw new OAuthSystemException(e); + } catch (IllegalAccessException e) { + throw new OAuthSystemException(e); + } catch (InvocationTargetException e) { + throw new OAuthSystemException(e); + } + + } + + + public static String getAuthHeaderField(String authHeader) { + + if (authHeader != null) { + Matcher m = OAUTH_HEADER.matcher(authHeader); + if (m.matches()) { + if (AUTH_SCHEME.equalsIgnoreCase(m.group(1))) { + return m.group(2); + } + } + } + return null; + } + + public static Map decodeOAuthHeader(String header) { + Map headerValues = new HashMap(); + if (header != null) { + Matcher m = OAUTH_HEADER.matcher(header); + if (m.matches()) { + if (AUTH_SCHEME.equalsIgnoreCase(m.group(1))) { + for (String nvp : m.group(2).split("\\s*,\\s*")) { + m = NVP.matcher(nvp); + if (m.matches()) { + String name = decodePercent(m.group(1)); + String value = decodePercent(m.group(2)); + headerValues.put(name, value); + } + } + } + } + } + return headerValues; + } + + // todo: implement method to decode header form (with no challenge) + + /** + * Construct a WWW-Authenticate or Authorization header with the OAuth challenge/credentials + */ + public static String encodeOAuthHeader(Map entries) { + StringBuffer sb = new StringBuffer(); + sb.append(OAuth.OAUTH_HEADER_NAME).append(" "); + for (Map.Entry entry : entries.entrySet()) { + if (!OAuthUtils.isEmpty(entry.getKey()) && !OAuthUtils.isEmpty(entry.getValue())) { + sb.append(entry.getKey()); + sb.append("=\""); + sb.append(entry.getValue()); + sb.append("\","); + } + } + + return sb.substring(0, sb.length() - 1); + } + + public static boolean isEmpty(String value) { + return value == null || "".equals(value); + } + + public static boolean hasEmptyValues(String[] array) { + if (array == null || array.length == 0) { + return true; + } + for (String s : array) { + if (isEmpty(s)) { + return true; + } + } + return false; + } + + public static String getAuthzMethod(String header) { + if (header != null) { + Matcher m = OAUTH_HEADER.matcher(header); + if (m.matches()) { + return m.group(1); + + } + } + return null; + } + + public static Set decodeScopes(String s) { + Set scopes = new HashSet(); + if (!OAuthUtils.isEmpty(s)) { + StringTokenizer tokenizer = new StringTokenizer(s, " "); + + while (tokenizer.hasMoreElements()) { + scopes.add(tokenizer.nextToken()); + } + } + return scopes; + + } + + public static String encodeScopes(Set s) { + StringBuffer scopes = new StringBuffer(); + for (String scope : s) { + scopes.append(scope).append(" "); + } + return scopes.toString().trim(); + + } + + public static boolean isMultipart(HttpServletRequest request) { + + if (!"post".equals(request.getMethod().toLowerCase())) { + return false; + } + String contentType = request.getContentType(); + if (contentType == null) { + return false; + } + if (contentType.toLowerCase().startsWith(MULTIPART)) { + return true; + } + return false; + } + + + public static boolean hasContentType(String requestContentType, String requiredContentType) { + if (OAuthUtils.isEmpty(requiredContentType) || OAuthUtils.isEmpty(requestContentType)) { + return false; + } + StringTokenizer tokenizer = new StringTokenizer(requestContentType, ";"); + while (tokenizer.hasMoreTokens()) { + if (requiredContentType.equals(tokenizer.nextToken())) { + return true; + } + } + + return false; + } + +} + + Propchange: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/utils/OAuthUtils.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/validators/AbstractValidator.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/validators/AbstractValidator.java?rev=1050663&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/validators/AbstractValidator.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/validators/AbstractValidator.java Sat Dec 18 16:38:36 2010 @@ -0,0 +1,126 @@ +/** + * Copyright 2010 Newcastle University + * + * http://research.ncl.ac.uk/smart/ + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.amber.oauth2.common.validators; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import javax.servlet.http.HttpServletRequest; + +import org.apache.amber.oauth2.common.OAuth; +import org.apache.amber.oauth2.common.exception.OAuthProblemException; +import org.apache.amber.oauth2.common.utils.OAuthUtils; + +/** + * @author Maciej Machulak (m.p.machulak@ncl.ac.uk) + * @author Lukasz Moren (lukasz.moren@ncl.ac.uk) + * @author Aad van Moorsel (aad.vanmoorsel@ncl.ac.uk) + */ +//todo add client secret in header, sect 2.1 +public abstract class AbstractValidator implements OAuthValidator { + + protected List requiredParams = new ArrayList(); + protected Map optionalParams = new HashMap(); + protected List notAllowedParams = new ArrayList(); + + + @Override + public void validateMethod(HttpServletRequest request) throws OAuthProblemException { + if (!request.getMethod().equals(OAuth.HttpMethod.POST)) { + throw OAuthUtils.handleOAuthProblemException("Method not set to POST."); + } + } + + @Override + public void validateContentType(HttpServletRequest request) throws OAuthProblemException { + String contentType = request.getContentType(); + if (!OAuthUtils.hasContentType(contentType, OAuth.ContentType.URL_ENCODED)) { + throw OAuthUtils.handleOAuthProblemException("Bad content type."); + } + } + + @Override + public void validateRequiredParameters(HttpServletRequest request) throws OAuthProblemException { + Set missingParameters = new HashSet(); + for (String requiredParam : requiredParams) { + String val = request.getParameter(requiredParam); + if (OAuthUtils.isEmpty(val)) { + missingParameters.add(requiredParam); + } + } + if (!missingParameters.isEmpty()) { + throw OAuthUtils.handleMissingParameters(missingParameters); + } + } + + @Override + public void validateOptionalParameters(HttpServletRequest request) throws OAuthProblemException { + + Set missingParameters = new HashSet(); + + for (Map.Entry requiredParam : optionalParams.entrySet()) { + String paramName = requiredParam.getKey(); + String val = request.getParameter(paramName); + if (!OAuthUtils.isEmpty(val)) { + String[] dependentParams = requiredParam.getValue(); + if (!OAuthUtils.hasEmptyValues(dependentParams)) { + for (String dependentParam : dependentParams) { + val = request.getParameter(dependentParam); + if (OAuthUtils.isEmpty(val)) { + missingParameters.add(dependentParam); + } + } + } + } + } + + if (!missingParameters.isEmpty()) { + throw OAuthUtils.handleMissingParameters(missingParameters); + } + } + + @Override + public void validateNotAllowedParameters(HttpServletRequest request) throws OAuthProblemException { + List notAllowedParameters = new ArrayList(); + for (String requiredParam : notAllowedParams) { + String val = request.getParameter(requiredParam); + if (!OAuthUtils.isEmpty(val)) { + notAllowedParameters.add(requiredParam); + } + } + if (!notAllowedParameters.isEmpty()) { + throw OAuthUtils.handleNotAllowedParametersOAuthException(notAllowedParameters); + } + } + + @Override + public void performAllValidations(HttpServletRequest request) throws OAuthProblemException { + this.validateContentType(request); + this.validateMethod(request); + this.validateRequiredParameters(request); + this.validateOptionalParameters(request); + this.validateNotAllowedParameters(request); + } +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/validators/AbstractValidator.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/validators/OAuthValidator.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/validators/OAuthValidator.java?rev=1050663&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/validators/OAuthValidator.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/validators/OAuthValidator.java Sat Dec 18 16:38:36 2010 @@ -0,0 +1,47 @@ +/** + * Copyright 2010 Newcastle University + * + * http://research.ncl.ac.uk/smart/ + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.amber.oauth2.common.validators; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.amber.oauth2.common.exception.OAuthProblemException; + +/** + * @author Maciej Machulak (m.p.machulak@ncl.ac.uk) + * @author Lukasz Moren (lukasz.moren@ncl.ac.uk) + * @author Aad van Moorsel (aad.vanmoorsel@ncl.ac.uk) + */ +public interface OAuthValidator { + + public void validateMethod(HttpServletRequest request) throws OAuthProblemException; + + public void validateContentType(HttpServletRequest request) throws OAuthProblemException; + + public void validateRequiredParameters(HttpServletRequest request) throws OAuthProblemException; + + public void validateOptionalParameters(HttpServletRequest request) throws OAuthProblemException; + + public void validateNotAllowedParameters(HttpServletRequest request) throws OAuthProblemException; + + public void performAllValidations(HttpServletRequest request) throws OAuthProblemException; + +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-common/src/main/java/org/apache/amber/oauth2/common/validators/OAuthValidator.java ------------------------------------------------------------------------------ svn:eol-style = native