Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PreEnforcerProvider should directly apply to the signal
Saves the step of first getting the pre-enforcer and applying it afterwards. Additionally make diffferent PreEnforcers implement PreEnforcer Interface, to make it easier viewing the different exisitng pre-enforcers. Signed-off-by: David Schwilk <david.schwilk@bosch.io>
- Loading branch information
1 parent
966c53d
commit 51f9f21
Showing
10 changed files
with
89 additions
and
74 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
55 changes: 55 additions & 0 deletions
55
policies/enforcement/src/main/java/org/eclipse/ditto/policies/enforcement/HeaderSetter.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,55 @@ | ||
/* | ||
* Copyright (c) 2022 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.policies.enforcement; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
import org.eclipse.ditto.base.model.auth.AuthorizationContext; | ||
import org.eclipse.ditto.base.model.auth.AuthorizationSubject; | ||
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition; | ||
import org.eclipse.ditto.base.model.headers.DittoHeaders; | ||
import org.eclipse.ditto.base.model.headers.DittoHeadersSettable; | ||
|
||
/** | ||
* Sets additional headers to a signal. | ||
* @param <T> the signal. | ||
*/ | ||
public class HeaderSetter<T extends DittoHeadersSettable<?>> implements PreEnforcer<T> { | ||
|
||
@Override | ||
public CompletionStage<T> apply(final T originalSignal) { | ||
return CompletableFuture.completedFuture(setOriginatorHeader(originalSignal)); | ||
} | ||
|
||
/** | ||
* Set the "ditto-originator" header to the primary authorization subject of a signal. | ||
* | ||
* @param originalSignal A signal with authorization context. | ||
* @param <T> the type of the {@code originalSignal} to preserve in the response. | ||
* @return A copy of the signal with the header "ditto-originator" set. | ||
* @since 3.0.0 | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static <T extends DittoHeadersSettable<?>> T setOriginatorHeader(final T originalSignal) { | ||
final DittoHeaders dittoHeaders = originalSignal.getDittoHeaders(); | ||
final AuthorizationContext authorizationContext = dittoHeaders.getAuthorizationContext(); | ||
return authorizationContext.getFirstAuthorizationSubject() | ||
.map(AuthorizationSubject::getId) | ||
.map(originatorSubjectId -> DittoHeaders.newBuilder(dittoHeaders) | ||
.putHeader(DittoHeaderDefinition.ORIGINATOR.getKey(), originatorSubjectId) | ||
.build()) | ||
.map(originatorHeader -> (T) originalSignal.setDittoHeaders(originatorHeader)) | ||
.orElse(originalSignal); | ||
} | ||
} |
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
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