Return-Path: Delivered-To: apmail-incubator-amber-commits-archive@minotaur.apache.org Received: (qmail 83534 invoked from network); 18 Dec 2010 16:14:31 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 18 Dec 2010 16:14:31 -0000 Received: (qmail 72654 invoked by uid 500); 18 Dec 2010 16:14:31 -0000 Delivered-To: apmail-incubator-amber-commits-archive@incubator.apache.org Received: (qmail 72631 invoked by uid 500); 18 Dec 2010 16:14:31 -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 72624 invoked by uid 99); 18 Dec 2010 16:14:30 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 18 Dec 2010 16:14:30 +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:14:26 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 957DE23889B2; Sat, 18 Dec 2010 16:14:06 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: svn commit: r1050659 [1/2] - in /incubator/amber/trunk/oauth-2.0/oauth2-client: ./ 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:14:06 -0000 To: amber-commits@incubator.apache.org From: tommaso@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20101218161406.957DE23889B2@eris.apache.org> Author: tommaso Date: Sat Dec 18 16:14:04 2010 New Revision: 1050659 URL: http://svn.apache.org/viewvc?rev=1050659&view=rev Log: [AMBER-11] - import of Leelo OAuth 2.0 implementation - oauth2-client module Added: incubator/amber/trunk/oauth-2.0/oauth2-client/ (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/pom.xml (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/HttpClient.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/OAuthClient.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/URLConnectionClient.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/request/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/request/ClientHeaderParametersApplier.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/request/OAuthClientRequest.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/GitHubTokenResponse.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthAccessTokenResponse.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthAuthzResponse.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthClientResponse.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthClientResponseFactory.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthErrorResponse.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthJSONAccessTokenResponse.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/CodeTokenValidator.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/CodeValidator.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/OAuthClientValidator.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/TokenValidator.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/OAuthClientTest.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/response/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/response/GitHubTokenResponseTest.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/response/OAuthAuthorizationResponseImplTest.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/response/OAuthClientResponseFactoryTest.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/response/OAuthJSONAccessTokenResponseTest.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/utils/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/utils/TestUtils.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/validator/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/validator/CodeTokenValidatorTest.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/validator/CodeValidatorTest.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/validator/TokenValidatorTest.java (with props) incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/resources/ incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/resources/log4j.properties (with props) Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/ ------------------------------------------------------------------------------ --- svn:ignore (added) +++ svn:ignore Sat Dec 18 16:14:04 2010 @@ -0,0 +1 @@ +target Added: incubator/amber/trunk/oauth-2.0/oauth2-client/pom.xml URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-client/pom.xml?rev=1050659&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-client/pom.xml (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-client/pom.xml Sat Dec 18 16:14:04 2010 @@ -0,0 +1,78 @@ + + + + + 4.0.0 + oauth2-client + org.apache.amber + Apache Amber: OAuth 2.0 Implementation - Client + jar + 0.2-SNAPSHOT + + + amber-oauth2-parent + org.apache.amber + 0.2-SNAPSHOT + + + + oauth2-client + + + org.apache.maven.plugins + maven-surefire-plugin + 2.5 + + + **/utils/* + **/OAuthClientTest.java + + + + + + + + + org.apache.amber + oauth2-common + ${project.version} + + + + org.codehaus.jettison + jettison + ${jettison.version} + + + stax + stax-api + + + + + + + \ No newline at end of file Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/pom.xml ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/HttpClient.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/HttpClient.java?rev=1050659&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/HttpClient.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/HttpClient.java Sat Dec 18 16:14:04 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.client; + +import java.util.Map; + +import org.apache.amber.oauth2.client.request.OAuthClientRequest; +import org.apache.amber.oauth2.client.response.OAuthClientResponse; +import org.apache.amber.oauth2.common.exception.OAuthProblemException; +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 interface HttpClient { + + public T execute( + OAuthClientRequest request, + Map headers, + String requestMethod, + Class responseClass) + throws OAuthSystemException, OAuthProblemException; +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/HttpClient.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/OAuthClient.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/OAuthClient.java?rev=1050659&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/OAuthClient.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/OAuthClient.java Sat Dec 18 16:14:04 2010 @@ -0,0 +1,66 @@ +/** + * 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.client; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.amber.oauth2.client.request.OAuthClientRequest; +import org.apache.amber.oauth2.client.response.OAuthAccessTokenResponse; +import org.apache.amber.oauth2.client.response.OAuthJSONAccessTokenResponse; +import org.apache.amber.oauth2.common.exception.OAuthProblemException; +import org.apache.amber.oauth2.common.exception.OAuthSystemException; +import org.apache.amber.oauth2.common.OAuth; + +/** + * OAuth Client - exposes a high-level API for Client Applications + * + * @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 OAuthClient { + + protected HttpClient httpClient; + + public OAuthClient(HttpClient oauthClient) { + this.httpClient = oauthClient; + } + + public T accessToken( + OAuthClientRequest request, + Class responseClass) + throws OAuthSystemException, OAuthProblemException { + + String method = OAuth.HttpMethod.POST; + Map headers = new HashMap(); + headers.put(OAuth.HeaderType.CONTENT_TYPE, OAuth.ContentType.URL_ENCODED); + + return httpClient.execute(request, headers, method, responseClass); + } + + public OAuthJSONAccessTokenResponse accessToken( + OAuthClientRequest request) + throws OAuthSystemException, OAuthProblemException { + return accessToken(request, OAuthJSONAccessTokenResponse.class); + } +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/OAuthClient.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/URLConnectionClient.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/URLConnectionClient.java?rev=1050659&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/URLConnectionClient.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/URLConnectionClient.java Sat Dec 18 16:14:04 2010 @@ -0,0 +1,109 @@ +/** + * 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.client; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; +import java.util.Map; + +import org.apache.amber.oauth2.client.request.OAuthClientRequest; +import org.apache.amber.oauth2.client.response.OAuthClientResponse; +import org.apache.amber.oauth2.client.response.OAuthClientResponseFactory; +import org.apache.amber.oauth2.common.OAuth; +import org.apache.amber.oauth2.common.exception.OAuthSystemException; +import org.apache.amber.oauth2.common.utils.OAuthUtils; +import org.apache.amber.oauth2.common.exception.OAuthProblemException; + + +/** + * Implementation of the OAuth HttpClient using URL Connection + * + * @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 URLConnectionClient implements HttpClient { + + public URLConnectionClient() { + } + + public T execute(OAuthClientRequest request, Map headers, + String requestMethod, Class responseClass) + throws OAuthSystemException, OAuthProblemException { + + String responseBody = null; + URLConnection c = null; + int responseCode = 0; + try { + URL url = new URL(request.getLocationUri()); + + c = url.openConnection(); + responseCode = -1; + if (c instanceof HttpURLConnection) { + HttpURLConnection httpURLConnection = (HttpURLConnection)c; + + if (headers != null && !headers.isEmpty()) { + for (Map.Entry header : headers.entrySet()) { + httpURLConnection.addRequestProperty(header.getKey(), header.getValue()); + } + } + + if (!OAuthUtils.isEmpty(requestMethod)) { + httpURLConnection.setRequestMethod(requestMethod); + if (requestMethod.equals(OAuth.HttpMethod.POST)) { + httpURLConnection.setDoOutput(true); + OutputStream ost = httpURLConnection.getOutputStream(); + PrintWriter pw = new PrintWriter(ost); + pw.print(request.getBody()); + pw.flush(); + pw.close(); + } + } else { + httpURLConnection.setRequestMethod(OAuth.HttpMethod.GET); + } + + httpURLConnection.connect(); + + InputStream inputStream; + responseCode = httpURLConnection.getResponseCode(); + if (responseCode == 400) { + inputStream = httpURLConnection.getErrorStream(); + } else { + inputStream = httpURLConnection.getInputStream(); + } + + responseBody = OAuthUtils.saveStreamAsString(inputStream); + } + } catch (IOException e) { + throw new OAuthSystemException(e); + } + + return OAuthClientResponseFactory + .createCustomResponse(responseBody, c.getContentType(), responseCode, responseClass); + } + +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/URLConnectionClient.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/request/ClientHeaderParametersApplier.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/request/ClientHeaderParametersApplier.java?rev=1050659&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/request/ClientHeaderParametersApplier.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/request/ClientHeaderParametersApplier.java Sat Dec 18 16:14:04 2010 @@ -0,0 +1,48 @@ +/** + * 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.client.request; + +import java.util.Map; + +import org.apache.amber.oauth2.common.OAuth; +import org.apache.amber.oauth2.common.exception.OAuthSystemException; +import org.apache.amber.oauth2.common.message.OAuthMessage; +import org.apache.amber.oauth2.common.parameters.OAuthParametersApplier; +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 ClientHeaderParametersApplier implements OAuthParametersApplier { + + public OAuthMessage applyOAuthParameters(OAuthMessage message, Map params) + throws OAuthSystemException { + + String header = OAuthUtils.encodeOAuthHeader(params); + message.addHeader(OAuth.HeaderType.AUTHORIZATION, header); + return message; + + } + +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/request/ClientHeaderParametersApplier.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/request/OAuthClientRequest.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/request/OAuthClientRequest.java?rev=1050659&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/request/OAuthClientRequest.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/request/OAuthClientRequest.java Sat Dec 18 16:14:04 2010 @@ -0,0 +1,233 @@ +/** + * 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.client.request; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.amber.oauth2.common.OAuth; +import org.apache.amber.oauth2.common.exception.OAuthSystemException; +import org.apache.amber.oauth2.common.message.OAuthMessage; +import org.apache.amber.oauth2.common.parameters.OAuthParametersApplier; +import org.apache.amber.oauth2.common.parameters.QueryParameterApplier; +import org.apache.amber.oauth2.common.message.types.GrantType; +import org.apache.amber.oauth2.common.parameters.BodyURLEncodedParametersApplier; + +/** + * OAuth Client Request + * + * @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 OAuthClientRequest implements OAuthMessage { + + protected String url; + protected String body; + protected Map headers; + + protected OAuthClientRequest(String url) { + this.url = url; + } + + public static AuthenticationRequestBuilder authorizationLocation(String url) { + return new AuthenticationRequestBuilder(url); + } + + public static TokenRequestBuilder tokenLocation(String url) { + return new TokenRequestBuilder(url); + } + + public String getBody() { + return body; + } + + public void setBody(String body) { + this.body = body; + } + + public Map getHeaders() { + return headers; + } + + @Override + public void addHeader(String name, String header) { + this.headers.put(name, header); + } + + public void setHeaders(Map headers) { + this.headers = headers; + } + + public String getLocationUri() { + return url; + } + + public void setLocationUri(String uri) { + this.url = uri; + } + + public String getHeader(String name) { + return this.headers.get(name); + } + + public void setHeader(String name, String value) { + this.headers.put(name, value); + } + + public abstract static class OAuthRequestBuilder { + + protected OAuthParametersApplier applier; + protected Map parameters = new HashMap(); + + protected String url; + + protected OAuthRequestBuilder(String url) { + this.url = url; + } + + public OAuthClientRequest buildQueryMessage() throws OAuthSystemException { + OAuthClientRequest request = new OAuthClientRequest(url); + this.applier = new QueryParameterApplier(); + return (OAuthClientRequest)applier.applyOAuthParameters(request, parameters); + } + + public OAuthClientRequest buildBodyMessage() throws OAuthSystemException { + OAuthClientRequest request = new OAuthClientRequest(url); + this.applier = new BodyURLEncodedParametersApplier(); + return (OAuthClientRequest)applier.applyOAuthParameters(request, parameters); + } + + public OAuthClientRequest buildHeaderMessage() throws OAuthSystemException { + OAuthClientRequest request = new OAuthClientRequest(url); + this.applier = new ClientHeaderParametersApplier(); + return (OAuthClientRequest)applier.applyOAuthParameters(request, parameters); + } + } + + public static class AuthenticationRequestBuilder extends OAuthRequestBuilder { + + public AuthenticationRequestBuilder(String url) { + super(url); + } + + public AuthenticationRequestBuilder setResponseType(String type) { + this.parameters.put(OAuth.OAUTH_RESPONSE_TYPE, type); + return this; + } + + public AuthenticationRequestBuilder setClientId(String clientId) { + this.parameters.put(OAuth.OAUTH_CLIENT_ID, clientId); + return this; + } + + public AuthenticationRequestBuilder setRedirectURI(String uri) { + this.parameters.put(OAuth.OAUTH_REDIRECT_URI, uri); + return this; + } + + public AuthenticationRequestBuilder setState(String state) { + this.parameters.put(OAuth.OAUTH_STATE, state); + return this; + } + + public AuthenticationRequestBuilder setScope(String scope) { + this.parameters.put(OAuth.OAUTH_SCOPE, scope); + return this; + } + + public AuthenticationRequestBuilder setParameter(String paramName, String paramValue) { + this.parameters.put(paramName, paramValue); + return this; + } + } + + public static class TokenRequestBuilder extends OAuthRequestBuilder { + + public TokenRequestBuilder(String url) { + super(url); + } + + public TokenRequestBuilder setGrantType(GrantType grantType) { + this.parameters.put(OAuth.OAUTH_GRANT_TYPE, grantType.toString()); + return this; + } + + public TokenRequestBuilder setClientId(String clientId) { + this.parameters.put(OAuth.OAUTH_CLIENT_ID, clientId); + return this; + } + + public TokenRequestBuilder setClientSecret(String secret) { + this.parameters.put(OAuth.OAUTH_CLIENT_SECRET, secret); + return this; + } + + public TokenRequestBuilder setUsername(String username) { + this.parameters.put(OAuth.OAUTH_USERNAME, username); + return this; + } + + public TokenRequestBuilder setPassword(String password) { + this.parameters.put(OAuth.OAUTH_PASSWORD, password); + return this; + } + + public TokenRequestBuilder setScope(String scope) { + this.parameters.put(OAuth.OAUTH_SCOPE, scope); + return this; + } + + public TokenRequestBuilder setCode(String code) { + this.parameters.put(OAuth.OAUTH_CODE, code); + return this; + } + + public TokenRequestBuilder setRedirectURI(String uri) { + this.parameters.put(OAuth.OAUTH_REDIRECT_URI, uri); + return this; + } + + public TokenRequestBuilder setAssertion(String assertion) { + this.parameters.put(OAuth.OAUTH_ASSERTION, assertion); + return this; + } + + public TokenRequestBuilder setAssertionType(String assertionType) { + this.parameters.put(OAuth.OAUTH_ASSERTION_TYPE, assertionType); + return this; + } + + public TokenRequestBuilder setRefreshToken(String token) { + this.parameters.put(OAuth.OAUTH_REFRESH_TOKEN, token); + return this; + } + + public TokenRequestBuilder setParameter(String paramName, String paramValue) { + this.parameters.put(paramName, paramValue); + return this; + } + + + } +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/request/OAuthClientRequest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/GitHubTokenResponse.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/GitHubTokenResponse.java?rev=1050659&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/GitHubTokenResponse.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/GitHubTokenResponse.java Sat Dec 18 16:14:04 2010 @@ -0,0 +1,69 @@ +/** + * 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.client.response; + +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 GitHubTokenResponse extends OAuthAccessTokenResponse { + + + public String getAccessToken() { + return parameters.get(OAuth.OAUTH_ACCESS_TOKEN); + } + + public String getExpiresIn() { + return parameters.get(OAuth.OAUTH_EXPIRES_IN); + } + + public String getRefreshToken() { + return parameters.get(OAuth.OAUTH_EXPIRES_IN); + } + + public String getScope() { + return parameters.get(OAuth.OAUTH_SCOPE); + } + + protected void setBody(String body) { + this.body = body; + parameters = OAuthUtils.decodeForm(body); + } + + protected void setContentType(String contentType) { + this.contentType = contentType; + } + + protected void setResponseCode(int code) { + this.responseCode = code; + } + + public String getParam(String name) { + return parameters.get(name); + } + +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/GitHubTokenResponse.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthAccessTokenResponse.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthAccessTokenResponse.java?rev=1050659&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthAccessTokenResponse.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthAccessTokenResponse.java Sat Dec 18 16:14:04 2010 @@ -0,0 +1,51 @@ +/** + * 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.client.response; + +import org.apache.amber.oauth2.client.validator.TokenValidator; +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 abstract class OAuthAccessTokenResponse extends OAuthClientResponse { + + public abstract String getAccessToken(); + + public abstract String getExpiresIn(); + + public abstract String getRefreshToken(); + + public abstract String getScope(); + + public String getBody() { + return body; + } + + @Override + protected void init(String body, String contentType, int responseCode) throws OAuthProblemException { + validator = new TokenValidator(); + super.init(body, contentType, responseCode); + } +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthAccessTokenResponse.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthAuthzResponse.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthAuthzResponse.java?rev=1050659&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthAuthzResponse.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthAuthzResponse.java Sat Dec 18 16:14:04 2010 @@ -0,0 +1,119 @@ +/** + * 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.client.response; + +import java.util.Map; +import javax.servlet.http.HttpServletRequest; + +import org.apache.amber.oauth2.client.validator.CodeTokenValidator; +import org.apache.amber.oauth2.client.validator.CodeValidator; +import org.apache.amber.oauth2.client.validator.OAuthClientValidator; +import org.apache.amber.oauth2.client.validator.TokenValidator; +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) + */ +public class OAuthAuthzResponse extends OAuthClientResponse { + + private HttpServletRequest request; + + protected OAuthAuthzResponse(HttpServletRequest request, OAuthClientValidator validator) { + this.request = request; + Map params = request.getParameterMap(); + for (Map.Entry entry : params.entrySet()) { + String key = entry.getKey(); + String[] values = entry.getValue(); + if (!OAuthUtils.hasEmptyValues(values)) { + parameters.put(key, values[0]); + } + } + this.validator = validator; + } + + public static OAuthAuthzResponse oauthCodeAuthzResponse(HttpServletRequest request) + throws OAuthProblemException { + OAuthAuthzResponse response = new OAuthAuthzResponse(request, new CodeValidator()); + response.validate(); + return response; + } + + public static OAuthAuthzResponse oAuthCodeAndTokenAuthzResponse(HttpServletRequest request) + throws OAuthProblemException { + OAuthAuthzResponse response = new OAuthAuthzResponse(request, new CodeTokenValidator()); + response.validate(); + return response; + } + + public static OAuthAuthzResponse oauthTokenAuthzResponse(HttpServletRequest request) + throws OAuthProblemException { + OAuthAuthzResponse response = new OAuthAuthzResponse(request, new TokenValidator()); + response.validate(); + return response; + } + + public String getAccessToken() { + return getParam(OAuth.OAUTH_ACCESS_TOKEN); + } + + public String getExpiresIn() { + return getParam(OAuth.OAUTH_EXPIRES_IN); + } + + public String getScope() { + return getParam(OAuth.OAUTH_SCOPE); + } + + public String getCode() { + return getParam(OAuth.OAUTH_CODE); + } + + public String getState() { + return getParam(OAuth.OAUTH_STATE); + } + + public HttpServletRequest getRequest() { + return request; + } + + public String getParam(String key) { + return this.parameters.get(key); + } + + protected void setBody(String body) { + this.body = body; + } + + protected void setContentType(String contentType) { + this.contentType = contentType; + } + + protected void setResponseCode(int responseCode) { + this.responseCode = responseCode; + } + +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthAuthzResponse.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthClientResponse.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthClientResponse.java?rev=1050659&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthClientResponse.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthClientResponse.java Sat Dec 18 16:14:04 2010 @@ -0,0 +1,63 @@ +/** + * 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.client.response; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.amber.oauth2.client.validator.OAuthClientValidator; +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 abstract class OAuthClientResponse { + + protected String body; + protected String contentType; + protected int responseCode; + + protected OAuthClientValidator validator; + protected Map parameters = new HashMap(); + + public abstract String getParam(String param); + + protected abstract void setBody(String body) throws OAuthProblemException; + + protected abstract void setContentType(String contentTypr); + + protected abstract void setResponseCode(int responseCode); + + protected void init(String body, String contentType, int responseCode) throws OAuthProblemException { + this.setBody(body); + this.setContentType(contentType); + this.setResponseCode(responseCode); + this.validate(); + + } + + protected void validate() throws OAuthProblemException { + validator.validate(this); + } +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthClientResponse.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthClientResponseFactory.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthClientResponseFactory.java?rev=1050659&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthClientResponseFactory.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthClientResponseFactory.java Sat Dec 18 16:14:04 2010 @@ -0,0 +1,66 @@ +/** + * 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.client.response; + + +import org.apache.amber.oauth2.common.exception.OAuthProblemException; +import org.apache.amber.oauth2.common.exception.OAuthSystemException; +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 OAuthClientResponseFactory { + + public static OAuthClientResponse createGitHubTokenResponse(String body, String contentType, + int responseCode) + throws OAuthProblemException { + GitHubTokenResponse resp = new GitHubTokenResponse(); + resp.init(body, contentType, responseCode); + return resp; + } + + public static OAuthClientResponse createJSONTokenResponse(String body, String contentType, + int responseCode) + throws OAuthProblemException { + OAuthJSONAccessTokenResponse resp = new OAuthJSONAccessTokenResponse(); + resp.init(body, contentType, responseCode); + return resp; + } + + public static T createCustomResponse(String body, String contentType, + int responseCode, + Class clazz) + throws OAuthSystemException, OAuthProblemException { + + OAuthClientResponse resp = (OAuthClientResponse)OAuthUtils + .instantiateClassWithParameters(clazz, null, null); + + resp.init(body, contentType, responseCode); + + return (T)resp; + } + + +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthClientResponseFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthErrorResponse.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthErrorResponse.java?rev=1050659&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthErrorResponse.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthErrorResponse.java Sat Dec 18 16:14:04 2010 @@ -0,0 +1,61 @@ +/** + * 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.client.response; + + +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 class OAuthErrorResponse { + + private String error; + private String errorDescription; + private String errorUri; + private String state; + + public OAuthErrorResponse(OAuthProblemException ex) { + this.error = ex.getError(); + this.errorDescription = ex.getDescription(); + this.errorUri = ex.getUri(); + this.state = ex.getState(); + } + + public String getError() { + return error; + } + + public String getErrorDescription() { + return errorDescription; + } + + public String getErrorUri() { + return errorUri; + } + + public String getState() { + return state; + } +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthErrorResponse.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthJSONAccessTokenResponse.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthJSONAccessTokenResponse.java?rev=1050659&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthJSONAccessTokenResponse.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthJSONAccessTokenResponse.java Sat Dec 18 16:14:04 2010 @@ -0,0 +1,83 @@ +/** + * 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.client.response; + +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.codehaus.jettison.json.JSONException; + +import org.apache.amber.oauth2.common.utils.JSONUtils; + +/** + * @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 OAuthJSONAccessTokenResponse extends OAuthAccessTokenResponse { + + public OAuthJSONAccessTokenResponse() { + } + + @Override + public String getAccessToken() { + return parameters.get(OAuth.OAUTH_ACCESS_TOKEN); + } + + @Override + public String getExpiresIn() { + return parameters.get(OAuth.OAUTH_EXPIRES_IN); + } + + public String getScope() { + return parameters.get(OAuth.OAUTH_SCOPE); + } + + public String getRefreshToken() { + return parameters.get(OAuth.OAUTH_REFRESH_TOKEN); + } + + protected void setBody(String body) throws OAuthProblemException { + + try { + this.body = body; + parameters = JSONUtils.parseJSON(body); + } catch (JSONException e) { + throw OAuthProblemException.error(OAuthError.CodeResponse.UNSUPPORTED_RESPONSE_TYPE, + "Invalid response! Response body is not " + OAuth.ContentType.JSON + " encoded"); + } + } + + protected void setContentType(String contentType) { + this.contentType = contentType; + } + + + protected void setResponseCode(int code) { + this.responseCode = code; + } + + public String getParam(String name) { + return parameters.get(name); + } + +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/response/OAuthJSONAccessTokenResponse.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/CodeTokenValidator.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/CodeTokenValidator.java?rev=1050659&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/CodeTokenValidator.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/CodeTokenValidator.java Sat Dec 18 16:14:04 2010 @@ -0,0 +1,40 @@ +/** + * 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.client.validator; + + +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 CodeTokenValidator extends OAuthClientValidator { + + public CodeTokenValidator() { + requiredParams.put(OAuth.OAUTH_CODE, new String[] {}); + requiredParams.put(OAuth.OAUTH_ACCESS_TOKEN, new String[] {}); + + notAllowedParams.add(OAuth.OAUTH_ACCESS_TOKEN); + } +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/CodeTokenValidator.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/CodeValidator.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/CodeValidator.java?rev=1050659&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/CodeValidator.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/CodeValidator.java Sat Dec 18 16:14:04 2010 @@ -0,0 +1,41 @@ +/** + * 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.client.validator; + + +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 CodeValidator extends OAuthClientValidator { + + public CodeValidator() { + + requiredParams.put(OAuth.OAUTH_CODE, new String[] {}); + + notAllowedParams.add(OAuth.OAUTH_ACCESS_TOKEN); + notAllowedParams.add(OAuth.OAUTH_EXPIRES_IN); + } +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/CodeValidator.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/OAuthClientValidator.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/OAuthClientValidator.java?rev=1050659&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/OAuthClientValidator.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/OAuthClientValidator.java Sat Dec 18 16:14:04 2010 @@ -0,0 +1,108 @@ +/** + * 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.client.validator; + +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 org.apache.amber.oauth2.common.error.OAuthError; +import org.apache.amber.oauth2.common.exception.OAuthProblemException; +import org.apache.amber.oauth2.common.utils.OAuthUtils; +import org.apache.amber.oauth2.client.response.OAuthClientResponse; +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 abstract class OAuthClientValidator { + + protected Map requiredParams = new HashMap(); + protected List notAllowedParams = new ArrayList(); + + public void validate(OAuthClientResponse response) throws OAuthProblemException { + validateErrorResponse(response); + validateParameters(response); + } + + public void validateParameters(OAuthClientResponse response) throws OAuthProblemException { + validateRequiredParameters(response); + validateNotAllowedParameters(response); + } + + public void validateErrorResponse(OAuthClientResponse response) throws OAuthProblemException { + String error = response.getParam(OAuthError.OAUTH_ERROR); + if (!OAuthUtils.isEmpty(error)) { + String errorDesc = response.getParam(OAuthError.OAUTH_ERROR_DESCRIPTION); + String errorUri = response.getParam(OAuthError.OAUTH_ERROR_URI); + String state = response.getParam(OAuth.OAUTH_STATE); + throw OAuthProblemException.error(error).description(errorDesc).uri(errorUri).state(state); + } + } + + + public void validateRequiredParameters(OAuthClientResponse response) throws OAuthProblemException { + Set missingParameters = new HashSet(); + + for (Map.Entry requiredParam : requiredParams.entrySet()) { + String paramName = requiredParam.getKey(); + String val = response.getParam(paramName); + if (OAuthUtils.isEmpty(val)) { + missingParameters.add(paramName); + } else { + String[] dependentParams = requiredParam.getValue(); + if (!OAuthUtils.hasEmptyValues(dependentParams)) { + for (String dependentParam : dependentParams) { + val = response.getParam(dependentParam); + if (OAuthUtils.isEmpty(val)) { + missingParameters.add(dependentParam); + } + } + } + } + } + + if (!missingParameters.isEmpty()) { + throw OAuthUtils.handleMissingParameters(missingParameters); + } + } + + public void validateNotAllowedParameters(OAuthClientResponse response) throws OAuthProblemException { + List notAllowedParameters = new ArrayList(); + for (String requiredParam : notAllowedParams) { + String val = response.getParam(requiredParam); + if (!OAuthUtils.isEmpty(val)) { + notAllowedParameters.add(requiredParam); + } + } + if (!notAllowedParameters.isEmpty()) { + throw OAuthUtils.handleNotAllowedParametersOAuthException(notAllowedParameters); + } + } + + +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/OAuthClientValidator.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/TokenValidator.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/TokenValidator.java?rev=1050659&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/TokenValidator.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/TokenValidator.java Sat Dec 18 16:14:04 2010 @@ -0,0 +1,40 @@ +/** + * 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.client.validator; + + +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 TokenValidator extends OAuthClientValidator { + + public TokenValidator() { + + requiredParams.put(OAuth.OAUTH_ACCESS_TOKEN, new String[] {}); + + notAllowedParams.add(OAuth.OAUTH_CODE); + } +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/src/main/java/org/apache/amber/oauth2/client/validator/TokenValidator.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/OAuthClientTest.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/OAuthClientTest.java?rev=1050659&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/OAuthClientTest.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/OAuthClientTest.java Sat Dec 18 16:14:04 2010 @@ -0,0 +1,94 @@ +/** + * 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.client; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +import org.apache.amber.oauth2.client.OAuthClient; +import org.apache.amber.oauth2.client.URLConnectionClient; +import org.apache.amber.oauth2.client.request.OAuthClientRequest; +import org.apache.amber.oauth2.common.exception.OAuthSystemException; +import org.junit.Test; + +import org.apache.amber.oauth2.client.response.GitHubTokenResponse; +import org.apache.amber.oauth2.common.exception.OAuthProblemException; +import org.apache.amber.oauth2.common.message.types.GrantType; + +/** + * Simple example that shows how to get OAuth 2.0 access token from Facebook + * using Amber OAuth 2.0 library + * + * @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 OAuthClientTest { + + public static void main(String[] args) throws OAuthSystemException, IOException { + + try { + OAuthClientRequest request = OAuthClientRequest + .authorizationLocation("https://graph.facebook.com/oauth/authorize") + .setClientId("131804060198305") + .setRedirectURI("http://localhost:8080/") + .buildQueryMessage(); + + //in web application you make redirection to uri: + System.out.println("Visit: " + request.getLocationUri() + "\nand grant permission"); + + System.out.print("Now enter the OAuth code you have received in redirect uri "); + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String code = br.readLine(); + + request = OAuthClientRequest + .tokenLocation("https://graph.facebook.com/oauth/access_token") + .setGrantType(GrantType.AUTHORIZATION_CODE) + .setClientId("131804060198305") + .setClientSecret("3acb294b071c9aec86d60ae3daf32a93") + .setRedirectURI("http://localhost:8080/") + .setCode(code) + .buildBodyMessage(); + + OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient()); + + //Facebook is not fully compatible with OAuth 2.0 draft 10, access token response is + //application/x-www-form-urlencoded, not json encoded so we use dedicated response class for that + //Own response class is an easy way to deal with oauth providers that introduce modifications to + //OAuth specification + GitHubTokenResponse oAuthResponse = oAuthClient.accessToken(request, GitHubTokenResponse.class); + + System.out.println( + "Access Token: " + oAuthResponse.getAccessToken() + ", Expires in: " + oAuthResponse + .getExpiresIn()); + } catch (OAuthProblemException e) { + System.out.println("OAuth error: " + e.getError()); + System.out.println("OAuth error description: " + e.getDescription()); + } + } + + @Test + public void dummyTest() { + + } +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/OAuthClientTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/response/GitHubTokenResponseTest.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/response/GitHubTokenResponseTest.java?rev=1050659&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/response/GitHubTokenResponseTest.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/response/GitHubTokenResponseTest.java Sat Dec 18 16:14:04 2010 @@ -0,0 +1,56 @@ +/** + * 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.client.response; + +import org.junit.Test; + +/** + * @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 GitHubTokenResponseTest { + @Test + public void testGetAccessToken() throws Exception { + + } + + @Test + public void testGetExpiresIn() throws Exception { + } + + @Test + public void testGetRefreshToken() throws Exception { + } + + @Test + public void testGetScope() throws Exception { + } + + @Test + public void testGet() throws Exception { + } + + @Test + public void testGetError() throws Exception { + } +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/response/GitHubTokenResponseTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/response/OAuthAuthorizationResponseImplTest.java URL: http://svn.apache.org/viewvc/incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/response/OAuthAuthorizationResponseImplTest.java?rev=1050659&view=auto ============================================================================== --- incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/response/OAuthAuthorizationResponseImplTest.java (added) +++ incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/response/OAuthAuthorizationResponseImplTest.java Sat Dec 18 16:14:04 2010 @@ -0,0 +1,333 @@ +/** + * 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.client.response; + +import java.util.HashMap; +import java.util.Map; +import javax.servlet.http.HttpServletRequest; + +import org.apache.amber.oauth2.client.utils.TestUtils; +import org.apache.amber.oauth2.common.OAuth; +import org.apache.amber.oauth2.common.exception.OAuthProblemException; +import org.junit.Assert; +import org.junit.Test; +import org.apache.amber.oauth2.common.error.OAuthError; + +import static org.easymock.EasyMock.createStrictMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.reset; +import static org.easymock.EasyMock.verify; +import static org.junit.Assert.fail; + +/** + * @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 OAuthAuthorizationResponseImplTest { + + + @Test + public void testGetAccessToken() throws Exception { + + HttpServletRequest request = createStrictMock(HttpServletRequest.class); + + //check valid request + TestUtils.expectNoErrorParameters(request); + Map parameters = new HashMap(); + parameters.put(OAuth.OAUTH_ACCESS_TOKEN, new String[] {"test_access_token"}); + parameters.put(OAuth.OAUTH_CODE, null); + parameters.put(OAuth.OAUTH_SCOPE, null); + parameters.put(OAuth.OAUTH_EXPIRES_IN, null); + + expect(request.getParameterMap()).andStubReturn(parameters); + + replay(request); + + OAuthAuthzResponse r = null; + try { + r = OAuthAuthzResponse.oauthTokenAuthzResponse(request); + } catch (OAuthProblemException e) { + fail("Exception not expected"); + } + + String token = r.getAccessToken(); + Assert.assertNotNull(token); + + verify(request); + + reset(request); + + //both parameters code and access token + TestUtils.expectNoErrorParameters(request); + parameters.put(OAuth.OAUTH_CODE, new String[] {"code"}); + parameters.put(OAuth.OAUTH_ACCESS_TOKEN, null); + parameters.put(OAuth.OAUTH_SCOPE, null); + parameters.put(OAuth.OAUTH_EXPIRES_IN, null); + + expect(request.getParameterMap()).andReturn(parameters); + + replay(request); + + r = OAuthAuthzResponse.oauthCodeAuthzResponse(request); + token = r.getAccessToken(); + Assert.assertNull(token); + verify(request); + + reset(request); + + //both parameters code and access token + TestUtils.expectNoErrorParameters(request); + + parameters.put(OAuth.OAUTH_CODE, new String[] {"test_code"}); + parameters.put(OAuth.OAUTH_ACCESS_TOKEN, new String[] {"test_access_token"}); + parameters.put(OAuth.OAUTH_SCOPE, null); + parameters.put(OAuth.OAUTH_EXPIRES_IN, null); + + expect(request.getParameterMap()).andReturn(parameters); + + replay(request); + + try { + OAuthAuthzResponse.oauthTokenAuthzResponse(request); + fail("Exception expected"); + } catch (OAuthProblemException e) { + Assert.assertTrue( + e.getError().equals(OAuthError.TokenResponse.INVALID_REQUEST)); + } + + + } + + + @Test + public void testGetExpiresIn() throws Exception { + HttpServletRequest request = createStrictMock(HttpServletRequest.class); + + TestUtils.expectNoErrorParameters(request); + Map parameters = new HashMap(); + parameters.put(OAuth.OAUTH_CODE, new String[] {"test_code"}); + parameters.put(OAuth.OAUTH_ACCESS_TOKEN, null); + parameters.put(OAuth.OAUTH_SCOPE, new String[] {"test_scope"}); + parameters.put(OAuth.OAUTH_EXPIRES_IN, new String[] {"test_expires_in"}); + + expect(request.getParameterMap()).andStubReturn(parameters); + + replay(request); + + OAuthAuthzResponse r = null; + try { + OAuthAuthzResponse.oauthCodeAuthzResponse(request); + fail("Exception expected"); + } catch (OAuthProblemException e) { + Assert.assertTrue( + e.getError().equals(OAuthError.TokenResponse.INVALID_REQUEST)); + } + + verify(request); + + reset(request); + + TestUtils.expectNoErrorParameters(request); + parameters.put(OAuth.OAUTH_CODE, null); + parameters.put(OAuth.OAUTH_ACCESS_TOKEN, new String[] {"token"}); + parameters.put(OAuth.OAUTH_SCOPE, new String[] {"test_scope"}); + parameters.put(OAuth.OAUTH_EXPIRES_IN, new String[] {"3600"}); + + expect(request.getParameterMap()).andStubReturn(parameters); + + replay(request); + + try { + r = OAuthAuthzResponse.oauthTokenAuthzResponse(request); + } catch (OAuthProblemException e) { + fail("Exception not expected"); + } + + Assert.assertNotNull(r.getExpiresIn()); + verify(request); + } + + @Test + public void testGetScope() throws Exception { + HttpServletRequest request = createStrictMock(HttpServletRequest.class); + + TestUtils.expectNoErrorParameters(request); + + Map parameters = new HashMap(); + parameters.put(OAuth.OAUTH_CODE, new String[] {"test_code"}); + parameters.put(OAuth.OAUTH_ACCESS_TOKEN, null); + parameters.put(OAuth.OAUTH_SCOPE, new String[] {"test_scope"}); + parameters.put(OAuth.OAUTH_EXPIRES_IN, new String[] {"3600"}); + + expect(request.getParameterMap()).andStubReturn(parameters); + + replay(request); + + OAuthAuthzResponse r = null; + try { + OAuthAuthzResponse.oauthCodeAuthzResponse(request); + fail("Exception expected"); + } catch (OAuthProblemException e) { + Assert.assertTrue( + e.getError().equals(OAuthError.TokenResponse.INVALID_REQUEST)); + + } + + verify(request); + + reset(request); + TestUtils.expectNoErrorParameters(request); + + parameters.put(OAuth.OAUTH_CODE, null); + parameters.put(OAuth.OAUTH_ACCESS_TOKEN, new String[] {"token"}); + parameters.put(OAuth.OAUTH_SCOPE, new String[] {"test_scope"}); + parameters.put(OAuth.OAUTH_EXPIRES_IN, new String[] {"3600"}); + + expect(request.getParameterMap()).andStubReturn(parameters); + + replay(request); + + try { + r = OAuthAuthzResponse.oauthTokenAuthzResponse(request); + } catch (OAuthProblemException e) { + fail("Exception not expected"); + } + Assert.assertNotNull(r.getScope()); + verify(request); + + } + + @Test + public void testGetCode() throws Exception { + HttpServletRequest request = createStrictMock(HttpServletRequest.class); + + //check valid request + TestUtils.expectNoErrorParameters(request); + Map parameters = new HashMap(); + parameters.put(OAuth.OAUTH_CODE, null); + parameters.put(OAuth.OAUTH_ACCESS_TOKEN, new String[] {"test_access_token"}); + parameters.put(OAuth.OAUTH_SCOPE, new String[] {null}); + parameters.put(OAuth.OAUTH_EXPIRES_IN, new String[] {null}); + + expect(request.getParameterMap()).andStubReturn(parameters); + + replay(request); + + OAuthAuthzResponse r = OAuthAuthzResponse.oauthTokenAuthzResponse(request); + String code = r.getCode(); + Assert.assertNull(code); + + verify(request); + + reset(request); + + //both parameters code and access token + TestUtils.expectNoErrorParameters(request); + + parameters.put(OAuth.OAUTH_CODE, new String[] {"code"}); + parameters.put(OAuth.OAUTH_ACCESS_TOKEN, null); + parameters.put(OAuth.OAUTH_SCOPE, new String[] {null}); + parameters.put(OAuth.OAUTH_EXPIRES_IN, new String[] {null}); + + expect(request.getParameterMap()).andStubReturn(parameters); + replay(request); + + r = OAuthAuthzResponse.oauthCodeAuthzResponse(request); + code = r.getCode(); + Assert.assertNotNull(code); + verify(request); + + reset(request); + + //both parameters code and access token + TestUtils.expectNoErrorParameters(request); + + parameters.put(OAuth.OAUTH_CODE, new String[] {"test_code"}); + parameters.put(OAuth.OAUTH_ACCESS_TOKEN, new String[] {"test_access_token"}); + parameters.put(OAuth.OAUTH_SCOPE, new String[] {null}); + parameters.put(OAuth.OAUTH_EXPIRES_IN, new String[] {null}); + + expect(request.getParameterMap()).andStubReturn(parameters); + + replay(request); + + + try { + OAuthAuthzResponse.oauthCodeAuthzResponse(request); + fail("Exception expected"); + } catch (OAuthProblemException e) { + Assert.assertTrue( + e.getError().equals(OAuthError.TokenResponse.INVALID_REQUEST)); + + } + + } + + @Test + public void testGetState() throws Exception { + + HttpServletRequest request = createStrictMock(HttpServletRequest.class); + + //check valid request + TestUtils.expectNoErrorParameters(request); + + Map parameters = new HashMap(); + parameters.put(OAuth.OAUTH_CODE, new String[] {"test_code"}); + parameters.put(OAuth.OAUTH_ACCESS_TOKEN, null); + parameters.put(OAuth.OAUTH_SCOPE, new String[] {null}); + parameters.put(OAuth.OAUTH_STATE, new String[] {"test_state"}); + parameters.put(OAuth.OAUTH_EXPIRES_IN, new String[] {null}); + + expect(request.getParameterMap()).andStubReturn(parameters); + + replay(request); + + OAuthAuthzResponse r = OAuthAuthzResponse.oauthCodeAuthzResponse(request); + String state = r.getState(); + Assert.assertNotNull(state); + + verify(request); + + reset(request); + //check valid request + TestUtils.expectNoErrorParameters(request); + + parameters.put(OAuth.OAUTH_CODE, new String[] {null}); + parameters.put(OAuth.OAUTH_ACCESS_TOKEN, new String[] {"test_access_token"}); + parameters.put(OAuth.OAUTH_SCOPE, new String[] {null}); + parameters.put(OAuth.OAUTH_STATE, new String[] {"test_state"}); + parameters.put(OAuth.OAUTH_EXPIRES_IN, new String[] {null}); + + expect(request.getParameterMap()).andStubReturn(parameters); + + replay(request); + + r = OAuthAuthzResponse.oauthTokenAuthzResponse(request); + state = r.getState(); + Assert.assertNotNull(state); + + verify(request); + } + +} Propchange: incubator/amber/trunk/oauth-2.0/oauth2-client/src/test/java/org/apache/amber/oauth2/client/response/OAuthAuthorizationResponseImplTest.java ------------------------------------------------------------------------------ svn:eol-style = native