switch (credentialCreator.getCredentialType()) { case API_KEY: cryptedCredential = cryptApiKey(credentialCreator.getCredentialPlainKey()); break; case PASSWORD: default: cryptedCredential = cryptPassword(credentialCreator.getCredentialPlainKey()); break; CredentialImpl credentialImpl = new CredentialImpl(credentialCreator.getScopeId(), credentialCreator.getUserId(), credentialCreator.getCredentialType(), cryptedCredential, credentialCreator.getCredentialStatus(), credentialCreator.getExpirationDate());
/** * Creates a new Credential based on the information provided in CredentialCreator * parameter. * * @param scopeId * The {@link ScopeId} in which to create the {@link Credential} * @param credentialCreator * Provides the information for the new Credential to be created. * @return The newly created Credential object. * @throws Exception * Whenever something bad happens. See specific {@link KapuaService} exceptions. * @since 1.0.0 */ @ApiOperation(nickname = "credentialCreate", value = "Create a Credential", notes = "Creates a new Credential based on the information provided in CredentialCreator parameter.", response = Credential.class) @POST @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) public Credential create( @ApiParam(value = "The ScopeId in which to create the Credential", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId, @ApiParam(value = "Provides the information for the new Credential to be created", required = true) CredentialCreator credentialCreator) throws Exception { credentialCreator.setScopeId(scopeId); return credentialService.create(credentialCreator); }
switch (credentialCreator.getCredentialType()) { case API_KEY: cryptedCredential = cryptApiKey(credentialCreator.getCredentialPlainKey()); break; case PASSWORD: default: cryptedCredential = cryptPassword(credentialCreator.getCredentialPlainKey()); break; CredentialImpl credentialImpl = new CredentialImpl(credentialCreator.getScopeId(), credentialCreator.getUserId(), credentialCreator.getCredentialType(), cryptedCredential, credentialCreator.getCredentialStatus(), credentialCreator.getExpirationDate());
ArgumentValidator.notNull(credentialCreator.getScopeId(), "credentialCreator.scopeId"); ArgumentValidator.notNull(credentialCreator.getUserId(), "credentialCreator.userId"); ArgumentValidator.notNull(credentialCreator.getCredentialType(), "credentialCreator.credentialType"); ArgumentValidator.notNull(credentialCreator.getCredentialStatus(), "credentialCreator.credentialStatus"); if (credentialCreator.getCredentialType() != CredentialType.API_KEY) { ArgumentValidator.notEmptyOrNull(credentialCreator.getCredentialPlainKey(), "credentialCreator.credentialKey"); if (credentialCreator.getCredentialType() == CredentialType.PASSWORD) { CredentialListResult existingCredentials = findByUserId(credentialCreator.getScopeId(), credentialCreator.getUserId()); for (Credential credential : existingCredentials.getItems()) { if (credential.getCredentialType().equals(CredentialType.PASSWORD)) { ArgumentValidator.match(credentialCreator.getCredentialPlainKey(), CommonsValidationRegex.PASSWORD_REGEXP, "credentialCreator.credentialKey"); AuthorizationService authorizationService = locator.getService(AuthorizationService.class); PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class); authorizationService.checkPermission(permissionFactory.newPermission(AuthenticationDomains.CREDENTIAL_DOMAIN, Actions.write, credentialCreator.getScopeId())); switch (credentialCreator.getCredentialType()) { case API_KEY: // Generate new api key SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); credentialCreator = new CredentialCreatorImpl(credentialCreator.getScopeId(), credentialCreator.getUserId(), credentialCreator.getCredentialType(), fullKey, credentialCreator.getCredentialStatus(), credentialCreator.getExpirationDate());
ArgumentValidator.notNull(credentialCreator.getScopeId(), "credentialCreator.scopeId"); ArgumentValidator.notNull(credentialCreator.getUserId(), "credentialCreator.userId"); ArgumentValidator.notNull(credentialCreator.getCredentialType(), "credentialCreator.credentialType"); ArgumentValidator.notNull(credentialCreator.getCredentialStatus(), "credentialCreator.credentialStatus"); if (credentialCreator.getCredentialType() != CredentialType.API_KEY) { ArgumentValidator.notEmptyOrNull(credentialCreator.getCredentialPlainKey(), "credentialCreator.credentialKey"); if (credentialCreator.getCredentialType() == CredentialType.PASSWORD) { CredentialListResult existingCredentials = findByUserId(credentialCreator.getScopeId(), credentialCreator.getUserId()); for (Credential credential : existingCredentials.getItems()) { if (credential.getCredentialType().equals(CredentialType.PASSWORD)) { ArgumentValidator.match(credentialCreator.getCredentialPlainKey(), CommonsValidationRegex.PASSWORD_REGEXP, "credentialCreator.credentialKey"); AuthorizationService authorizationService = locator.getService(AuthorizationService.class); PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class); authorizationService.checkPermission(permissionFactory.newPermission(AuthenticationDomains.CREDENTIAL_DOMAIN, Actions.write, credentialCreator.getScopeId())); switch (credentialCreator.getCredentialType()) { case API_KEY: // Generate new api key SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); credentialCreator = new CredentialCreatorImpl(credentialCreator.getScopeId(), credentialCreator.getUserId(), credentialCreator.getCredentialType(), fullKey, credentialCreator.getCredentialStatus(), credentialCreator.getExpirationDate());