Search in sources :

Example 1 with IridaClientDetails

use of ca.corefacility.bioinformatics.irida.model.IridaClientDetails in project irida by phac-nml.

the class ClientsController method postCreateClient.

/**
 * Create a new client
 *
 * @param client           The client to add
 * @param scope_read       if the client should be allowed to read from the server (value
 *                         should be "read").
 * @param scope_write      if the client should be allowed to write to the server (value
 *                         should be "write").
 * @param scope_auto_read  whether to allow automatic authorization for the read scope
 * @param scope_auto_write whether to allow automatic authorization for the write scope
 * @param refresh          whether the client should allow refresh tokens
 * @param model            Model for the view
 * @param locale           Locale of the current user session
 * @return Redirect to the newly created client page, or back to the
 * creation page in case of an error.
 */
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String postCreateClient(@ModelAttribute IridaClientDetails client, @RequestParam(required = false, defaultValue = "") String scope_read, @RequestParam(required = false, defaultValue = "") String scope_write, @RequestParam(required = false, defaultValue = "") String scope_auto_read, @RequestParam(required = false, defaultValue = "") String scope_auto_write, @RequestParam(required = false, defaultValue = "") String refresh, Model model, Locale locale) {
    client.setClientSecret(generateClientSecret());
    Set<String> autoScopes = new HashSet<>();
    Set<String> scopes = new HashSet<>();
    if (scope_write.equals("write")) {
        scopes.add("write");
        if (scope_auto_write.equals("write")) {
            autoScopes.add("write");
        }
    }
    if (scope_read.equals("read")) {
        scopes.add("read");
        if (scope_auto_read.equals("read")) {
            autoScopes.add("read");
        }
    }
    if (refresh.equals("refresh")) {
        client.getAuthorizedGrantTypes().add("refresh_token");
    }
    client.setScope(scopes);
    client.setAutoApprovableScopes(autoScopes);
    String responsePage;
    try {
        IridaClientDetails create = clientDetailsService.create(client);
        responsePage = "redirect:/clients/" + create.getId();
    } catch (RuntimeException ex) {
        handleCreateUpdateException(ex, model, locale, scope_write, scope_read, scope_auto_read, scope_auto_write, client.getClientId(), client.getAccessTokenValiditySeconds());
        responsePage = getAddClientPage(model);
    }
    return responsePage;
}
Also used : IridaClientDetails(ca.corefacility.bioinformatics.irida.model.IridaClientDetails) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with IridaClientDetails

use of ca.corefacility.bioinformatics.irida.model.IridaClientDetails in project irida by phac-nml.

the class ClientsController method getEditPage.

/**
 * Get the page to edit {@link IridaClientDetails}
 *
 * @param clientId
 *            The ID of the {@link IridaClientDetails}
 * @param model
 *            Model for the view
 * @return view name for editing client details
 */
@RequestMapping(value = "/{clientId}/edit", method = RequestMethod.GET)
public String getEditPage(@PathVariable Long clientId, Model model) {
    IridaClientDetails client = clientDetailsService.read(clientId);
    model.addAttribute("client", client);
    // make it easier
    if (!client.getAuthorizedGrantTypes().isEmpty()) {
        model.addAttribute("selectedGrant", client.getAuthorizedGrantTypes().iterator().next());
    }
    Set<String> scopes = client.getScope();
    for (String scope : scopes) {
        model.addAttribute("given_scope_" + scope, true);
    }
    Set<String> autoScopes = client.getAutoApprovableScopes();
    for (String autoScope : autoScopes) {
        model.addAttribute("given_scope_auto_" + autoScope, true);
    }
    if (client.getAuthorizedGrantTypes().contains("refresh_token")) {
        model.addAttribute("refresh", true);
    }
    getAddClientPage(model);
    return EDIT_CLIENT_PAGE;
}
Also used : IridaClientDetails(ca.corefacility.bioinformatics.irida.model.IridaClientDetails) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with IridaClientDetails

use of ca.corefacility.bioinformatics.irida.model.IridaClientDetails in project irida by phac-nml.

the class ClientsController method postEditClient.

/**
 * Submit client details edit
 *
 * @param clientId                   the long ID of the {@link IridaClientDetails} to edit
 * @param accessTokenValiditySeconds The new accessTokenValiditySeconds
 * @param authorizedGrantTypes       the new authorizedGrantTypes
 * @param scope_read                 whether to allow read scope
 * @param scope_write                whether to allow write scope
 * @param scope_auto_read            whether to allow automatic authorization for the read scope
 * @param scope_auto_write           whether to allow automatic authorization for the write scope
 * @param new_secret                 whether to generate a new client secret
 * @param refresh                    Whether the client shoudl allow refresh tokens
 * @param refreshTokenValidity       How long the refresh token will be valid
 * @param model                      Model for the view
 * @param locale                     Locale of the logged in user
 * @return Redirect to the client details page if successful, the edit page
 * if there are errors
 */
