001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 */
019
020package org.nuxeo.ecm.platform.oauth.providers;
021
022import net.oauth.OAuthServiceProvider;
023
024import org.nuxeo.ecm.core.api.DocumentModel;
025
026public class NuxeoOAuthServiceProvider extends OAuthServiceProvider {
027
028    public static final String SCHEMA = "oauthServiceProvider";
029
030    protected String gadgetUrl;
031
032    protected String serviceName;
033
034    protected String keyType;
035
036    protected String consumerKey;
037
038    protected String consumerSecret;
039
040    protected String publicKey;
041
042    protected String description;
043
044    protected boolean enabled = true;
045
046    protected boolean readOnly = false;
047
048    protected Long id;
049
050    public NuxeoOAuthServiceProvider(String requestTokenURL, String userAuthorizationURL, String accessTokenURL) {
051        super(requestTokenURL, userAuthorizationURL, accessTokenURL);
052    }
053
054    public NuxeoOAuthServiceProvider(Long id, String gadgetUrl, String serviceName, String consumerKey,
055            String consumerSecret, String publicKey) {
056        super(null, null, null);
057        this.id = id;
058        this.gadgetUrl = gadgetUrl;
059        this.serviceName = serviceName;
060        this.consumerKey = consumerKey;
061        this.consumerSecret = consumerSecret;
062        this.publicKey = publicKey;
063        this.readOnly = true;
064    }
065
066    public static NuxeoOAuthServiceProvider createFromDirectoryEntry(DocumentModel entry) {
067
068        String requestTokenURL = (String) entry.getProperty(SCHEMA, "requestTokenURL");
069        String userAuthorizationURL = (String) entry.getProperty(SCHEMA, "userAuthorizationURL");
070        String accessTokenURL = (String) entry.getProperty(SCHEMA, "accessTokenURL");
071
072        NuxeoOAuthServiceProvider provider = new NuxeoOAuthServiceProvider(requestTokenURL, userAuthorizationURL,
073                accessTokenURL);
074
075        provider.consumerKey = (String) entry.getProperty(SCHEMA, "consumerKey");
076        provider.consumerSecret = (String) entry.getProperty(SCHEMA, "consumerSecret");
077        provider.gadgetUrl = (String) entry.getProperty(SCHEMA, "gadgetUrl");
078        provider.id = (Long) entry.getProperty(SCHEMA, "id");
079        provider.keyType = (String) entry.getProperty(SCHEMA, "keyType");
080        provider.publicKey = (String) entry.getProperty(SCHEMA, "publicKey");
081        provider.serviceName = (String) entry.getProperty(SCHEMA, "serviceName");
082        Boolean enabledFlag = (Boolean) entry.getProperty(SCHEMA, "enabled");
083        if (Boolean.FALSE.equals(enabledFlag)) {
084            provider.enabled = false;
085        }
086        return provider;
087    }
088
089    protected DocumentModel asDocumentModel(DocumentModel entry) {
090
091        entry.setProperty(SCHEMA, "gadgetUrl", gadgetUrl);
092        entry.setProperty(SCHEMA, "serviceName", serviceName);
093        entry.setProperty(SCHEMA, "keyType", keyType);
094        entry.setProperty(SCHEMA, "consumerKey", consumerKey);
095        entry.setProperty(SCHEMA, "consumerSecret", consumerSecret);
096        entry.setProperty(SCHEMA, "publicKey", publicKey);
097        entry.setProperty(SCHEMA, "requestTokenURL", requestTokenURL);
098        entry.setProperty(SCHEMA, "accessTokenURL", accessTokenURL);
099        entry.setProperty(SCHEMA, "userAuthorizationURL", userAuthorizationURL);
100        entry.setProperty(SCHEMA, "enabled", Boolean.valueOf(enabled));
101        return entry;
102    }
103
104    public String getGadgetUrl() {
105        return gadgetUrl;
106    }
107
108    public String getServiceName() {
109        return serviceName;
110    }
111
112    public String getKeyType() {
113        return keyType;
114    }
115
116    public String getConsumerKey() {
117        return consumerKey;
118    }
119
120    public String getConsumerSecret() {
121        return consumerSecret;
122    }
123
124    public String getPublicKey() {
125        return publicKey;
126    }
127
128    public Long getId() {
129        return id;
130    }
131
132    public String getRequestTokenUR() {
133        return requestTokenURL;
134    }
135
136    public String getUserAuthorizationURL() {
137        return userAuthorizationURL;
138    }
139
140    public String getAccessTokenURL() {
141        return accessTokenURL;
142    }
143
144    public String getDescription() {
145        return description;
146    }
147
148    public boolean isReadOnly() {
149        return readOnly;
150    }
151}