Search in sources :

Example 6 with UserValidationException

use of nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException in project timbuctoo by HuygensING.

the class TwitterUserValidator method getUserFromAccessToken.

@Override
public Optional<User> getUserFromAccessToken(String accessToken) throws UserValidationException {
    String token = accessToken.substring(0, accessToken.indexOf("_"));
    String tokenSecret = accessToken.substring(accessToken.indexOf("_") + 1, accessToken.length());
    if (users.get(token) != null) {
        return Optional.of(users.get(token));
    } else {
        twitter.setOAuthAccessToken(new AccessToken(token, tokenSecret));
        twitter4j.User userTwitter;
        try {
            userTwitter = twitter.showUser(Long.valueOf(twitter.getId()));
        } catch (TwitterException e) {
            return Optional.empty();
        }
        User user = User.create(userTwitter.getName(), userTwitter.getId() + "", userTwitter.getId() + "");
        users.put(token, user);
        return Optional.of(user);
    }
}
Also used : User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) AccessToken(twitter4j.auth.AccessToken) TwitterException(twitter4j.TwitterException)

Example 7 with UserValidationException

use of nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException in project timbuctoo by HuygensING.

the class JsonLdEditEndpoint method submitChanges.

@PUT
public Response submitChanges(String jsonLdImport, @PathParam("user") String ownerId, @PathParam("dataset") String dataSetId, @HeaderParam("authorization") String authHeader) throws LogStorageFailedException {
    Optional<User> user;
    try {
        user = userValidator.getUserFromAccessToken(authHeader);
    } catch (UserValidationException e) {
        user = Optional.empty();
    }
    Optional<DataSet> dataSetOpt = dataSetRepository.getDataSet(user.get(), ownerId, dataSetId);
    if (!dataSetOpt.isPresent()) {
        return Response.status(Response.Status.NOT_FOUND).build();
    }
    final DataSet dataSet = dataSetOpt.get();
    final QuadStore quadStore = dataSet.getQuadStore();
    final ImportManager importManager = dataSet.getImportManager();
    final Response response = checkWriteAccess(dataSet, user, permissionFetcher);
    if (response != null) {
        return response;
    }
    try {
        final Future<ImportStatus> promise = importManager.generateLog(dataSet.getMetadata().getBaseUri(), dataSet.getMetadata().getBaseUri(), fromCurrentState(documentLoader, jsonLdImport, quadStore, TIM_USERS + user.get().getPersistentId(), UUID.randomUUID().toString(), Clock.systemUTC()));
        return handleImportManagerResult(promise);
    } catch (IOException e) {
        return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
    } catch (ConcurrentUpdateException e) {
        return Response.status(Response.Status.CONFLICT).entity(e.getMessage()).build();
    }
}
Also used : Response(javax.ws.rs.core.Response) UserValidationException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException) ImportManager(nl.knaw.huygens.timbuctoo.v5.dataset.ImportManager) User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) QuadStore(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.QuadStore) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) ImportStatus(nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus) IOException(java.io.IOException) ConcurrentUpdateException(nl.knaw.huygens.timbuctoo.v5.jsonldimport.ConcurrentUpdateException) PUT(javax.ws.rs.PUT)

Example 8 with UserValidationException

use of nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException in project timbuctoo by HuygensING.

the class SingleEntity method delete.

@DELETE
public Response delete(@PathParam("collection") String collectionName, @HeaderParam("Authorization") String authHeader, @PathParam("id") UUIDParam id) {
    Optional<User> user;
    try {
        user = userValidator.getUserFromAccessToken(authHeader);
    } catch (UserValidationException e) {
        user = Optional.empty();
    }
    Optional<User> newUser = user;
    if (!newUser.isPresent()) {
        return Response.status(Response.Status.UNAUTHORIZED).build();
    } else {
        return transactionEnforcer.executeAndReturn(timbuctooActions -> {
            JsonCrudService jsonCrudService = crudServiceFactory.newJsonCrudService(timbuctooActions);
            try {
                jsonCrudService.delete(collectionName, id.get(), newUser.get());
                return commitAndReturn(Response.noContent().build());
            } catch (InvalidCollectionException e) {
                return rollbackAndReturn(Response.status(Response.Status.NOT_FOUND).entity(jsnO("message", jsn(e.getMessage()))).build());
            } catch (NotFoundException e) {
                return rollbackAndReturn(Response.status(Response.Status.NOT_FOUND).entity(jsnO("message", jsn("not found"))).build());
            } catch (PermissionFetchingException e) {
                return rollbackAndReturn(Response.status(Response.Status.FORBIDDEN).entity(jsnO("message", jsn(e.getMessage()))).build());
            } catch (IOException e) {
                return rollbackAndReturn(Response.status(Response.Status.BAD_REQUEST).entity(jsnO("message", jsn(e.getMessage()))).build());
            }
        });
    }
}
Also used : UserValidationException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException) User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) PermissionFetchingException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.PermissionFetchingException) JsonCrudService(nl.knaw.huygens.timbuctoo.crud.JsonCrudService) InvalidCollectionException(nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException) NotFoundException(nl.knaw.huygens.timbuctoo.core.NotFoundException) IOException(java.io.IOException) DELETE(javax.ws.rs.DELETE)

Aggregations

User (nl.knaw.huygens.timbuctoo.v5.security.dto.User)7 UserValidationException (nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException)7 IOException (java.io.IOException)4 InvalidCollectionException (nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException)3 JsonCrudService (nl.knaw.huygens.timbuctoo.crud.JsonCrudService)3 PermissionFetchingException (nl.knaw.huygens.timbuctoo.v5.security.exceptions.PermissionFetchingException)3 POST (javax.ws.rs.POST)2 PUT (javax.ws.rs.PUT)2 NotFoundException (nl.knaw.huygens.timbuctoo.core.NotFoundException)2 ImportManager (nl.knaw.huygens.timbuctoo.v5.dataset.ImportManager)2 ImportStatus (nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus)2 DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ExecutionResult (graphql.ExecutionResult)1 GraphQL (graphql.GraphQL)1 GraphQLSchema (graphql.schema.GraphQLSchema)1 UUID (java.util.UUID)1 DELETE (javax.ws.rs.DELETE)1