-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
CredentialStore
+ inmem implementation (#164)
* feat(store): adds a CredentialStore + inmem impl * DEPENDENCIES
- Loading branch information
1 parent
084a28d
commit d47d56d
Showing
15 changed files
with
688 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...identity-hub-core/src/main/java/org/eclipse/edc/identityhub/DefaultServicesExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.identityhub; | ||
|
||
import org.eclipse.edc.identityhub.defaults.InMemoryCredentialStore; | ||
import org.eclipse.edc.identityhub.spi.generator.PresentationGenerator; | ||
import org.eclipse.edc.identityhub.spi.resolution.CredentialQueryResolver; | ||
import org.eclipse.edc.identityhub.spi.store.CredentialStore; | ||
import org.eclipse.edc.runtime.metamodel.annotation.Extension; | ||
import org.eclipse.edc.runtime.metamodel.annotation.Provider; | ||
import org.eclipse.edc.spi.system.ServiceExtension; | ||
import org.eclipse.edc.spi.system.ServiceExtensionContext; | ||
|
||
@Extension("Default Services Extension") | ||
public class DefaultServicesExtension implements ServiceExtension { | ||
@Provider(isDefault = true) | ||
public CredentialStore createInMemStore() { | ||
return new InMemoryCredentialStore(); | ||
|
||
} | ||
|
||
@Provider(isDefault = true) | ||
public CredentialQueryResolver createCredentialResolver(ServiceExtensionContext context) { | ||
context.getMonitor().warning(" #### Creating a default NOOP CredentialQueryResolver, that will always return 'null'!"); | ||
return (query, issuerScopes) -> null; | ||
} | ||
|
||
@Provider(isDefault = true) | ||
public PresentationGenerator createPresentationGenerator(ServiceExtensionContext context) { | ||
context.getMonitor().warning(" #### Creating a default NOOP PresentationGenerator, that will always return 'null'!"); | ||
return (credentials, presentationDefinition) -> null; | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
...-hub-core/src/main/java/org/eclipse/edc/identityhub/defaults/InMemoryCredentialStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.identityhub.defaults; | ||
|
||
import org.eclipse.edc.connector.core.store.ReflectionBasedQueryResolver; | ||
import org.eclipse.edc.identityhub.spi.store.CredentialStore; | ||
import org.eclipse.edc.identityhub.spi.store.model.VerifiableCredentialResource; | ||
import org.eclipse.edc.spi.query.QueryResolver; | ||
import org.eclipse.edc.spi.query.QuerySpec; | ||
import org.eclipse.edc.spi.result.StoreResult; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
import java.util.stream.Stream; | ||
|
||
import static org.eclipse.edc.spi.result.StoreResult.alreadyExists; | ||
import static org.eclipse.edc.spi.result.StoreResult.notFound; | ||
import static org.eclipse.edc.spi.result.StoreResult.success; | ||
|
||
public class InMemoryCredentialStore implements CredentialStore { | ||
private final Map<String, VerifiableCredentialResource> store = new HashMap<>(); | ||
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); | ||
private final QueryResolver<VerifiableCredentialResource> queryResolver = new ReflectionBasedQueryResolver<>(VerifiableCredentialResource.class); | ||
|
||
@Override | ||
public StoreResult<Void> create(VerifiableCredentialResource credentialResource) { | ||
lock.writeLock().lock(); | ||
var id = credentialResource.getId(); | ||
try { | ||
if (store.containsKey(id)) { | ||
return alreadyExists("A VerifiableCredentialResource with ID %s already exists".formatted(id)); | ||
} | ||
store.put(id, credentialResource); | ||
return success(null); | ||
} finally { | ||
lock.writeLock().unlock(); | ||
} | ||
} | ||
|
||
@Override | ||
public StoreResult<Stream<VerifiableCredentialResource>> query(QuerySpec querySpec) { | ||
lock.readLock().lock(); | ||
try { | ||
var result = queryResolver.query(store.values().stream(), querySpec); | ||
return success(result); | ||
} finally { | ||
lock.readLock().unlock(); | ||
} | ||
} | ||
|
||
@Override | ||
public StoreResult<Void> update(VerifiableCredentialResource credentialResource) { | ||
lock.writeLock().lock(); | ||
try { | ||
var id = credentialResource.getId(); | ||
if (!store.containsKey(id)) { | ||
return notFound("A VerifiableCredentialResource with ID %s was not found".formatted(id)); | ||
} | ||
store.put(id, credentialResource); | ||
return success(); | ||
} finally { | ||
lock.writeLock().unlock(); | ||
} | ||
} | ||
|
||
@Override | ||
public StoreResult<Void> delete(String id) { | ||
lock.writeLock().lock(); | ||
try { | ||
if (!store.containsKey(id)) { | ||
return notFound("A VerifiableCredentialResource with ID %s was not found".formatted(id)); | ||
} | ||
store.remove(id); | ||
return success(); | ||
} finally { | ||
lock.writeLock().unlock(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.