Commit 4991351e authored by Matija Obreza's avatar Matija Obreza

Merge branch 'oauth2-jwt-tokenstore-cache' into 'master'

Oauth2 jwt tokenstore cache

See merge request genesys-pgr/genesys-server!413
parents 3677912b af51c33c
......@@ -21,13 +21,11 @@ import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.genesys.amphibian.client.model.Document;
import org.genesys.amphibian.client.model.Preview;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.service.RepositoryService;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.service.AmphibianService;
import org.genesys2.server.service.AmphibianService.AmphibianException;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -84,7 +82,7 @@ public class AmphibianController {
@GetMapping(path = { "/preview/{uuid:\\w{8}\\-\\w{4}\\-.{22}}/{sheet}/{startRow}" })
@ApiOperation(value = "Get the overview of the parsed dataset", notes = "Use the same reference UUID as provided when ingesting a dataset")
public List<Document> getData(@ApiParam(value = "Your reference UUID", required = true) @PathVariable UUID uuid, // uuid
public List<Object> getData(@ApiParam(value = "Your reference UUID", required = true) @PathVariable UUID uuid, // uuid
@ApiParam(value = "Sheet index", required = true) @PathVariable long sheet, // sheet index
@ApiParam(value = "Index of the first row", required = true) @PathVariable long startRow, // start row
@ApiParam(value = "Number of rows to return", required = false) @RequestParam(name = "count", required = false, defaultValue = "50") Optional<Integer> count)
......@@ -92,12 +90,7 @@ public class AmphibianController {
repositoryService.getFile(uuid);
List<Document> data = amphibianService.getPreviewData(uuid, sheet, startRow, count.orElse(50));
if (data == null || data.isEmpty()) {
throw new NotFoundElement("No data in this dataset for sheet=" + sheet + " startRow=" + startRow);
} else {
return data;
}
return amphibianService.getPreviewData(uuid, sheet, startRow, count.orElse(50));
}
}
......@@ -19,7 +19,6 @@ import java.io.IOException;
import java.util.List;
import java.util.UUID;
import org.genesys.amphibian.client.model.Document;
import org.genesys.amphibian.client.model.Preview;
import org.genesys2.server.exception.InvalidApiUsageException;
......@@ -79,6 +78,6 @@ public interface AmphibianService {
* @return the preview data
* @throws AmphibianException
*/
List<Document> getPreviewData(UUID uuid, long sheet, long startRow, int count) throws AmphibianException;
List<Object> getPreviewData(UUID uuid, long sheet, long startRow, int count) throws AmphibianException;
}
......@@ -26,7 +26,6 @@ import java.util.UUID;
import org.genesys.amphibian.client.api.InfoApi;
import org.genesys.amphibian.client.api.PreviewApi;
import org.genesys.amphibian.client.invoker.ApiClient;
import org.genesys.amphibian.client.model.Document;
import org.genesys.amphibian.client.model.Preview;
import org.genesys2.server.service.AmphibianService;
import org.slf4j.Logger;
......@@ -35,6 +34,8 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.google.common.collect.Lists;
@Service
public class AmphibianServiceImpl implements AmphibianService, InitializingBean {
public static final Logger LOG = LoggerFactory.getLogger(AmphibianServiceImpl.class);
......@@ -52,7 +53,7 @@ public class AmphibianServiceImpl implements AmphibianService, InitializingBean
previewApi = new PreviewApi(amphibianClient);
try {
LOG.info("Amphibian {} at {}", infoApi.versionUsingGET(), amphibianClient.getBasePath());
LOG.info("Amphibian {} at {}", infoApi.getVersion(), amphibianClient.getBasePath());
} catch (Throwable e) {
LOG.warn("Amphibian at {} not reachable: {}", amphibianClient.getBasePath(), e.getMessage());
}
......@@ -79,7 +80,7 @@ public class AmphibianServiceImpl implements AmphibianService, InitializingBean
}
try {
return previewApi.ingestUsingPOST(uuid, localFile, null, null, contentType);
return previewApi.ingest(uuid, localFile, contentType, null, null);
} catch (Throwable e) {
throw new AmphibianException("Error ingesting file", e);
} finally {
......@@ -92,16 +93,16 @@ public class AmphibianServiceImpl implements AmphibianService, InitializingBean
if (amphibianClient == null) {
throw new AmphibianNotAvailableException();
}
return previewApi.getUsingGET(uuid);
return previewApi.getPreview(uuid);
}
@Override
public List<Document> getPreviewData(UUID uuid, long sheet, long startRow, int count) throws AmphibianException {
public List<Object> getPreviewData(UUID uuid, long sheet, long startRow, int count) throws AmphibianException {
if (amphibianClient == null) {
throw new AmphibianNotAvailableException();
}
try {
return previewApi.getDataUsingGET(sheet, startRow, uuid, count);
return previewApi.getData(uuid, sheet, startRow, count, Lists.newArrayList());
} catch (Throwable e) {
throw new AmphibianException("Error getting preview data", e);
}
......
/*
* Copyright 2019 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys2.spring;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.code.RandomValueAuthorizationCodeServices;
/**
* An InMemoryAuthorizationCodeServices implementation using the cache manager ensures
* codes are shared between instances.
*
* @author Matija Obreza
*/
public class CachedInMemoryAuthorizationCodeServices extends RandomValueAuthorizationCodeServices implements InitializingBean {
@Autowired
private CacheManager cacheManager;
private String authorizationCodeCacheName = "oauth2.authorizationCodes";
private Cache authorizationCodeCache;
public void setAuthorizationCodeCacheName(String authorizationCodeCacheName) {
this.authorizationCodeCacheName = authorizationCodeCacheName;
}
@Override
public void afterPropertiesSet() throws Exception {
authorizationCodeCache = cacheManager.getCache(authorizationCodeCacheName);
if (authorizationCodeCache == null) {
throw new Exception("Could not obtain cache " + authorizationCodeCacheName);
}
}
@Override
protected void store(String code, OAuth2Authentication authentication) {
authorizationCodeCache.put(code, authentication);
}
@Override
protected OAuth2Authentication remove(String code) {
return authorizationCodeCache.get(code, OAuth2Authentication.class);
}
}
......@@ -19,6 +19,7 @@ import java.util.Arrays;
import org.genesys.blocks.oauth.service.OAuthServiceImpl;
import org.genesys.blocks.security.component.OAuthClientOriginCheckFilter;
import org.genesys2.spring.CachedInMemoryAuthorizationCodeServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
......@@ -42,6 +43,7 @@ import org.springframework.security.oauth2.config.annotation.web.configurers.Res
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.approval.ApprovalStore;
import org.springframework.security.oauth2.provider.approval.TokenApprovalStore;
import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler;
import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
......@@ -80,6 +82,8 @@ public class OAuth2ServerConfig {
public TokenStore tokenStore() {
return new JwtTokenStore(accessTokenConverter());
}
/**
* Access token converter.
......@@ -196,7 +200,7 @@ public class OAuth2ServerConfig {
store.setTokenStore(tokenStore());
return store;
}
/**
* Token services.
*
......@@ -229,11 +233,19 @@ public class OAuth2ServerConfig {
endpoints
.tokenStore(tokenStore())
.authorizationCodeServices(authorizationCodeServices())
.tokenEnhancer(tokenEnhancerChain)
.userDetailsService(userDetailsService)
.authenticationManager(authenticationManager);
}
@Bean
public AuthorizationCodeServices authorizationCodeServices() {
// Make sure Hazelcast cacheManager is used
AuthorizationCodeServices authCodeServ = new CachedInMemoryAuthorizationCodeServices();
return authCodeServ;
}
@Override
public void configure(final AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.allowFormAuthenticationForClients().checkTokenAccess("permitAll()").realm(APPLICATION_RESOURCE_ID + "/client").passwordEncoder(passwordEncoder);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment