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 serviceLogin;
057
058    public NuxeoOAuth2Token(long expirationTimeMilliseconds, String clientId) {
059        this("", "", expirationTimeMilliseconds);
060        this.clientId = clientId;
061        refresh();
062    }
063
064    public NuxeoOAuth2Token(String accessToken, String refreshToken, Long expirationTimeMilliseconds) {
065        this.accessToken = accessToken;
066        this.refreshToken = refreshToken;
067        this.expirationTimeMilliseconds = expirationTimeMilliseconds;
068        this.creationDate = Calendar.getInstance();
069        this.isShared = false;
070    }
071
072    public NuxeoOAuth2Token(StoredCredential credential) {
073        this(credential.getAccessToken(), credential.getRefreshToken(), credential.getExpirationTimeMilliseconds());
074    }
075
076    public NuxeoOAuth2Token(DocumentModel entry) {
077        this.id = (Long) entry.getProperty(SCHEMA, "id");
078        this.accessToken = (String) entry.getProperty(SCHEMA, "accessToken");
079        this.refreshToken = (String) entry.getProperty(SCHEMA, "refreshToken");
080        this.expirationTimeMilliseconds = (Long) entry.getProperty(SCHEMA, "expirationTimeMilliseconds");
081        this.serviceName = (String) entry.getProperty(SCHEMA, "serviceName");
082        this.nuxeoLogin = (String) entry.getProperty(SCHEMA, "nuxeoLogin");
083        this.clientId = (String) entry.getProperty(SCHEMA, "clientId");
084        this.creationDate = (Calendar) entry.getProperty(SCHEMA, "creationDate");
085        this.isShared = (Boolean) entry.getProperty(SCHEMA, "isShared");
086        this.serviceLogin = (String) entry.getProperty(SCHEMA, "serviceLogin");
087    }
088
089    public static StoredCredential asCredential(DocumentModel entry) {
090        StoredCredential credential = new StoredCredential();
091        String accessToken = (String) entry.getProperty(SCHEMA, "accessToken");
092        String refreshToken = (String) entry.getProperty(SCHEMA, "refreshToken");
093        Long expirationTimeMilliseconds = (Long) entry.getProperty(SCHEMA, "expirationTimeMilliseconds");
094        credential.setAccessToken(accessToken);
095        credential.setRefreshToken(refreshToken);
096        credential.setExpirationTimeMilliseconds(expirationTimeMilliseconds);
097        return credential;
098    }
099
100    public Map<String, Object> toMap() {
101        Map<String, Object> map = new HashMap<String, Object>();
102        map.put("serviceName", serviceName);
103        map.put("nuxeoLogin", nuxeoLogin);
104        map.put("accessToken", accessToken);
105        map.put("refreshToken", refreshToken);
106        map.put("expirationTimeMilliseconds", expirationTimeMilliseconds);
107        map.put("clientId", clientId);
108        map.put("creationDate", creationDate);
109        map.put("isShared", isShared);
110        map.put("serviceLogin", serviceLogin);
111        return map;
112    }
113
114    public Map<String, Object> toJsonObject() {
115        Map<String, Object> m = new HashMap<>();
116        m.put("access_token", accessToken);
117        m.put("refresh_token", refreshToken);
118        m.put("token_type", "bearer");
119        m.put("expires_in",
120                Math.floor((creationDate.getTimeInMillis() + expirationTimeMilliseconds - new Date().getTime()) / 1000));
121        return m;
122    }
123
124    public void updateEntry(DocumentModel entry) {
125        entry.setProperty(SCHEMA, "serviceName", this.serviceName);
126        entry.setProperty(SCHEMA, "nuxeoLogin", this.nuxeoLogin);
127        entry.setProperty(SCHEMA, "accessToken", this.accessToken);
128        entry.setProperty(SCHEMA, "refreshToken", this.refreshToken);
129        entry.setProperty(SCHEMA, "expirationTimeMilliseconds", this.expirationTimeMilliseconds);
130        entry.setProperty(SCHEMA, "clientId", this.clientId);
131        entry.setProperty(SCHEMA, "isShared", this.isShared);
132        entry.setProperty(SCHEMA, "serviceLogin", this.serviceLogin);
133    }
134
135    public void refresh() {
136        accessToken = RandomStringUtils.random(32, true, true);
137        refreshToken = RandomStringUtils.random(64, true, true);
138        creationDate = Calendar.getInstance();
139    }
140
141    public boolean isExpired() {
142        return creationDate != null
143                && creationDate.getTimeInMillis() + expirationTimeMilliseconds < Calendar.getInstance().getTimeInMillis();
144    }
145
146    public void setServiceName(String serviceName) {
147        this.serviceName = serviceName;
148    }
149
150    public void setNuxeoLogin(String userId) {
151        this.nuxeoLogin = userId;
152    }
153
154    public String getNuxeoLogin() {
155        return nuxeoLogin;
156    }
157
158    public String getAccessToken() {
159        return accessToken;
160    }
161
162    public void setAccessToken(String accessToken) {
163        this.accessToken = accessToken;
164    }
165
166    public String getRefreshToken() {
167        return refreshToken;
168    }
169
170    public void setRefreshToken(String refreshToken) {
171        this.refreshToken = refreshToken;
172    }
173
174    public Long getExpirationTimeMilliseconds() {
175        return expirationTimeMilliseconds;
176    }
177
178    public void setExpirationTimeMilliseconds(Long expirationTimeMilliseconds) {
179        this.expirationTimeMilliseconds = expirationTimeMilliseconds;
180    }
181
182    public String getServiceName() {
183        return serviceName;
184    }
185
186    public String getClientId() {
187        return clientId;
188    }
189
190    public void setClientId(String clientId) {
191        this.clientId = clientId;
192    }
193
194    public boolean isShared() {
195        return isShared;
196    }
197
198    public void setIsShared(boolean isShared) {
199        this.isShared = isShared;
200    }
201
202    public String getServiceLogin() {
203        return serviceLogin;
204    }
205
206    public void setServiceLogin(String serviceLogin) {
207        this.serviceLogin = serviceLogin;
208    }
209
210    public Calendar getCreationDate() {
211        return creationDate;
212    }
213
214    public void setCreationDate(Calendar creationDate) {
215        this.creationDate = creationDate;
216    }
217}