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 org.nuxeo.ecm.core.api.DocumentModel;
023
024import net.oauth.OAuthServiceProvider;
025
026public class NuxeoOAuthServiceProvider extends OAuthServiceProvider {
027
028    private static final long serialVersionUID = 1L;
029
030    public static final String SCHEMA = "oauthServiceProvider";
031
032    protected String gadgetUrl;
033
034    protected String serviceName;
035
036    protected String keyType;
037
038    protected String consumerKey;
039
040    protected String consumerSecret;
041
042    protected String publicKey;
043
044    protected String description;
045
046    protected boolean enabled = true;
047
048    protected boolean readOnly = false;
049
050    protected Long id;
051
052    public NuxeoOAuthServiceProvider(String requestTokenURL, String userAuthorizationURL, String accessTokenURL) {
053        super(requestTokenURL, userAuthorizationURL, accessTokenURL);
054    }
055
056    public NuxeoOAuthServiceProvider(Long id, String gadgetUrl, String serviceName, String consumerKey,
057            String consumerSecret, String publicKey) {
058        super(null, null, null);
059        this.id = id;
060        this.gadgetUrl = gadgetUrl;
061        this.serviceName = serviceName;
062        this.consumerKey = consumerKey;
063        this.consumerSecret = consumerSecret;
064        this.publicKey = publicKey;
065        this.readOnly = true;
066    }
067
068    public static NuxeoOAuthServiceProvider createFromDirectoryEntry(DocumentModel entry) {
069
070        String requestTokenURL = (String) entry.getProperty(SCHEMA, "requestTokenURL");
071        String userAuthorizationURL = (String) entry.getProperty(SCHEMA, "userAuthorizationURL");
072        String accessTokenURL = (String) entry.getProperty(SCHEMA, "accessTokenURL");
073
074        NuxeoOAuthServiceProvider provider = new NuxeoOAuthServiceProvider(requestTokenURL, userAuthorizationURL,
075                accessTokenURL);
076
077        provider.consumerKey = (String) entry.getProperty(SCHEMA, "consumerKey");
078        provider.consumerSecret = (String) entry.getProperty(SCHEMA, "consumerSecret");
079        provider.gadgetUrl = (String) entry.getProperty(SCHEMA, "gadgetUrl");
080        provider.id = (Long) entry.getProperty(SCHEMA, "id");
081        provider.keyType = (String) entry.getProperty(SCHEMA, "keyType");
082        provider.publicKey = (String) entry.getProperty(SCHEMA, "publicKey");
083        provider.serviceName = (String) entry.getProperty(SCHEMA, "serviceName");
084        Boolean enabledFlag = (Boolean) entry.getProperty(SCHEMA, "enabled");
085        if (Boolean.FALSE.equals(enabledFlag)) {
086            provider.enabled = false;
087        }
088        return provider;
089    }
090
091    protected DocumentModel asDocumentModel(DocumentModel entry) {
092
093        entry.setProperty(SCHEMA, "gadgetUrl", gadgetUrl);
094        entry.setProperty(SCHEMA, "serviceName", serviceName);
095        entry.setProperty(SCHEMA, "keyType", keyType);
096        entry.setProperty(SCHEMA, "consumerKey", consumerKey);
097        entry.setProperty(SCHEMA, "consumerSecret", consumerSecret);
098        entry.setProperty(SCHEMA, "publicKey", publicKey);
099        entry.setProperty(SCHEMA, "requestTokenURL", requestTokenURL);
100        entry.setProperty(SCHEMA, "accessTokenURL", accessTokenURL);
101        entry.setProperty(SCHEMA, "userAuthorizationURL", userAuthorizationURL);
102        entry.setProperty(SCHEMA, "enabled", Boolean.valueOf(enabled));
103        return entry;
104    }
105
106    public String getGadgetUrl() {
107        return gadgetUrl;
108    }
109
110    public String getServiceName() {
111        return serviceName;
112    }
113
114    public String getKeyType() {
115        return keyType;
116    }
117
118    public String getConsumerKey() {
119        return consumerKey;
120    }
121
122    public String getConsumerSecret() {
123        return consumerSecret;
124    }
125
126    public String getPublicKey() {
127        return publicKey;
128    }
129
130    public Long getId() {
131        return id;
132    }
133
134    public String getRequestTokenUR() {
135        return requestTokenURL;
136    }
137
138    public String getUserAuthorizationURL() {
139        return userAuthorizationURL;
140    }
141
142    public String getAccessTokenURL() {
143        return accessTokenURL;
144    }
145
146    public String getDescription() {
147        return description;
148    }
149
150    public boolean isReadOnly() {
151        return readOnly;
152    }
153}