001/*
002 * (C) Copyright 2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 */
017
018package org.nuxeo.ecm.platform.oauth.providers;
019
020import net.oauth.OAuthServiceProvider;
021
022import org.nuxeo.ecm.core.api.DocumentModel;
023
024public class NuxeoOAuthServiceProvider extends OAuthServiceProvider {
025
026    public static final String SCHEMA = "oauthServiceProvider";
027
028    protected String gadgetUrl;
029
030    protected String serviceName;
031
032    protected String keyType;
033
034    protected String consumerKey;
035
036    protected String consumerSecret;
037
038    protected String publicKey;
039
040    protected String description;
041
042    protected boolean enabled = true;
043
044    protected boolean readOnly = false;
045
046    protected Long id;
047
048    public NuxeoOAuthServiceProvider(String requestTokenURL, String userAuthorizationURL, String accessTokenURL) {
049        super(requestTokenURL, userAuthorizationURL, accessTokenURL);
050    }
051
052    public NuxeoOAuthServiceProvider(Long id, String gadgetUrl, String serviceName, String consumerKey,
053            String consumerSecret, String publicKey) {
054        super(null, null, null);
055        this.id = id;
056        this.gadgetUrl = gadgetUrl;
057        this.serviceName = serviceName;
058        this.consumerKey = consumerKey;
059        this.consumerSecret = consumerSecret;
060        this.publicKey = publicKey;
061        this.readOnly = true;
062    }
063
064    public static NuxeoOAuthServiceProvider createFromDirectoryEntry(DocumentModel entry) {
065
066        String requestTokenURL = (String) entry.getProperty(SCHEMA, "requestTokenURL");
067        String userAuthorizationURL = (String) entry.getProperty(SCHEMA, "userAuthorizationURL");
068        String accessTokenURL = (String) entry.getProperty(SCHEMA, "accessTokenURL");
069
070        NuxeoOAuthServiceProvider provider = new NuxeoOAuthServiceProvider(requestTokenURL, userAuthorizationURL,
071                accessTokenURL);
072
073        provider.consumerKey = (String) entry.getProperty(SCHEMA, "consumerKey");
074        provider.consumerSecret = (String) entry.getProperty(SCHEMA, "consumerSecret");
075        provider.gadgetUrl = (String) entry.getProperty(SCHEMA, "gadgetUrl");
076        provider.id = (Long) entry.getProperty(SCHEMA, "id");
077        provider.keyType = (String) entry.getProperty(SCHEMA, "keyType");
078        provider.publicKey = (String) entry.getProperty(SCHEMA, "publicKey");
079        provider.serviceName = (String) entry.getProperty(SCHEMA, "serviceName");
080        Boolean enabledFlag = (Boolean) entry.getProperty(SCHEMA, "enabled");
081        if (Boolean.FALSE.equals(enabledFlag)) {
082            provider.enabled = false;
083        }
084        return provider;
085    }
086
087    protected DocumentModel asDocumentModel(DocumentModel entry) {
088
089        entry.setProperty(SCHEMA, "gadgetUrl", gadgetUrl);
090        entry.setProperty(SCHEMA, "serviceName", serviceName);
091        entry.setProperty(SCHEMA, "keyType", keyType);
092        entry.setProperty(SCHEMA, "consumerKey", consumerKey);
093        entry.setProperty(SCHEMA, "consumerSecret", consumerSecret);
094        entry.setProperty(SCHEMA, "publicKey", publicKey);
095        entry.setProperty(SCHEMA, "requestTokenURL", requestTokenURL);
096        entry.setProperty(SCHEMA, "accessTokenURL", accessTokenURL);
097        entry.setProperty(SCHEMA, "userAuthorizationURL", userAuthorizationURL);
098        entry.setProperty(SCHEMA, "enabled", Boolean.valueOf(enabled));
099        return entry;
100    }
101
102    public String getGadgetUrl() {
103        return gadgetUrl;
104    }
105
106    public String getServiceName() {
107        return serviceName;
108    }
109
110    public String getKeyType() {
111        return keyType;
112    }
113
114    public String getConsumerKey() {
115        return consumerKey;
116    }
117
118    public String getConsumerSecret() {
119        return consumerSecret;
120    }
121
122    public String getPublicKey() {
123        return publicKey;
124    }
125
126    public Long getId() {
127        return id;
128    }
129
130    public String getRequestTokenUR() {
131        return requestTokenURL;
132    }
133
134    public String getUserAuthorizationURL() {
135        return userAuthorizationURL;
136    }
137
138    public String getAccessTokenURL() {
139        return accessTokenURL;
140    }
141
142    public String getDescription() {
143        return description;
144    }
145
146    public boolean isReadOnly() {
147        return readOnly;
148    }
149}