001/*
002 * (C) Copyright 2006-2013 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 *     Nelson Silva
018 */
019package org.nuxeo.ecm.platform.oauth2.tokens;
020
021import java.util.Calendar;
022import java.util.Date;
023import java.util.HashMap;
024import java.util.Map;
025
026import com.google.api.client.auth.oauth2.StoredCredential;
027import org.apache.commons.lang.RandomStringUtils;
028import org.nuxeo.ecm.core.api.DocumentModel;
029
030public class NuxeoOAuth2Token {
031
032    public static final String SCHEMA = "oauth2Token";
033
034    public static final String KEY_SERVICE_LOGIN = "serviceLogin";
035
036    public static final String KEY_NUXEO_LOGIN = "nuxeoLogin";
037
038    protected Long id;
039
040    protected String serviceName;
041
042    protected String nuxeoLogin;
043
044    protected String accessToken;
045
046    protected String clientId;
047
048    protected Calendar creationDate;
049
050    private String refreshToken;
051
052    private Long expirationTimeMilliseconds;
053
054    private boolean isShared;
055
056    protected String sharedWith;
057
058    protected String serviceLogin;
059
060    public NuxeoOAuth2Token(long expirationTimeMilliseconds, String clientId) {
061        this("", "", expirationTimeMilliseconds);
062        this.clientId = clientId;
063        refresh();
064    }
065
066    public NuxeoOAuth2Token(String accessToken, String refreshToken, Long expirationTimeMilliseconds) {
067        this.accessToken = accessToken;
068        this.refreshToken = refreshToken;
069        this.expirationTimeMilliseconds = expirationTimeMilliseconds;
070        this.creationDate = Calendar.getInstance();
071        this.isShared = false;
072        this.sharedWith = "";
073    }
074
075    public NuxeoOAuth2Token(StoredCredential credential) {
076        this(credential.getAccessToken(), credential.getRefreshToken(), credential.getExpirationTimeMilliseconds());
077    }
078
079    public NuxeoOAuth2Token(DocumentModel entry) {
080        this.id = (Long) entry.getProperty(SCHEMA, "id");
081        this.accessToken = (String) entry.getProperty(SCHEMA, "accessToken");
082        this.refreshToken = (String) entry.getProperty(SCHEMA, "refreshToken");
083        this.expirationTimeMilliseconds = (Long) entry.getProperty(SCHEMA, "expirationTimeMilliseconds");
084        this.serviceName = (String) entry.getProperty(SCHEMA, "serviceName");
085        this.nuxeoLogin = (String) entry.getProperty(SCHEMA, "nuxeoLogin");
086        this.clientId = (String) entry.getProperty(SCHEMA, "clientId");
087        this.creationDate = (Calendar) entry.getProperty(SCHEMA, "creationDate");
088        this.isShared = (Boolean) entry.getProperty(SCHEMA, "isShared");
089        this.sharedWith = (String) entry.getProperty(SCHEMA, "sharedWith");
090        this.serviceLogin = (String) entry.getProperty(SCHEMA, "serviceLogin");
091    }
092
093    public static StoredCredential asCredential(DocumentModel entry) {
094        StoredCredential credential = new StoredCredential();
095        String accessToken = (String) entry.getProperty(SCHEMA, "accessToken");
096        String refreshToken = (String) entry.getProperty(SCHEMA, "refreshToken");
097        Long expirationTimeMilliseconds = (Long) entry.getProperty(SCHEMA, "expirationTimeMilliseconds");
098        credential.setAccessToken(accessToken);
099        credential.setRefreshToken(refreshToken);
100        credential.setExpirationTimeMilliseconds(expirationTimeMilliseconds);
101        return credential;
102    }
103
104    public Map<String, Object> toMap() {
105        Map<String, Object> map = new HashMap<String, Object>();
106        map.put("serviceName", serviceName);
107        map.put("nuxeoLogin", nuxeoLogin);
108        map.put("accessToken", accessToken);
109        map.put("refreshToken", refreshToken);
110        map.put("expirationTimeMilliseconds", expirationTimeMilliseconds);
111        map.put("clientId", clientId);
112        map.put("creationDate", creationDate);
113        map.put("isShared", isShared);
114        map.put("sharedWith", sharedWith);
115        map.put("serviceLogin", serviceLogin);
116        return map;
117    }
118
119    public Map<String, Object> toJsonObject() {
120        Map<String, Object> m = new HashMap<>();
121        m.put("access_token", accessToken);
122        m.put("refresh_token", refreshToken);
123        m.put("token_type", "bearer");
124        m.put("expires_in",
125                Math.floor((creationDate.getTimeInMillis() + expirationTimeMilliseconds - new Date().getTime()) / 1000));
126        return m;
127    }
128
129    public void updateEntry(DocumentModel entry) {
130        entry.setProperty(SCHEMA, "serviceName", this.serviceName);
131        entry.setProperty(SCHEMA, "nuxeoLogin", this.nuxeoLogin);
132        entry.setProperty(SCHEMA, "accessToken", this.accessToken);
133        entry.setProperty(SCHEMA, "refreshToken", this.refreshToken);
134        entry.setProperty(SCHEMA, "expirationTimeMilliseconds", this.expirationTimeMilliseconds);
135        entry.setProperty(SCHEMA, "clientId", this.clientId);
136        entry.setProperty(SCHEMA, "isShared", this.isShared);
137        entry.setProperty(SCHEMA, "sharedWith", this.sharedWith);
138        entry.setProperty(SCHEMA, "serviceLogin", this.serviceLogin);
139    }
140
141    public void refresh() {
142        accessToken = RandomStringUtils.random(32, true, true);
143        refreshToken = RandomStringUtils.random(64, true, true);
144        creationDate = Calendar.getInstance();
145    }
146
147    public boolean isExpired() {
148        return creationDate != null
149                && creationDate.getTimeInMillis() + expirationTimeMilliseconds < Calendar.getInstance().getTimeInMillis();
150    }
151
152    public void setServiceName(String serviceName) {
153        this.serviceName = serviceName;
154    }
155
156    public void setNuxeoLogin(String userId) {
157        this.nuxeoLogin = userId;
158    }
159
160    public String getNuxeoLogin() {
161        return nuxeoLogin;
162    }
163
164    public String getAccessToken() {
165        return accessToken;
166    }
167
168    public void setAccessToken(String accessToken) {
169        this.accessToken = accessToken;
170    }
171
172    public String getRefreshToken() {
173        return refreshToken;
174    }
175
176    public void setRefreshToken(String refreshToken) {
177        this.refreshToken = refreshToken;
178    }
179
180    public Long getExpirationTimeMilliseconds() {
181        return expirationTimeMilliseconds;
182    }
183
184    public void setExpirationTimeMilliseconds(Long expirationTimeMilliseconds) {
185        this.expirationTimeMilliseconds = expirationTimeMilliseconds;
186    }
187
188    public String getServiceName() {
189        return serviceName;
190    }
191
192    public String getClientId() {
193        return clientId;
194    }
195
196    public void setClientId(String clientId) {
197        this.clientId = clientId;
198    }
199
200    public boolean isShared() {
201        return isShared;
202    }
203
204    public void setIsShared(boolean isShared) {
205        this.isShared = isShared;
206    }
207
208    public String getSharedWith() {
209        return sharedWith;
210    }
211
212    public void setSharedWith(String sharedWith) {
213        this.sharedWith = sharedWith;
214    }
215
216    public String getServiceLogin() {
217        return serviceLogin;
218    }
219
220    public void setServiceLogin(String serviceLogin) {
221        this.serviceLogin = serviceLogin;
222    }
223
224    public Calendar getCreationDate() {
225        return creationDate;
226    }
227
228    public void setCreationDate(Calendar creationDate) {
229        this.creationDate = creationDate;
230    }
231
232    public Long getId() {
233        return id;
234    }
235
236}