001    /**
002     * Copyright (c) 2000-2013 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            @Override
059            public Token getAccessToken(Token requestToken, Verifier verifier)
060                    throws OAuthException {
061    
062                    try {
063                            return new TokenImpl(
064                                    _oAuthService.getAccessToken(
065                                            (org.scribe.model.Token)requestToken.getWrappedToken(),
066                                            (org.scribe.model.Verifier)verifier.getWrappedVerifier()));
067                    }
068                    catch (Exception e) {
069                            throw new OAuthException(e);
070                    }
071            }
072    
073            @Override
074            public Token getRequestToken() throws OAuthException {
075                    try {
076                            return new TokenImpl(_oAuthService.getRequestToken());
077                    }
078                    catch (Exception e) {
079                            throw new OAuthException(e);
080                    }
081            }
082    
083            @Override
084            public String getVersion() throws OAuthException {
085                    try {
086                            return _oAuthService.getVersion();
087                    }
088                    catch (Exception e) {
089                            throw new OAuthException(e);
090                    }
091            }
092    
093            @Override
094            public void signRequest(Token accessToken, OAuthRequest oAuthRequest)
095                    throws OAuthException {
096    
097                    try {
098                            _oAuthService.signRequest(
099                                    (org.scribe.model.Token)accessToken.getWrappedToken(),
100                                    (org.scribe.model.OAuthRequest)
101                                            oAuthRequest.getWrappedOAuthRequest());
102                    }
103                    catch (Exception e) {
104                            throw new OAuthException(e);
105                    }
106            }
107    
108            private OAuthService _oAuthService;
109    
110    }