-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
How to logout with Form based AUTH #27389
Comments
/cc @pedroigor, @sberyozkin |
Another related question for LOGIN, right now I'm using the standard form auth post using the HTML below.
If I wanted, How would I accomplish a programmatic login so I can do bean validations on my Login backing bean? |
@tmulle One option could be to add a |
I'm looking at the How would the code you suggested be called from a link on a web page, i.e. Prior to your response, one thing I tried a few days ago was to delete the cookies on the server side, but the cookies were never deleted. The cookies are being found ok in the server, but setting the maxAge doesn't appear to do anything. I tried initially finding the This code was found in the standard JSF tutorials on how to handle form auth login/logouts. On my users.xhtml file:
|
|
You can also try to "remove" the cookie on the client side and reload the page. I.e. something like @sberyozkin This would probably deserve a clarification in the docs. WDYT? I've also found: https://stackoverflow.com/questions/68640209/quarkus-http-authentication-logout |
@mkouba Sure. We have not really prioritized on the Form authentication (which was introduced in Quarkus to help users already familiar it to get started fast with Quarkus Security) and instead recommend OIDC, but the users are keeping using it, so makes sense to add this hint. I'm not sure though why the code at https://stackoverflow.com/questions/68640209/quarkus-http-authentication-logout does not work... I see, Stuart comments that the path |
I was able to get the logout working via code if I set the "Path" of the cookie as well as setting the max age. I thought that because I simply read the existing cookies and ONLY overrode the max-age that the original path value would suffice. But it appears I have to also manually set that as well. After doing so, the "Quarkus-credential" cookie was deleted properly.
Now, with LOGIN via code, there seems to be an issue where the "quarkus-credential" cookie is NOT being set in the HttpResponse after successful login when I use the following code, which I found from the official JSF documents. The weird thing is that my user is successfully logged in but the cookie is not set, so when I try to access a protected resource I'm redirected back to the login page even though the user is logged in already. Using the standard "/j_security_check" form POST works fine, but we want to login programmatically so we can validate the username/password fields and check to make sure they are entered and display an error on the page itself without being redirected to an error page. This is the code I am using to attempt to login via code from my JSF page. Like I said, it works, if I enter bad values I get an AuthenticationException on the server so I know it is attempting to validate the credentials. It just doesn't seem to be adding the cookie into the cookie stream to get stored in the browser. https://docs.oracle.com/javaee/6/tutorial/doc/glxce.html
|
Was just wondering if either of you had a suggestion on how to handle server side logins via code using the standard http mechanism as I mention above? We'd like to use JFF/Primefaces customized Login forms so we can support validation of fields (missing password, username, etc) BEFORE the form is posted to the server. And the only way I know of is to use a backing bean which calls the code I posted above from the JSF tutorials. Thanks! |
@michalvavrik Hey Michal, did you happen to fix this issue with your recent Form auth update ? thanks |
@sberyozkin in a sense, yes; there is still only way to logout - get rid of cookies. @tmulle mentions he does that and needs a way to avoid redirect to error page - that is possible now. IMO it's enough as removing cookies on the front end is pretty simple. |
FTR this code (POST and |
I'd expect it too, thanks for sharing. My personal opinion is that it is so straightforward, that there is no point of mentioning it in docs or providing build in logout endpoint. I don't know how others see it though. |
@sberyozkin ^^^ |
Hi, How to implement log out on all devices? |
You won't as form auth mechanism doesn't keep state, that is saved in a cookie and authenticity of the cookie is verified by the secret key. That's legit question though. |
For a reference, here is my attempt to implement logout:
|
Here's an example of clearing the cookie with the @Path("/logout")
@POST
public RestResponse<?> logout(@Context SecurityContext securityContext) {
if (securityContext.getUserPrincipal() != null) {
return RestResponse.ResponseBuilder.noContent()
.cookie(new NewCookie.Builder("quarkus-credential")
.maxAge(0)
.expiry(Date.from(Instant.EPOCH))
.path("/")
.build())
.build();
}
return RestResponse.ResponseBuilder.create(RestResponse.Status.BAD_REQUEST, "Not authenticated")
.build();
} |
Here is a RestEasy Classic version.. @Path("/api/logout")
public class LogoutResource {
@ConfigProperty(name = "quarkus.http.auth.form.cookie-name")
String cookieName;
@Inject
CurrentIdentityAssociation identity;
@POST
public Response logout() {
if (identity.getIdentity().isAnonymous()) {
throw new UnauthorizedException("Not authenticated");
}
final NewCookie removeCookie = new NewCookie.Builder(cookieName)
.maxAge(0)
.expiry(Date.from(Instant.EPOCH))
.path("/")
.build();
return Response.noContent().cookie(removeCookie).build();
}
} |
I propose to close this as logout is now documented #36818 by @melloware and IMO it is not necessary to provide anything OOTB. |
Yep let's close @tmulle |
ok yeah we can close this now that we have examples.. thanks everyone. |
We are using Quarkus with FORM based authentication and JDBC backend with the JSF extension so we can use MyFaces/Primefaces.
This is a great link to get Primefaces working on Quarkus!
https://github.com/melloware/quarkus-faces
Everything works fine and I can log in and see the cookie Quarkus-credential be created and all is well with the SecurityIdentity, etc.
Question is, how do I let the user logout? We need to have the ability to manually log out users from our website.
I tried the following code which I found on the web, but I'm suspecting this isn't working because there is no HTTPSession?
According to: https://quarkus.io/guides/security-built-in-authentication#form-auth only a cookie is created?
So, how do I let the user manually log out via a link on the webpage? Am I going to have to write some client-side javascript to manually delete the cookies? I don't want to solely rely on the cookie expirations, etc.
The text was updated successfully, but these errors were encountered: