use of io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKeyScope in project hopsworks by logicalclocks.
the class ApiKeyController method addScope.
/**
* @param user
* @param keyName
* @param scopes
* @return
* @throws ApiKeyException
*/
public ApiKey addScope(Users user, String keyName, Set<ApiScope> scopes) throws ApiKeyException {
ApiKey apiKey = validate(user, keyName, scopes);
List<ApiKeyScope> newScopes;
Set<ApiScope> oldScopes = toApiScope(apiKey.getApiKeyScopeCollection());
scopes.removeAll(oldScopes);
if (!scopes.isEmpty()) {
newScopes = getKeyScopes(scopes, apiKey);
apiKey.getApiKeyScopeCollection().addAll(newScopes);
apiKey.setModified(new Date());
apiKey = apiKeyFacade.update(apiKey);
// run api key update handlers
ApiKeyHandler.runApiKeyCreateHandlers(apiKeyHandlers, apiKey);
}
return apiKey;
}
use of io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKeyScope in project hopsworks by logicalclocks.
the class ApiKeyController method update.
/**
* @param user
* @param keyName
* @param scopes
* @return
* @throws ApiKeyException
*/
public ApiKey update(Users user, String keyName, Set<ApiScope> scopes) throws ApiKeyException {
ApiKey apiKey = validate(user, keyName, scopes);
Collection<ApiKeyScope> oldScopes = apiKey.getApiKeyScopeCollection();
List<ApiKeyScope> toKeep = new ArrayList<>();
List<ApiKeyScope> toAdd = new ArrayList<>();
boolean exist;
boolean added = false;
for (ApiScope scope : scopes) {
exist = false;
for (ApiKeyScope apiKeyScope : oldScopes) {
if (apiKeyScope.getScope().equals(scope)) {
toKeep.add(apiKeyScope);
exist = true;
break;
}
}
if (!exist) {
added = true;
toAdd.add(new ApiKeyScope(scope, apiKey));
}
}
boolean update = false;
oldScopes.removeAll(toKeep);
if (!oldScopes.isEmpty()) {
for (ApiKeyScope apiKeyScope : oldScopes) {
apiKeyScopeFacade.remove(apiKeyScope);
}
update = true;
}
if (added) {
toKeep.addAll(toAdd);
update = true;
}
if (update) {
// make a copy of toRemove scopes
Collection<ApiKeyScope> toRemove = new ArrayList<>(oldScopes);
apiKey.setApiKeyScopeCollection(toKeep);
apiKey.setModified(new Date());
apiKey = apiKeyFacade.update(apiKey);
// run api key handlers
ApiKeyHandler.runApiKeyCreateHandlers(apiKeyHandlers, apiKey, toAdd);
ApiKeyHandler.runApiKeyDeleteHandlers(apiKeyHandlers, apiKey, toRemove);
}
return apiKey;
}
use of io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKeyScope in project hopsworks by logicalclocks.
the class ApiKeyController method removeScope.
/**
* @param user
* @param keyName
* @param scopes
* @return
* @throws ApiKeyException
*/
public ApiKey removeScope(Users user, String keyName, Set<ApiScope> scopes) throws ApiKeyException {
ApiKey apiKey = validate(user, keyName, scopes);
Collection<ApiKeyScope> oldScopes = apiKey.getApiKeyScopeCollection();
List<ApiKeyScope> toRemove = new ArrayList<>();
for (ApiScope scope : scopes) {
for (ApiKeyScope apiKeyScope : oldScopes) {
if (apiKeyScope.getScope().equals(scope)) {
toRemove.add(apiKeyScope);
break;
}
}
}
boolean removed = apiKey.getApiKeyScopeCollection().removeAll(toRemove);
if (removed && !apiKey.getApiKeyScopeCollection().isEmpty()) {
// this should not be necessary
for (ApiKeyScope apiKeyScope : toRemove) {
apiKeyScopeFacade.remove(apiKeyScope);
}
apiKey.setModified(new Date());
apiKey = apiKeyFacade.update(apiKey);
// run api key update handlers
ApiKeyHandler.runApiKeyDeleteHandlers(apiKeyHandlers, apiKey, toRemove);
} else if (removed && apiKey.getApiKeyScopeCollection().isEmpty()) {
throw new ApiKeyException(RESTCodes.ApiKeyErrorCode.KEY_SCOPE_EMPTY, Level.FINE);
}
return apiKey;
}
use of io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKeyScope in project hopsworks by logicalclocks.
the class ApiKeyController method createNewKey.
/**
* Create new key for the give user with the given key name and scopes.
* @param user
* @param keyName
* @param scopes
* @throws UserException
* @throws ApiKeyException
* @return
*/
public String createNewKey(Users user, String keyName, Set<ApiScope> scopes, Boolean reserved) throws UserException, ApiKeyException {
if (user == null) {
throw new UserException(RESTCodes.UserErrorCode.USER_WAS_NOT_FOUND, Level.FINE);
}
if (keyName == null || keyName.isEmpty()) {
throw new ApiKeyException(RESTCodes.ApiKeyErrorCode.KEY_NAME_NOT_SPECIFIED, Level.FINE);
}
if (keyName.length() > 45) {
throw new ApiKeyException(RESTCodes.ApiKeyErrorCode.KEY_NAME_NOT_VALID, Level.FINE);
}
if (scopes == null || scopes.isEmpty()) {
throw new ApiKeyException(RESTCodes.ApiKeyErrorCode.KEY_SCOPE_NOT_SPECIFIED, Level.FINE);
}
ApiKey apiKey = apiKeyFacade.findByUserAndName(user, keyName);
if (apiKey != null) {
throw new ApiKeyException(RESTCodes.ApiKeyErrorCode.KEY_NAME_EXIST, Level.FINE);
}
Secret secret = generateApiKey();
Date date = new Date();
apiKey = new ApiKey(user, secret.getPrefix(), secret.getSha256HexDigest(), secret.getSalt(), date, date, keyName, reserved);
List<ApiKeyScope> keyScopes = getKeyScopes(scopes, apiKey);
apiKey.setApiKeyScopeCollection(keyScopes);
apiKeyFacade.save(apiKey);
// run create handlers
ApiKeyHandler.runApiKeyCreateHandlers(apiKeyHandlers, apiKey);
sendCreatedEmail(user, keyName, date, scopes);
return secret.getPrefixPlusSecret();
}
Aggregations