-
Notifications
You must be signed in to change notification settings - Fork 2
Common holder for authenticated user information #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Update on this: using only Spring to configure OIDC (no provider's libraries) and setting the correct scopes ( We might still want to provide a Vaadin wrapper for user information, to be used for example with other authentication method (e.g. internal form), but it comes easier to build one from the same interface. The OIDC standard defines the concept of claim (see spec) as a piece of information that is provided about the user. Several claims are already defined by the standards, and they're all mapped to |
We should investigate if this way of injecting the authentication principal works in Vaadin views and, if not, why: @Route("/foo")
public class MyView extends VerticalLayout {
public MyView(@AuthenticatedPrincipal OidcUser user) {
// ...
}
} The other method to get the user instance is statically from the Authentication auth = SecurityContextHolder.getContext().getAuthentication();
OidcUser user = (OidcUser) auth.getPrincipal(); Spring's oauth2Login.successHandler((req, res, auth) -> {
OidcUser user = (OidcUser) auth.getPrincipal();
// set the user somewhere, like req.getSession()
}); |
We could provide accessors to the user object via an injectable context bean, something like: interface VaadinAuthContext {
Optional<OidcUser> getAuthenticatedUser();
} And then expose it in the starter as a bean: @Configuration
public class VaadinAuthSecurityConfiguration extends VaadinWebSecurity {
// ...
@Bean
public VaadinAuthContext getAuthContext() {
return () -> Optional.of(SecurityContextHolder.getContext())
.map(SecurityContext::getAuthentication)
.map(Authentication::getPrincipal)
.filter(OidcUser.class::isInstance)
.map(OidcUser.class::cast);
}
} Then the bean would be injectable in Vaadin views and used to get the authenticated user, e.g. @Route("/foo")
public class FooView extends VerticalLayout {
public FooView(VaadinAuthContext authContext) {
authContext.getAuthenticatedUser().ifPresentOrElse(
user -> add(new Text("Hello " + user.getFullName())),
() -> add(new Text("You are not authenticated."));
}
} |
When authenticating against an OIDC provider with Spring, we can find user information using the
Authentication.getPrincipal()
method.The method signature returns a generic
Object
, so each provider might return different types.Prototyping with the three selected providers turns out:
KeycloakPrincipal
(which implements Java SecurityPrincipal
);OidcUser
To provide a consistent API, we should provide a common holder for these informations, such as a
VaadinUser
object. An instance of said type would then be filled with informations from each provided and available in the Vaadin session or by bean injection.The text was updated successfully, but these errors were encountered: