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;
}
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;
}
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;
}
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));
}
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());
}
Aggregations