Skip to content
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

Feature/refactor #39

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading