001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.oauth;
016    
017    import com.liferay.portal.kernel.oauth.OAuthException;
018    import com.liferay.portal.kernel.oauth.OAuthManager;
019    import com.liferay.portal.kernel.oauth.OAuthRequest;
020    import com.liferay.portal.kernel.oauth.Token;
021    import com.liferay.portal.kernel.oauth.Verifier;
022    
023    import org.scribe.builder.api.Api;
024    import org.scribe.builder.api.DefaultApi10a;
025    import org.scribe.model.OAuthConstants;
026    import org.scribe.oauth.OAuthService;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class OAuthManagerImpl implements OAuthManager {
032    
033            public OAuthManagerImpl(
034                    String key, String secret, final String accessURL,
035                    final String requestURL, String callbackURL, String scope) {
036    
037                    Api api = new DefaultApi10a() {
038    
039                            @Override
040                            public String getAccessTokenEndpoint() {
041                                    return accessURL;
042                            }
043    
044                            @Override
045                            public String getRequestTokenEndpoint() {
046                                    return requestURL;
047                            }
048    
049                    };
050    
051                    if (callbackURL == null) {
052                            callbackURL = OAuthConstants.OUT_OF_BAND;
053                    }
054    
055                    _oAuthService = api.createService(key, secret, callbackURL, scope);
056            }
057    
058            public Token getAccessToken(Token requestToken, Verifier verifier)
059                    throws OAuthException {
060    
061                    try {
062                            return new TokenImpl(
063                                    _oAuthService.getAccessToken(
064                                            (org.scribe.model.Token)requestToken.getWrappedToken(),
065                                            (org.scribe.model.Verifier)verifier.getWrappedVerifier()));
066                    }
067                    catch (Exception e) {
068                            throw new OAuthException(e);
069                    }
070            }
071    
072            public Token getRequestToken() throws OAuthException {
073                    try {
074                            return new TokenImpl(_oAuthService.getRequestToken());
075                    }
076                    catch (Exception e) {
077                            throw new OAuthException(e);
078                    }
079            }
080    
081            public String getVersion() throws OAuthException {
082                    try {
083                            return _oAuthService.getVersion();
084                    }
085                    catch (Exception e) {
086                            throw new OAuthException(e);
087                    }
088            }
089    
090            public void signRequest(Token accessToken, OAuthRequest oAuthRequest)
091                    throws OAuthException {
092    
093                    try {
094                            _oAuthService.signRequest(
095                                    (org.scribe.model.Token)accessToken.getWrappedToken(),
096                                    (org.scribe.model.OAuthRequest)
097                                            oAuthRequest.getWrappedOAuthRequest());
098                    }
099                    catch (Exception e) {
100                            throw new OAuthException(e);
101                    }
102            }
103    
104            private OAuthService _oAuthService;
105    
106    }