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