@RequestMapping(value = "/{clientId}/edit", method = RequestMethod.POST)
public String postEditClient(@PathVariable Long clientId, @RequestParam(required = false, defaultValue = "0") Integer accessTokenValiditySeconds, @RequestParam(required = false, defaultValue = "") String authorizedGrantTypes, @RequestParam(required = false, defaultValue = "") String scope_read, @RequestParam(required = false, defaultValue = "") String scope_write, @RequestParam(required = false, defaultValue = "") String scope_auto_read, @RequestParam(required = false, defaultValue = "") String scope_auto_write, @RequestParam(required = false, defaultValue = "") String refresh, @RequestParam(required = false, defaultValue = "0") Integer refreshTokenValidity, @RequestParam(required = false, defaultValue = "") String new_secret, Model model, Locale locale) {
    IridaClientDetails readClient = clientDetailsService.read(clientId);
    if (accessTokenValiditySeconds != 0) {
        readClient.setAccessTokenValiditySeconds(accessTokenValiditySeconds);
    }
    if (!Strings.isNullOrEmpty(authorizedGrantTypes)) {
        readClient.setAuthorizedGrantTypes(Sets.newHashSet(authorizedGrantTypes));
    }
    Set<String> scopes = new HashSet<>();
    Set<String> autoScopes = new HashSet<>();
    if (scope_write.equals("write")) {
        scopes.add("write");
        if (scope_auto_write.equals("write")) {
            autoScopes.add("write");
        }
    }
    if (scope_read.equals("read")) {
        scopes.add("read");
        if (scope_auto_read.equals("read")) {
            autoScopes.add("read");
        }
    }
    readClient.setScope(scopes);
    readClient.setAutoApprovableScopes(autoScopes);
    if (!Strings.isNullOrEmpty(new_secret)) {
        String clientSecret = generateClientSecret();
        readClient.setClientSecret(clientSecret);
    }
    if (refresh.equals("refresh")) {
        readClient.getAuthorizedGrantTypes().add("refresh_token");
    } else {
        readClient.getAuthorizedGrantTypes().remove("refresh_token");
    }
    if (refreshTokenValidity != 0) {
        readClient.setRefreshTokenValiditySeconds(refreshTokenValidity);
    }
    String response;
    try {
        clientDetailsService.update(readClient);
        response = "redirect:/clients/" + clientId;
    } catch (RuntimeException e) {
        handleCreateUpdateException(e, model, locale, scope_write, scope_read, scope_auto_write, scope_auto_read, readClient.getClientId(), accessTokenValiditySeconds);
        response = getEditPage(clientId, model);
    }
    return response;
}
Also used : IridaClientDetails(ca.corefacility.bioinformatics.irida.model.IridaClientDetails) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with IridaClientDetails

use of ca.corefacility.bioinformatics.irida.model.IridaClientDetails in project irida by phac-nml.

the class ClientsControllerTest method testSubmitEditClient.

@Test
public void testSubmitEditClient() {
    IridaClientDetails client = new IridaClientDetails();
    Long id = 1L;
    client.setId(id);
    ExtendedModelMap model = new ExtendedModelMap();
    String scope_read = "read";
    when(clientDetailsService.read(id)).thenReturn(client);
    String postCreateClient = controller.postEditClient(id, 0, "", scope_read, "", "", "", "", 0, "", model, locale);
    assertEquals("redirect:/clients/1", postCreateClient);
    ArgumentCaptor<IridaClientDetails> captor = ArgumentCaptor.forClass(IridaClientDetails.class);
    verify(clientDetailsService).update(captor.capture());
    IridaClientDetails updated = captor.getValue();
    Set<String> scope = updated.getScope();
    assertTrue(scope.contains(scope_read));
}
Also used : ExtendedModelMap(org.springframework.ui.ExtendedModelMap) IridaClientDetails(ca.corefacility.bioinformatics.irida.model.IridaClientDetails) Test(org.junit.Test)

Example 5 with IridaClientDetails

use of ca.corefacility.bioinformatics.irida.model.IridaClientDetails in project irida by phac-nml.

the class ClientsControllerTest method testSubmitEditWithClientSecretUpdate.

@Test
public void testSubmitEditWithClientSecretUpdate() {
    IridaClientDetails client = new IridaClientDetails();
    String originalSecret = "original";
    Long id = 1L;
    client.setId(id);
    client.setClientSecret(originalSecret);
    ExtendedModelMap model = new ExtendedModelMap();
    when(clientDetailsService.read(id)).thenReturn(client);
    String postCreateClient = controller.postEditClient(id, 0, "", "", "", "", "", "", 0, "true", model, locale);
    assertEquals("redirect:/clients/1", postCreateClient);
    ArgumentCaptor<IridaClientDetails> captor = ArgumentCaptor.forClass(IridaClientDetails.class);
    verify(clientDetailsService).update(captor.capture());
    IridaClientDetails value = captor.getValue();
    assertNotEquals("Secret should be different", originalSecret, value.getClientSecret());
}
Also used : ExtendedModelMap(org.springframework.ui.ExtendedModelMap) IridaClientDetails(ca.corefacility.bioinformatics.irida.model.IridaClientDetails) Test(org.junit.Test)

Aggregations

IridaClientDetails (ca.corefacility.bioinformatics.irida.model.IridaClientDetails)15 Test (org.junit.Test)8 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 DataTablesResponse (ca.corefacility.bioinformatics.irida.ria.web.components.datatables.DataTablesResponse)2 DataTablesResponseModel (ca.corefacility.bioinformatics.irida.ria.web.components.datatables.models.DataTablesResponseModel)2 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)2 PageRequest (org.springframework.data.domain.PageRequest)2 DataTablesParams (ca.corefacility.bioinformatics.irida.ria.web.components.datatables.DataTablesParams)1 DTClient (ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTClient)1 PageImpl (org.springframework.data.domain.PageImpl)1 Sort (org.springframework.data.domain.Sort)1 Specification (org.springframework.data.jpa.domain.Specification)1 Authentication (org.springframework.security.core.Authentication)1 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1