Search in sources :

Example 1 with IridaOAuthException

use of ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException in project irida by phac-nml.

the class ProjectsController method syncProject.

/**
 * Get a {@link Project} from a remote api and mark it to be synchronized in
 * this IRIDA installation
 *
 * @param url           the URL of the remote project
 * @param syncFrequency How often to sync the project
 * @param model         Model for the view
 * @return Redirect to the new project. If an oauth exception occurs it will
 * be forwarded back to the creation page.
 */
@RequestMapping(value = "/projects/synchronize", method = RequestMethod.POST)
public String syncProject(@RequestParam String url, @RequestParam ProjectSyncFrequency syncFrequency, Model model) {
    try {
        Project read = projectRemoteService.read(url);
        read.setId(null);
        read.getRemoteStatus().setSyncStatus(SyncStatus.MARKED);
        read.setSyncFrequency(syncFrequency);
        read = projectService.create(read);
        return "redirect:/projects/" + read.getId() + "/metadata";
    } catch (IridaOAuthException ex) {
        Map<String, String> errors = new HashMap<>();
        errors.put("oauthError", ex.getMessage());
        model.addAttribute("errors", errors);
        return getSynchronizeProjectPage(model);
    } catch (EntityNotFoundException ex) {
        Map<String, String> errors = new HashMap<>();
        errors.put("urlError", ex.getMessage());
        model.addAttribute("errors", errors);
        return getSynchronizeProjectPage(model);
    }
}
Also used : IridaOAuthException(ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException) DTProject(ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTProject) Project(ca.corefacility.bioinformatics.irida.model.project.Project) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) ImmutableMap(com.google.common.collect.ImmutableMap) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with IridaOAuthException

use of ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException in project irida by phac-nml.

the class RemoteAPIControllerTest method testCheckApiStatusInactive.

@Test
public void testCheckApiStatusInactive() {
    Long apiId = 1L;
    RemoteAPI client = new RemoteAPI("name", "http://uri", "a description", "id", "secret");
    when(remoteAPIService.read(apiId)).thenReturn(client);
    when(projectRemoteService.getServiceStatus(client)).thenThrow(new IridaOAuthException("invalid token", client));
    String checkApiStatus = remoteAPIController.checkApiStatus(apiId);
    assertEquals(RemoteAPIController.INVALID_OAUTH_TOKEN, checkApiStatus);
    verify(remoteAPIService).read(apiId);
    verify(projectRemoteService).getServiceStatus(client);
}
Also used : RemoteAPI(ca.corefacility.bioinformatics.irida.model.RemoteAPI) IridaOAuthException(ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException) Test(org.junit.Test)

Example 3 with IridaOAuthException

use of ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException in project irida by phac-nml.

the class ProjectSynchronizationService method findMarkedProjectsToSync.

/**
 * Find projects which should be synchronized and launch a synchornization
 * task.
 */
public synchronized void findMarkedProjectsToSync() {
    // mark any projects which should be synched first
    findProjectsToMark();
    List<Project> markedProjects = projectService.getProjectsWithRemoteSyncStatus(SyncStatus.MARKED);
    logger.trace("Checking for projects to sync");
    for (Project project : markedProjects) {
        /*
			 * Set the correct authorization for the user who's syncing the
			 * project
			 */
        User readBy = project.getRemoteStatus().getReadBy();
        setAuthentication(readBy);
        logger.debug("Syncing project at " + project.getRemoteStatus().getURL());
        try {
            RemoteAPI api = project.getRemoteStatus().getApi();
            tokenService.updateTokenFromRefreshToken(api);
            syncProject(project);
        } catch (IridaOAuthException e) {
            logger.trace("Can't sync project " + project.getRemoteStatus().getURL() + " due to oauth error:", e);
            project.getRemoteStatus().setSyncStatus(SyncStatus.UNAUTHORIZED);
            projectService.update(project);
        } catch (Exception e) {
            logger.debug("An error occurred while synchronizing project " + project.getRemoteStatus().getURL(), e);
            project.getRemoteStatus().setSyncStatus(SyncStatus.ERROR);
            projectService.update(project);
        } finally {
            // clear the context holder when you're done
            SecurityContextHolder.clearContext();
            logger.debug("Done project " + project.getRemoteStatus().getURL());
        }
    }
}
Also used : RemoteAPI(ca.corefacility.bioinformatics.irida.model.RemoteAPI) IridaOAuthException(ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException) Project(ca.corefacility.bioinformatics.irida.model.project.Project) User(ca.corefacility.bioinformatics.irida.model.user.User) IridaOAuthException(ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException) ProjectSynchronizationException(ca.corefacility.bioinformatics.irida.exceptions.ProjectSynchronizationException)

Example 4 with IridaOAuthException

use of ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException in project irida by phac-nml.

the class OAuthTokenRestTemplate method createRequest.

/**
 * Add an OAuth token from the tokenRepository to the request
 */
@Override
protected ClientHttpRequest createRequest(URI uri, HttpMethod method) throws IOException {
    RemoteAPIToken token;
    try {
        token = tokenService.getToken(remoteAPI);
    } catch (EntityNotFoundException ex) {
        logger.debug("No token found for service " + remoteAPI);
        throw new IridaOAuthException("No token fround for service", remoteAPI, ex);
    }
    if (token.isExpired()) {
        logger.debug("Token for service is expired " + remoteAPI);
        throw new IridaOAuthException("Token is expired for service", remoteAPI);
    }
    ClientHttpRequest createRequest = super.createRequest(uri, method);
    createRequest.getHeaders().add("Authorization", "Bearer " + token.getTokenString());
    return createRequest;
}
Also used : IridaOAuthException(ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException) RemoteAPIToken(ca.corefacility.bioinformatics.irida.model.RemoteAPIToken) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest)

Example 5 with IridaOAuthException

use of ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException in project irida by phac-nml.

the class IridaOAuthErrorHandler method handleError.

/**
 * Overriding this method to throw a {@link IridaOAuthException} in case of
 * an HTTP UNAUTHORIZED response.
 */
@Override
public void handleError(ClientHttpResponse response) throws IOException {
    HttpStatus statusCode = response.getStatusCode();
    logger.trace("Checking error type " + statusCode.toString());
    switch(statusCode) {
        case UNAUTHORIZED:
            logger.trace("Throwing new IridaOAuthException for this error");
            throw new IridaOAuthException("User is unauthorized for this service", remoteAPI);
        default:
            logger.trace("Passing error to superclass");
            super.handleError(response);
    }
}
Also used : IridaOAuthException(ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException) HttpStatus(org.springframework.http.HttpStatus)

Aggregations

IridaOAuthException (ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException)7 RemoteAPI (ca.corefacility.bioinformatics.irida.model.RemoteAPI)4 Test (org.junit.Test)3 EntityNotFoundException (ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException)2 Project (ca.corefacility.bioinformatics.irida.model.project.Project)2 ProjectSynchronizationException (ca.corefacility.bioinformatics.irida.exceptions.ProjectSynchronizationException)1 RemoteAPIToken (ca.corefacility.bioinformatics.irida.model.RemoteAPIToken)1 User (ca.corefacility.bioinformatics.irida.model.user.User)1 DTProject (ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTProject)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpStatus (org.springframework.http.HttpStatus)1 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)1 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1