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