-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from kaizerpwn/feature/auth
Authentication using httpOnly cookie
- Loading branch information
Showing
7 changed files
with
193 additions
and
2 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
50 changes: 50 additions & 0 deletions
50
app/src/main/java/com/ibrahimokic/ordermanagement/security/JwtAuthenticationFilter.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,50 @@ | ||
package com.ibrahimokic.ordermanagement.security; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class JwtAuthenticationFilter extends OncePerRequestFilter { | ||
private final JwtDecoder jwtDecoder; | ||
private final JwtToPrincipalConverter jwtToPrincipalConverter; | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
extractTokenFromRequest(request) | ||
.map(jwtDecoder::decode) | ||
.map(jwtToPrincipalConverter::convert) | ||
.map(UserPrincipalAuthenticationToken::new) | ||
.ifPresent(authentication -> SecurityContextHolder.getContext().setAuthentication(authentication)); | ||
|
||
filterChain.doFilter(request, response); | ||
} | ||
|
||
public Optional<String> extractTokenFromRequest(HttpServletRequest request) { | ||
Cookie[] cookies = request.getCookies(); | ||
|
||
if (cookies != null) { | ||
Optional<Cookie> accessTokenCookie = Arrays.stream(cookies) | ||
.filter(cookie -> "accessToken".equals(cookie.getName())) | ||
.findFirst(); | ||
|
||
if (accessTokenCookie.isPresent()) { | ||
return Optional.ofNullable(accessTokenCookie.get().getValue()); | ||
} | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/ibrahimokic/ordermanagement/security/JwtDecoder.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,20 @@ | ||
package com.ibrahimokic.ordermanagement.security; | ||
|
||
import com.auth0.jwt.JWT; | ||
import com.auth0.jwt.algorithms.Algorithm; | ||
import com.auth0.jwt.interfaces.DecodedJWT; | ||
import com.ibrahimokic.ordermanagement.config.JwtConfig; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class JwtDecoder { | ||
private final JwtConfig jwtConfig; | ||
public DecodedJWT decode(String token) { | ||
return JWT | ||
.require(Algorithm.HMAC256(jwtConfig.getSecretKey())) | ||
.build() | ||
.verify(token); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
app/src/main/java/com/ibrahimokic/ordermanagement/security/JwtToPrincipalConverter.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,27 @@ | ||
package com.ibrahimokic.ordermanagement.security; | ||
|
||
import com.auth0.jwt.interfaces.DecodedJWT; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
@Component | ||
public class JwtToPrincipalConverter { | ||
public UserPrincipal convert(DecodedJWT jwt) { | ||
return UserPrincipal.builder() | ||
.userId(Long.valueOf(jwt.getSubject())) | ||
.email(jwt.getClaim("email").asString()) | ||
.authorities(extractAuthoritiesFromClaim(jwt)) | ||
.build(); | ||
} | ||
|
||
private List<SimpleGrantedAuthority> extractAuthoritiesFromClaim(DecodedJWT jwt) { | ||
var claim = jwt.getClaim("role"); | ||
|
||
if(claim.isNull() || claim.isMissing()) | ||
return List.of(); | ||
|
||
return claim.asList(SimpleGrantedAuthority.class); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
app/src/main/java/com/ibrahimokic/ordermanagement/security/UserPrincipal.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,51 @@ | ||
package com.ibrahimokic.ordermanagement.security; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
|
||
import java.util.Collection; | ||
|
||
@Data | ||
@Builder | ||
public class UserPrincipal implements UserDetails { | ||
private final Long userId; | ||
private final String email; | ||
private final Collection<? extends GrantedAuthority> authorities; | ||
|
||
@Override | ||
public Collection<? extends GrantedAuthority> getAuthorities() { | ||
return authorities; | ||
} | ||
|
||
@Override | ||
public String getPassword() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getUsername() { | ||
return email; | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonExpired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonLocked() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isCredentialsNonExpired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return true; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
.../main/java/com/ibrahimokic/ordermanagement/security/UserPrincipalAuthenticationToken.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,23 @@ | ||
package com.ibrahimokic.ordermanagement.security; | ||
|
||
import org.springframework.security.authentication.AbstractAuthenticationToken; | ||
|
||
public class UserPrincipalAuthenticationToken extends AbstractAuthenticationToken { | ||
private final UserPrincipal principal; | ||
|
||
public UserPrincipalAuthenticationToken(UserPrincipal principal) { | ||
super(principal.getAuthorities()); | ||
this.principal = principal; | ||
setAuthenticated(true); | ||
} | ||
|
||
@Override | ||
public Object getCredentials() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public UserPrincipal getPrincipal() { | ||
return principal; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
app/src/main/java/com/ibrahimokic/ordermanagement/security/WebSecurityConfig.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