Skip to content

Commit

Permalink
Merge pull request #39 from wwan13/feature/refactor
Browse files Browse the repository at this point in the history
Feature/refactor
  • Loading branch information
wwan13 authored Jul 16, 2024
2 parents 001a995 + c9883f6 commit f8d4800
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = 'io.wwan13'
version = '0.0.5'
version = '0.0.9'

java {
sourceCompatibility = '17'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ public interface RequestAccessManager {
void manageWithAuthentication(HttpMethod method, String uri, Set<String> roles);

void manageWithoutAuthentication(HttpMethod method, String uri);

boolean isUnsecuredRequest(HttpMethod method, String uri);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import io.wwan13.wintersecurity.auth.RequestStorage;
import io.wwan13.wintersecurity.auth.TokenExtractor;
import io.wwan13.wintersecurity.constant.Constants;
import io.wwan13.wintersecurity.exception.unauthirized.ExpiredJwtTokenException;
import io.wwan13.wintersecurity.exception.unauthirized.InvalidJwtTokenException;
import io.wwan13.wintersecurity.jwt.TokenClaims;
import io.wwan13.wintersecurity.jwt.TokenDecoder;
import org.springframework.http.HttpMethod;
Expand Down Expand Up @@ -56,15 +58,24 @@ private void actionIfTokenPresent(
HttpServletRequest request,
RequestStorage storage
) {
TokenClaims claims = tokenDecoder.decode(token);
try {
TokenClaims claims = tokenDecoder.decode(token);

accessManager.manageWithAuthentication(
HttpMethod.resolve(request.getMethod()),
request.getRequestURI(),
claims.getRoles()
);
accessManager.manageWithAuthentication(
HttpMethod.resolve(request.getMethod()),
request.getRequestURI(),
claims.getRoles()
);

storage.save(Constants.ATTRIBUTE_CLAIMS_KEY, claims);
} catch (InvalidJwtTokenException | ExpiredJwtTokenException e) {
HttpMethod method = HttpMethod.resolve(request.getMethod());
String uri = request.getRequestURI();

storage.save(Constants.ATTRIBUTE_CLAIMS_KEY, claims);
if (!accessManager.isUnsecuredRequest(method, uri)) {
throw e;
}
}
}

private void actionIfTokenAbsent(HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public HttpRequestAccessManager(AuthPatterns authPatterns) {
this.authPatterns = authPatterns;
}

@Override
public void manageWithAuthentication(
HttpMethod method,
String uri,
Expand All @@ -44,11 +45,16 @@ public void manageWithAuthentication(
}
}

@Override
public void manageWithoutAuthentication(HttpMethod method, String uri) {
Set<String> role = Collections.singleton(DefaultAuthPattern.ANONYMOUS_ROLE);

if (!authPatterns.isAccessibleRequest(method, uri, role)) {
if (!isUnsecuredRequest(method, uri)) {
throw new UnauthorizedException();
}
}

@Override
public boolean isUnsecuredRequest(HttpMethod method, String uri) {
Set<String> role = Collections.singleton(DefaultAuthPattern.ANONYMOUS_ROLE);
return authPatterns.isAccessibleRequest(method, uri, role);
}
}

0 comments on commit f8d4800

Please sign in to comment.