001/* 002 * Copyright 2020 Anyware Services 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 017package org.ametys.plugins.workspaces.documents.onlyoffice; 018 019import java.util.HashMap; 020import java.util.Map; 021import java.util.Set; 022 023import org.apache.avalon.framework.component.Component; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.avalon.framework.service.Serviceable; 027import org.apache.commons.collections4.SetUtils; 028import org.apache.commons.lang3.StringUtils; 029 030import org.ametys.core.authentication.token.AuthenticationTokenManager; 031import org.ametys.core.ui.Callable; 032import org.ametys.core.user.CurrentUserProvider; 033import org.ametys.runtime.config.Config; 034import org.ametys.runtime.plugin.component.AbstractLogEnabled; 035 036import com.auth0.jwt.JWT; 037import com.auth0.jwt.algorithms.Algorithm; 038 039/** 040 * Main helper for OnlyOffice 041 */ 042public class OnlyOfficeManager extends AbstractLogEnabled implements Component, Serviceable 043{ 044 /** The Avalon role */ 045 public static final String ROLE = OnlyOfficeManager.class.getName(); 046 047 private AuthenticationTokenManager _tokenManager; 048 private CurrentUserProvider _currentUserProvider; 049 050 @Override 051 public void service(ServiceManager manager) throws ServiceException 052 { 053 _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE); 054 _tokenManager = (AuthenticationTokenManager) manager.lookup(AuthenticationTokenManager.ROLE); 055 } 056 057 /** 058 * Generate a token for OnlyOffice use 059 * @param fileId id of the resource that will be used by OnlyOffice 060 * @return a map containing tokenReponse : the token to use 061 */ 062 @Callable 063 public Map<String, Object> generateNewToken(String fileId) 064 { 065 Set<String> contexts = SetUtils.unmodifiableSet(fileId); 066 067 String reponse = _tokenManager.generateToken(_currentUserProvider.getUser(), 30000, true, null, contexts, "onlyOfficeReponse", null); 068 069 Map<String, Object> result = new HashMap<>(); 070 result.put("tokenReponse", reponse); 071 072 return result; 073 } 074 075 /** 076 * Sign a json configuration for OnlyOffice using a secret parametrized key 077 * @param toSign The json to sign 078 * @return The signed json 079 */ 080 @Callable 081 public Map<String, Object> signConfiguration(String toSign) 082 { 083 String secret = Config.getInstance().getValue("project.onlyoffice.secret"); 084 085 Map<String, Object> result = new HashMap<>(); 086 087 if (StringUtils.isNotBlank(secret)) 088 { 089 Algorithm algorithm = Algorithm.HMAC256(secret); 090 String token = JWT.create() 091 .withIssuer(toSign) 092 .sign(algorithm); 093 result.put("signature", token); 094 } 095 096 result.put("signed", "true"); 097 return result; 098 } 099}