You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@oltu.apache.org by to...@apache.org on 2010/12/18 17:38:37 UTC
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...
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 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ 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.
+
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <artifactId>amber-oauth2-parent</artifactId>
+ <groupId>org.apache.amber</groupId>
+ <version>0.2-SNAPSHOT</version>
+ </parent>
+
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>oauth2-common</artifactId>
+ <name>Apache Amber: OAuth 2.0 Implementation - Common</name>
+ <version>0.2-SNAPSHOT</version>
+ <packaging>jar</packaging>
+ <description>OAuth 2.0 library - Common</description>
+
+ <build>
+ <finalName>oauth2-common</finalName>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>2.5</version>
+ <configuration>
+ <excludes>
+ <exclude>**/TestOAuthClient.java</exclude>
+ <exclude>**/TestOAuthClient.java</exclude>
+ </excludes>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>cobertura-maven-plugin</artifactId>
+ <configuration>
+ <instrumentation>
+ <excludes>
+ <exclude>org/apache/amber/oauth2/common/message/types/**.class</exclude>
+ <exclude>org/apache/amber/oauth2/common/OAuth**.class</exclude>
+ </excludes>
+ </instrumentation>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.codehaus.jettison</groupId>
+ <artifactId>jettison</artifactId>
+ <version>${jettison.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>stax</groupId>
+ <artifactId>stax-api</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ </dependencies>
+</project>
\ 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<String, String> parameters = new HashMap<String, String>();
+
+ 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<String, String> 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<String, String> getHeaders();
+
+ void setHeaders(Map<String, String> 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<String, String> headers = new HashMap<String, String>();
+
+ 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<String, String> getHeaders() {
+ return headers;
+ }
+
+ @Override
+ public void setHeaders(Map<String, String> 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<String, String> parameters = new HashMap<String, String>();
+ 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<String, String> 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<String, String> 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:
+ * <ul>
+ * <li>HTTP request URI Query</li>
+ * <li>HTTP request entity-body with application/x-www-form-urlencoded encoding</li>
+ * <li>HTTP request entity-body with application/json encoding</li>
+ * <li>HTTP request Authorization/WWW-Authenticate header</li>
+ * </ul>
+ * <p/>
+ * 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<String, String> 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<String, String> 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<String, String> fragmentParams = new HashMap<String, String>();
+ 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<String, String> 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<String, String> params) throws JSONException {
+ JSONObject jsonObject = new JSONObject();
+ for (Map.Entry<String, String> 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<String, String> parseJSON(String jsonBody) throws JSONException {
+
+ Map<String, String> params = new HashMap<String, String>();
+ 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.
+ * <p/>
+ * 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 <code>application/x-www-form-urlencoded</code> String
+ *
+ * @param parameters parameters to encode
+ * @param encoding The name of a supported
+ * <a href="../lang/package-summary.html#charenc">character
+ * encoding</a>.
+ * @return Translated string
+ */
+ public static String format(
+ final Collection<? extends Map.Entry<String, String>> parameters,
+ final String encoding) {
+ final StringBuilder result = new StringBuilder();
+ for (final Map.Entry<String, String> 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<String> 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<String> missingParams) {
+ if (missingParams == null || missingParams.size() == 0) {
+ return true;
+ }
+ return false;
+ }
+
+ public static OAuthProblemException handleNotAllowedParametersOAuthException(
+ List<String> 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<String, String> decodeForm(String form) {
+ Map<String, String> params = new HashMap<String, String>();
+ 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<String, String> decodeOAuthHeader(String header) {
+ Map<String, String> headerValues = new HashMap<String, String>();
+ 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<String, String> entries) {
+ StringBuffer sb = new StringBuffer();
+ sb.append(OAuth.OAUTH_HEADER_NAME).append(" ");
+ for (Map.Entry<String, String> 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<String> decodeScopes(String s) {
+ Set<String> scopes = new HashSet<String>();
+ if (!OAuthUtils.isEmpty(s)) {
+ StringTokenizer tokenizer = new StringTokenizer(s, " ");
+
+ while (tokenizer.hasMoreElements()) {
+ scopes.add(tokenizer.nextToken());
+ }
+ }
+ return scopes;
+
+ }
+
+ public static String encodeScopes(Set<String> 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<String> requiredParams = new ArrayList<String>();
+ protected Map<String, String[]> optionalParams = new HashMap<String, String[]>();
+ protected List<String> notAllowedParams = new ArrayList<String>();
+
+
+ @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<String> missingParameters = new HashSet<String>();
+ 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<String> missingParameters = new HashSet<String>();
+
+ for (Map.Entry<String, String[]> 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<String> notAllowedParameters = new ArrayList<String>();
+ 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