-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proof of concept support for authorization.
Indirectly related to #16.
- Loading branch information
Showing
29 changed files
with
786 additions
and
27 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
...core.auth.core/src/main/java/org/openhab/core/internal/auth/AuthorizationManagerImpl.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,78 @@ | ||
/** | ||
* Copyright (c) 2019-2020 Contributors to the OpenSmartHouse project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* 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.openhab.core.internal.auth; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import org.openhab.core.auth.Authentication; | ||
import org.openhab.core.auth.AuthorizationManager; | ||
import org.openhab.core.auth.Permission; | ||
import org.openhab.core.auth.PermissionEvaluator; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
import org.osgi.service.component.annotations.ReferenceCardinality; | ||
import org.osgi.service.component.annotations.ReferencePolicy; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@Component | ||
public class AuthorizationManagerImpl implements AuthorizationManager { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(AuthorizationManagerImpl.class); | ||
private List<PermissionEvaluator<Object>> evaluators = new CopyOnWriteArrayList<>(); | ||
|
||
@Override | ||
public <T> boolean hasPermission(Permission permission, T object, Authentication authentication) { | ||
if (authentication == null) { | ||
return false; | ||
} | ||
|
||
for (PermissionEvaluator<Object> evaluator : evaluators) { | ||
if (evaluator.supports(object.getClass(), permission)) { | ||
if (!evaluator.hasPermission(permission, authentication, object)) { | ||
logger.trace("Access denied to object {} ({}) for authentication {} reported by evaluator {}", | ||
object, object.getClass().getName(), authentication, evaluator); | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public boolean hasPermission(Permission permission, String id, Class<?> type, Authentication authentication) { | ||
if (authentication == null) { | ||
return false; | ||
} | ||
|
||
for (PermissionEvaluator<Object> evaluator : evaluators) { | ||
if (evaluator.supports(type, permission)) { | ||
if (!evaluator.hasPermission(permission, authentication, (Class<Object>) type, id)) { | ||
logger.trace("Access denied to object with id {} ({}) for authentication {} reported by evaluator {}", | ||
id, type.getName(), authentication, evaluator); | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC) | ||
public void addPermissionEvaluator(PermissionEvaluator<Object> evaluator) { | ||
this.evaluators.add(evaluator); | ||
} | ||
|
||
public void removePermissionEvaluator(PermissionEvaluator<Object> evaluator) { | ||
this.evaluators.remove(evaluator); | ||
} | ||
|
||
} |
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
30 changes: 30 additions & 0 deletions
30
...smarthouse.core.auth/src/main/java/org/openhab/core/auth/AuthenticationContextHolder.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,30 @@ | ||
/** | ||
* Copyright (c) 2019-2020 Contributors to the OpenSmartHouse project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* 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.openhab.core.auth; | ||
|
||
import java.util.Optional; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
/** | ||
* The access layer to an authentication context during execution of an action. | ||
* | ||
* @author Łukasz Dywicki - Initial contribution. | ||
*/ | ||
public interface AuthenticationContextHolder { | ||
|
||
@Nullable | ||
Authentication getAuthentication(); | ||
|
||
Optional<Authentication> fetchAuthentication(); | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
....opensmarthouse.core.auth/src/main/java/org/openhab/core/auth/AuthorizationException.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,22 @@ | ||
/** | ||
* Copyright (c) 2019-2020 Contributors to the OpenSmartHouse project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* 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.openhab.core.auth; | ||
|
||
/** | ||
* The access layer to an authentication context during execution of an action. | ||
* | ||
* @author Łukasz Dywicki - Initial contribution. | ||
*/ | ||
public class AuthorizationException extends Exception { | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...rg.opensmarthouse.core.auth/src/main/java/org/openhab/core/auth/AuthorizationManager.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,26 @@ | ||
/** | ||
* Copyright (c) 2010-2020 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* 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.openhab.core.auth; | ||
|
||
/** | ||
* Authorization manager is main entry point for security checks. | ||
* | ||
* @author Łukasz Dywicki - Initial contribution | ||
*/ | ||
public interface AuthorizationManager { | ||
|
||
<T> boolean hasPermission(Permission permission, T object, Authentication authentication); | ||
|
||
boolean hasPermission(Permission permission, String id, Class<?> type, Authentication authentication); | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...smarthouse.core.auth/src/main/java/org/openhab/core/auth/AuthorizationManagerFilters.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,44 @@ | ||
/** | ||
* Copyright (c) 2019-2020 Contributors to the OpenSmartHouse project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* 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.openhab.core.auth; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Set; | ||
import java.util.function.BiFunction; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Helpers for filtering out collections and returning trimmed data. | ||
* | ||
* @author Łukasz Dywicki - Initial contribution | ||
*/ | ||
public class AuthorizationManagerFilters { | ||
|
||
public static <T> List<T> filter(Authentication authentication, BiFunction<Authentication, T, Boolean> check, List<T> values) { | ||
return values.stream().filter(value -> check.apply(authentication, value)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public static <T> Set<T> filter(Authentication authentication, BiFunction<Authentication, T, Boolean> check, Set<T> values) { | ||
return values.stream().filter(value -> check.apply(authentication, value)) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
public static <K, V> Map<K, V> filter(Authentication authentication, BiFunction<Authentication, Entry<K, V>, Boolean> check, Map<K, V> values) { | ||
return values.entrySet().stream().filter(entry -> check.apply(authentication, entry)) | ||
.collect(Collectors.toMap(Entry::getKey, Entry::getValue)); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
...use.core.auth/src/main/java/org/openhab/core/auth/MutableAuthenticationContextHolder.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,24 @@ | ||
/** | ||
* Copyright (c) 2019-2020 Contributors to the OpenSmartHouse project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* 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.openhab.core.auth; | ||
|
||
/** | ||
* Writeable holder for authentication context which should be used early in processing chain. | ||
* | ||
* @author Łukasz Dywicki - Initial contribution. | ||
*/ | ||
public interface MutableAuthenticationContextHolder { | ||
|
||
void setAuthentication(Authentication authentication); | ||
|
||
} |
Oops, something went wrong.