-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use custom CDI extension for injecting Principals (#5883)
This is to avoid dependencies on CDI driven by javax.ws.rs.core.Context and resolve `Principal` suppliers at the Weld / test extension level. Overall, this reduced interference from external libs on the class path when running NessieJaxRsExtension.
- Loading branch information
Showing
5 changed files
with
93 additions
and
41 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
43 changes: 43 additions & 0 deletions
43
...jersey/src/main/java/org/projectnessie/tools/compatibility/jersey/PrincipalExtension.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,43 @@ | ||
/* | ||
* Copyright (C) 2023 Dremio | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.projectnessie.tools.compatibility.jersey; | ||
|
||
import java.security.Principal; | ||
import java.util.function.Supplier; | ||
import javax.enterprise.context.RequestScoped; | ||
import javax.enterprise.event.Observes; | ||
import javax.enterprise.inject.Default; | ||
import javax.enterprise.inject.spi.AfterBeanDiscovery; | ||
import javax.enterprise.inject.spi.BeanManager; | ||
import javax.enterprise.inject.spi.Extension; | ||
import javax.enterprise.util.TypeLiteral; | ||
|
||
/** | ||
* A CDI extension that always produces {@code null} {@link Principal} objects simulating execution | ||
* without authentication. | ||
*/ | ||
public class PrincipalExtension implements Extension { | ||
@SuppressWarnings("unused") | ||
public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager bm) { | ||
Supplier<Principal> principal = () -> null; | ||
|
||
abd.addBean() | ||
.addType(new TypeLiteral<Supplier<Principal>>() {}) | ||
.addQualifier(Default.Literal.INSTANCE) | ||
.scope(RequestScoped.class) | ||
.produceWith(i -> principal); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...rs-testextension/src/main/java/org/projectnessie/jaxrs/ext/ContextPrincipalExtension.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,48 @@ | ||
/* | ||
* Copyright (C) 2023 Dremio | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.projectnessie.jaxrs.ext; | ||
|
||
import java.security.Principal; | ||
import java.util.function.Supplier; | ||
import javax.enterprise.context.RequestScoped; | ||
import javax.enterprise.event.Observes; | ||
import javax.enterprise.inject.Default; | ||
import javax.enterprise.inject.spi.AfterBeanDiscovery; | ||
import javax.enterprise.inject.spi.BeanManager; | ||
import javax.enterprise.inject.spi.Extension; | ||
import javax.enterprise.util.TypeLiteral; | ||
import javax.ws.rs.core.SecurityContext; | ||
|
||
public class ContextPrincipalExtension implements Extension { | ||
private final Supplier<Principal> principal; | ||
|
||
public ContextPrincipalExtension(Supplier<SecurityContext> securityContext) { | ||
this.principal = | ||
() -> { | ||
SecurityContext context = securityContext.get(); | ||
return context == null ? null : context.getUserPrincipal(); | ||
}; | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager bm) { | ||
abd.addBean() | ||
.addType(new TypeLiteral<Supplier<Principal>>() {}) | ||
.addQualifier(Default.Literal.INSTANCE) | ||
.scope(RequestScoped.class) | ||
.produceWith(i -> principal); | ||
} | ||
} |
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
33 changes: 0 additions & 33 deletions
33
...rest-services/src/main/java/org/projectnessie/services/rest/ContextPrincipalSupplier.java
This file was deleted.
Oops, something went wrong.