PageRenderTime 20ms CodeModel.GetById 8ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/src/main/java/com/google/ie/common/openid/OAuthConsumerUtil.java

http://thoughtsite.googlecode.com/
Java | 73 lines | 41 code | 13 blank | 19 comment | 0 complexity | a7fbf2879f8792bab052c43b9b795dbe MD5 | raw file
 1/* Copyright 2010 Google Inc.
 2 * 
 3 * Licensed under the Apache License, Version 2.0 (the "License");
 4 * you may not use this file except in compliance with the License.
 5 * You may obtain a copy of the License at
 6 * 
 7 *      http://www.apache.org/licenses/LICENSE-2.0
 8 * 
 9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS.
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License
14 */
15
16package com.google.ie.common.openid;
17
18import com.google.inject.Inject;
19import com.google.step2.Step2OAuthClient;
20
21import org.apache.log4j.Logger;
22
23import net.oauth.OAuth;
24import net.oauth.OAuthAccessor;
25import net.oauth.OAuthException;
26import net.oauth.OAuthMessage;
27
28import java.io.IOException;
29import java.net.URISyntaxException;
30
31/**
32 * Utility class for requesting OAuth tokens from accessors
33 * 
34 * @author sweis@google.com (Steve Weis)
35 */
36public class OAuthConsumerUtil {
37
38    private Logger log = Logger.getLogger(OAuthConsumerUtil.class);
39    private final Step2OAuthClient client;
40
41    @Inject
42    public OAuthConsumerUtil(Step2OAuthClient client) {
43        this.client = client;
44    }
45
46    public OAuthAccessor getRequestToken(OAuthAccessor accessor)
47                    throws IOException, OAuthException, URISyntaxException {
48        OAuthAccessor accessorCopy = new OAuthAccessor(accessor.consumer);
49
50        OAuthMessage response = client.invoke(accessor,
51                        accessor.consumer.serviceProvider.requestTokenURL,
52                        OAuth.newList("scope", accessor.getProperty("scope").toString()));
53        log.info("Successfully got OAuth request token");
54        accessorCopy.requestToken = response.getParameter("oauth_token");
55        accessorCopy.tokenSecret = response.getParameter("oauth_token_secret");
56        return accessor;
57    }
58
59    public OAuthAccessor getAccessToken(OAuthAccessor accessor)
60                    throws IOException, OAuthException, URISyntaxException {
61        OAuthAccessor accessorCopy = new OAuthAccessor(accessor.consumer);
62
63        OAuthMessage response = client.invoke(accessor,
64                        accessor.consumer.serviceProvider.accessTokenURL,
65                        OAuth.newList("oauth_token", accessor.requestToken,
66                        "scope", accessor.getProperty("scope").toString()));
67        log.info("Successfully got OAuth access token");
68        accessorCopy.accessToken = response.getParameter("oauth_token");
69        accessorCopy.tokenSecret = response.getParameter("oauth_token_secret");
70        return accessorCopy;
71    }
72}
73