001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.component.gae.auth;
018
019 import java.io.IOException;
020
021 import com.google.gdata.client.authn.oauth.GoogleOAuthParameters;
022
023 import org.apache.camel.Exchange;
024 import org.apache.camel.component.gae.bind.OutboundBinding;
025
026 /**
027 * Binds {@link GoogleOAuthParameters} to a Camel {@link Exchange}. This binding
028 * is used by <code>gauth:upgrade</code> endpoints by default.
029 */
030 public class GAuthUpgradeBinding implements OutboundBinding<GAuthEndpoint, GoogleOAuthParameters, GoogleOAuthParameters> {
031
032 /**
033 * Name of the Camel header containing an access token.
034 */
035 public static final String GAUTH_ACCESS_TOKEN = "CamelGauthAccessToken";
036
037 /**
038 * Name of the Camel header containing an access token secret.
039 */
040 public static final String GAUTH_ACCESS_TOKEN_SECRET = "CamelGauthAccessTokenSecret";
041
042 /**
043 * Default value for access token and access token secret in GoogleOAuthParameters
044 */
045 private static final String EMPTY_TOKEN = "";
046
047 /**
048 * Creates a {@link GoogleOAuthParameters} object from endpoint and
049 * <code>exchange.getIn()</code> data. The created parameter object is used
050 * to upgrade an authorized request token to an access token. If the
051 * {@link GAuthComponent} is configured to use the HMAC_SHA1 signature
052 * method, a request token secret is obtained from a
053 * {@link GAuthTokenSecret#COOKIE_NAME} cookie.
054 *
055 * @param endpoint
056 * @param exchange
057 * @param request
058 * ignored.
059 * @return
060 * @throws GAuthException
061 * if the {@link GAuthComponent} is configured to use the
062 * HMAC_SHA1 signature method but there's no cookie with the
063 * request token secret.
064 */
065 public GoogleOAuthParameters writeRequest(GAuthEndpoint endpoint, Exchange exchange, GoogleOAuthParameters request) throws Exception {
066 request = new GoogleOAuthParameters();
067 request.setOAuthConsumerKey(endpoint.getConsumerKey());
068 request.setOAuthConsumerSecret(endpoint.getConsumerSecret());
069 request.setOAuthToken(exchange.getIn().getHeader("oauth_token", String.class));
070 request.setOAuthVerifier(exchange.getIn().getHeader("oauth_verifier", String.class));
071
072 if (endpoint.getComponent().getKeyLoader() == null) {
073 // HMAC_SHA signature is used for getting an access token.
074 // The required token secret has been previously stored as cookie.
075 String cookie = exchange.getIn().getHeader("Cookie", String.class);
076 GAuthTokenSecret tokenSecret = GAuthTokenSecret.fromCookie(cookie);
077 if (tokenSecret == null) {
078 throw new GAuthException(GAuthTokenSecret.COOKIE_NAME + " cookie doesn't exist");
079 }
080 request.setOAuthTokenSecret(tokenSecret.getValue());
081 }
082 return request;
083 }
084
085 /**
086 * Creates an <code>exchange.getOut()</code> message that containing the
087 * access token and the access token secret in the message header.
088 *
089 * @param endpoint
090 * @param exchange
091 * @param response
092 * @return
093 * @see #GAUTH_ACCESS_TOKEN
094 * @see #GAUTH_ACCESS_TOKEN_SECRET
095 */
096 public Exchange readResponse(GAuthEndpoint endpoint, Exchange exchange, GoogleOAuthParameters response) throws IOException {
097 exchange.getOut().setHeaders(exchange.getIn().getHeaders());
098 exchange.getOut().setHeader(GAUTH_ACCESS_TOKEN, canonicalizeToken(response.getOAuthToken()));
099 exchange.getOut().setHeader(GAUTH_ACCESS_TOKEN_SECRET, canonicalizeToken(response.getOAuthTokenSecret()));
100 return exchange;
101 }
102
103 private static String canonicalizeToken(String token) {
104 if (token == null) {
105 return null;
106 } else if (EMPTY_TOKEN.equals(token)) {
107 return null;
108 } else {
109 return token;
110 }
111 }
112
113 }