001/*
002 * (C) Copyright 2006-2008 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 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.oauth.tokens;
021
022import java.util.Calendar;
023
024import org.nuxeo.ecm.core.api.DocumentModel;
025
026/**
027 * Implementation of the {@link OAuthToken} interface. Provides mapping features to DocumentModel so that Token can be
028 * stored in a SQL Directory
029 *
030 * @author tiry
031 */
032public class NuxeoOAuthToken implements OAuthToken {
033
034    public static final String SCHEMA = "oauthToken";
035
036    protected String appId;
037
038    protected String callbackUrl;
039
040    protected String nuxeoLogin;
041
042    protected String token;
043
044    protected String tokenSecret;
045
046    protected String consumerKey;
047
048    protected Type type;
049
050    protected Calendar creationDate;
051
052    protected String verifier;
053
054    protected long durationInMinutes;
055
056    protected boolean clientToken = false;
057
058    protected String clientId;
059
060    public NuxeoOAuthToken(String consumerKey, String callBack) {
061        this.appId = consumerKey;
062        this.consumerKey = consumerKey;
063        this.callbackUrl = callBack;
064        this.creationDate = Calendar.getInstance();
065    }
066
067    public NuxeoOAuthToken(NuxeoOAuthToken originalToken) {
068        this.appId = originalToken.appId;
069        this.callbackUrl = originalToken.callbackUrl;
070        this.nuxeoLogin = originalToken.nuxeoLogin;
071        this.token = originalToken.token;
072        this.tokenSecret = originalToken.tokenSecret;
073        this.consumerKey = originalToken.consumerKey;
074        this.type = originalToken.type;
075        this.verifier = originalToken.verifier;
076        this.durationInMinutes = originalToken.durationInMinutes;
077        this.creationDate = Calendar.getInstance();
078    }
079
080    public NuxeoOAuthToken(DocumentModel entry) {
081        this.appId = (String) entry.getProperty(SCHEMA, "appId");
082        this.callbackUrl = (String) entry.getProperty(SCHEMA, "callbackUrl");
083        this.nuxeoLogin = (String) entry.getProperty(SCHEMA, "nuxeoLogin");
084        this.token = (String) entry.getProperty(SCHEMA, "token");
085        this.tokenSecret = (String) entry.getProperty(SCHEMA, "tokenSecret");
086        this.consumerKey = (String) entry.getProperty(SCHEMA, "consumerKey");
087        this.type = OAuthToken.Type.ACCESS;
088        this.verifier = (String) entry.getProperty(SCHEMA, "verifier");
089        this.durationInMinutes = (Long) entry.getProperty(SCHEMA, "durationInMinutes");
090        this.creationDate = (Calendar) entry.getProperty(SCHEMA, "creationDate");
091
092        Long clientTokenL = (Long) entry.getProperty(SCHEMA, "clientToken");
093        if (clientTokenL != null && clientTokenL.equals(1)) {
094            this.clientToken = true;
095        }
096        this.clientId = (String) entry.getProperty(SCHEMA, "clientId");
097    }
098
099    public void updateEntry(DocumentModel entry) {
100        entry.setProperty(SCHEMA, "appId", this.appId);
101        entry.setProperty(SCHEMA, "callbackUrl", this.callbackUrl);
102        entry.setProperty(SCHEMA, "nuxeoLogin", this.nuxeoLogin);
103        entry.setProperty(SCHEMA, "tokenSecret", this.tokenSecret);
104        entry.setProperty(SCHEMA, "consumerKey", this.consumerKey);
105        entry.setProperty(SCHEMA, "verifier", this.verifier);
106        entry.setProperty(SCHEMA, "durationInMinutes", this.durationInMinutes);
107        entry.setProperty(SCHEMA, "creationDate", this.creationDate);
108
109        entry.setProperty(SCHEMA, "clientId", this.clientId);
110        if (this.clientToken) {
111            entry.setProperty(SCHEMA, "clientToken", 1);
112        } else {
113            entry.setProperty(SCHEMA, "clientToken", 0);
114        }
115    }
116
117    public String getAppId() {
118        return appId;
119    }
120
121    public String getCallbackUrl() {
122        return callbackUrl;
123    }
124
125    public String getNuxeoLogin() {
126        return nuxeoLogin;
127    }
128
129    public String getToken() {
130        return token;
131    }
132
133    public String getTokenSecret() {
134        return tokenSecret;
135    }
136
137    public String getConsumerKey() {
138        return consumerKey;
139    }
140
141    public Type getType() {
142        return type;
143    }
144
145    public Calendar getCreationDate() {
146        return creationDate;
147    }
148
149    @Override
150    public String getValue(String keyName) {
151
152        return null;
153    }
154
155    @Override
156    public void setValue(String keyName, String value) {
157
158    }
159
160    public String getVerifier() {
161        return verifier;
162    }
163
164    public boolean isExpired() {
165        // XXX
166        return false;
167    }
168
169    public void setNuxeoLogin(String login) {
170        nuxeoLogin = login;
171    }
172
173    public boolean isClientToken() {
174        return clientToken;
175    }
176
177    public String getClientId() {
178        return clientId;
179    }
180
181}