Skip to content

Commit

Permalink
cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Gorzala committed Dec 17, 2023
1 parent 0a91c86 commit ff4945c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.dancier.dancer.authentication.event.NewUserCreatedEvent;
import net.dancier.dancer.authentication.model.*;
import net.dancier.dancer.authentication.repository.*;
import net.dancier.dancer.core.config.CookieConfiguration;
import net.dancier.dancer.core.exception.ApplicationException;
import net.dancier.dancer.core.exception.BusinessException;
import net.dancier.dancer.core.exception.NotFoundException;
Expand Down Expand Up @@ -55,6 +56,7 @@ public class AuthenticationService {
private final VerifiedActionCodeRepository verifiedActionCodeRepository;
private final String frontendBaseName;
private final ApplicationEventPublisher applicationEventPublisher;
private final CookieConfiguration cookieConfiguration;

public Authentication authenticate(Authentication authentication) {
return this.authenticationManager.authenticate(authentication);
Expand All @@ -70,10 +72,10 @@ public String generateJwtToken(String subject) {
public ResponseCookie generateCookie(String token) {
return ResponseCookie.from("jwt-token", token)
.maxAge(Duration.ofDays(30))
.secure(true)
.secure(cookieConfiguration.getSecure())
.httpOnly(true)
.path("/")
.sameSite("None")
.sameSite(cookieConfiguration.getSameSite())
.build();
}

Expand All @@ -83,12 +85,7 @@ public ResponseCookie generateCookie(String token) {
*/
public ResponseCookie generateClearingCookie() {
return ResponseCookie.from("jwt-token", "")
.maxAge(Duration.ofDays(0))
.secure(true)
.httpOnly(true)
.path("/")
.sameSite("None")
.build();
.build();
}

public User getUser(UUID userId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.dancier.dancer.core.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;


@Data
@Component
@ConfigurationProperties(prefix = "app.cookie")
public class CookieConfiguration {
private Boolean secure;
private String sameSite;
}
3 changes: 3 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ app:
auth:
tokenSecret: 04ca023b39512e46d0c2cf4b48d5aac61d34302994c87ed4eff225dcf3b0a218739f3897051a057f9b846a69ea2927a587044164b7bae5e1306219d50b588cb1
tokenExpirationMsec: 864000000
cookie:
secure: true # otherwise overwrite it via env-var (APP_COOKIE_SECURE)
sameSite: Strict # other values needs to be overwritten by env vars could be: [Strict|Lax|None]
cors:
allowedOrigins: http://localhost:4200,http://localho.st:4200 # Comma separated list of allowed origins
# differentiate between success and failure
Expand Down

0 comments on commit ff4945c

Please sign in to comment.