use of io.helidon.security.Subject in project helidon by oracle.
the class AtnProvider method syncAuthenticate.
@Override
protected AuthenticationResponse syncAuthenticate(ProviderRequest providerRequest) {
EndpointConfig endpointConfig = providerRequest.endpointConfig();
Config atnConfig = endpointConfig.config(CONFIG_KEY).orElse(null);
Subject user = null;
Subject service = null;
List<Auth> list;
Optional<AtnConfig> optional = providerRequest.endpointConfig().instance(AtnConfig.class);
if (optional.isPresent()) {
list = optional.get().auths();
} else if (atnConfig != null && !atnConfig.isLeaf()) {
list = atnConfig.asNodeList().map(this::fromConfig).orElse(Collections.emptyList());
} else {
list = fromAnnotations(endpointConfig);
}
for (Auth authentication : list) {
if (authentication.type() == SubjectType.USER) {
user = buildSubject(authentication);
} else {
service = buildSubject(authentication);
}
}
return AuthenticationResponse.success(user, service);
}
use of io.helidon.security.Subject in project helidon by oracle.
the class OutboundOverrideExample method startServingService.
static CompletionStage<Void> startServingService(int port) {
Config config = createConfig("serving-service");
Routing routing = Routing.builder().register(WebSecurity.create(config.get("security"))).get("/hello", (req, res) -> {
res.send(req.context().get(SecurityContext.class).flatMap(SecurityContext::user).map(Subject::principal).map(Principal::getName).orElse("Anonymous"));
}).build();
return startServer(routing, port, server -> servingPort = server.port());
}
use of io.helidon.security.Subject in project helidon by oracle.
the class OutboundOverrideJwtExample method startServingService.
static CompletionStage<Void> startServingService(int port) {
Config config = createConfig("serving-service-jwt");
Routing routing = Routing.builder().register(WebSecurity.create(config.get("security"))).get("/hello", (req, res) -> {
// This is the token. It should be bearer <signed JWT base64 encoded>
req.headers().first("Authorization").ifPresent(System.out::println);
res.send(req.context().get(SecurityContext.class).flatMap(SecurityContext::user).map(Subject::principal).map(Principal::getName).orElse("Anonymous"));
}).build();
return startServer(routing, port, server -> servingPort = server.port());
}
use of io.helidon.security.Subject in project helidon by oracle.
the class OutboundProviderSyncTest method testSuccess.
@Test
public void testSuccess() {
String username = "aUser";
Subject subject = Subject.create(Principal.create(username));
SecurityContext context = mock(SecurityContext.class);
when(context.user()).thenReturn(Optional.of(subject));
when(context.service()).thenReturn(Optional.empty());
SecurityEnvironment se = SecurityEnvironment.create();
ProviderRequest request = mock(ProviderRequest.class);
when(request.securityContext()).thenReturn(context);
when(request.env()).thenReturn(se);
OutboundProviderSync ops = new OutboundProviderSync();
OutboundSecurityResponse response = ops.syncOutbound(request, SecurityEnvironment.create(), EndpointConfig.create());
assertThat(response.status(), is(SecurityResponse.SecurityStatus.SUCCESS));
assertThat(response.requestHeaders().get("X-AUTH-USER"), is(List.of(username)));
}
use of io.helidon.security.Subject in project helidon by oracle.
the class GoogleBuilderMain method start.
static int start(int port) {
Security security = Security.builder().addProvider(GoogleTokenProvider.builder().clientId("your-client-id.apps.googleusercontent.com")).build();
WebSecurity ws = WebSecurity.create(security);
Routing.Builder routing = Routing.builder().register(ws).get("/rest/profile", WebSecurity.authenticate(), (req, res) -> {
Optional<SecurityContext> securityContext = req.context().get(SecurityContext.class);
res.headers().contentType(MediaType.TEXT_PLAIN.withCharset("UTF-8"));
res.send("Response from builder based service, you are: \n" + securityContext.flatMap(SecurityContext::user).map(Subject::toString).orElse("Security context is null"));
req.next();
}).register(StaticContentSupport.create("/WEB"));
theServer = GoogleUtil.startIt(port, routing);
return theServer.port();
}
Aggregations