use of io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKey in project hopsworks by logicalclocks.
the class ApiKeyController method getApiKey.
/**
* @param key
* @return
* @throws ApiKeyException
*/
public ApiKey getApiKey(String key) throws ApiKeyException {
String[] parts = key.split(Secret.KEY_ID_SEPARATOR_REGEX);
if (parts.length < 2) {
throw new ApiKeyException(RESTCodes.ApiKeyErrorCode.KEY_NOT_FOUND, Level.FINE);
}
ApiKey apiKey = apiKeyFacade.findByPrefix(parts[0]);
if (apiKey == null) {
throw new ApiKeyException(RESTCodes.ApiKeyErrorCode.KEY_NOT_FOUND, Level.FINE);
}
// ___MinLength can be set to 0 b/c no validation is needed if the key was in db
Secret secret = new Secret(parts[0], parts[1], apiKey.getSalt());
if (!secret.getSha256HexDigest().equals(apiKey.getSecret())) {
throw new ApiKeyException(RESTCodes.ApiKeyErrorCode.KEY_NOT_FOUND, Level.FINE);
}
return apiKey;
}
use of io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKey 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.ApiKey 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.ApiKey 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.ApiKey in project hopsworks by logicalclocks.
the class ApiKeyController method deleteAll.
/**
* @param user
*/
public void deleteAll(Users user) throws ApiKeyException {
List<ApiKey> keys = apiKeyFacade.findByUser(user);
for (ApiKey key : keys) {
// run delete handlers
ApiKeyHandler.runApiKeyDeleteHandlers(apiKeyHandlers, key);
apiKeyFacade.remove(key);
}
sendDeletedAllEmail(user);
}
Aggregations