Skip to content

Commit

Permalink
Merge pull request #22 from wwan13/feature/auth
Browse files Browse the repository at this point in the history
refactor : Rename module authorizedRequest to authPatterns
  • Loading branch information
wwan13 authored Jun 6, 2024
2 parents 4732c71 + d0c7b74 commit 3a40b43
Show file tree
Hide file tree
Showing 18 changed files with 85 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* limitations under the License.
*/

package io.wwan13.wintersecurity.auth.authorizedrequest;
package io.wwan13.wintersecurity.auth.authpattern;

import org.springframework.http.HttpMethod;

import java.util.Map;
import java.util.Set;

public record AuthorizedRequest(
public record AuthPatterns(
Map<Requests, Permissions> registered,
boolean isElseRequestPermit
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.wwan13.wintersecurity.auth.authorizedrequest;
package io.wwan13.wintersecurity.auth.authpattern;

import io.wwan13.wintersecurity.constant.DefaultAuthPattern;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.wwan13.wintersecurity.auth.authorizedrequest;
package io.wwan13.wintersecurity.auth.authpattern;

import org.springframework.http.HttpMethod;
import org.springframework.util.AntPathMatcher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
* limitations under the License.
*/

package io.wwan13.wintersecurity.auth.authorizedrequest.support;
package io.wwan13.wintersecurity.auth.authpattern.support;

import io.wwan13.wintersecurity.auth.authorizedrequest.AuthorizedRequest;
import io.wwan13.wintersecurity.auth.authpattern.AuthPatterns;

public class AuthorizedRequestApplier {
public class AuthPatternsApplier {

private AuthorizedRequestApplier() {
private AuthPatternsApplier() {
throw new IllegalStateException("Cannot instantiate a utility class!");
}

public static AuthorizedRequest apply(AuthorizedRequestRegistry registry) {
public static AuthPatterns apply(AuthPatternsRegistry registry) {
return registry.apply();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* limitations under the License.
*/

package io.wwan13.wintersecurity.auth.authorizedrequest.support;
package io.wwan13.wintersecurity.auth.authpattern.support;

import io.wwan13.wintersecurity.auth.authorizedrequest.Permissions;
import io.wwan13.wintersecurity.auth.authorizedrequest.AuthorizedRequest;
import io.wwan13.wintersecurity.auth.authorizedrequest.Requests;
import io.wwan13.wintersecurity.auth.authpattern.Permissions;
import io.wwan13.wintersecurity.auth.authpattern.AuthPatterns;
import io.wwan13.wintersecurity.auth.authpattern.Requests;
import io.wwan13.wintersecurity.constant.RegistryOptions;
import org.springframework.http.HttpMethod;

Expand All @@ -27,21 +27,21 @@
import java.util.Set;
import java.util.stream.Collectors;

public class AuthorizedRequestRegistry {
public class AuthPatternsRegistry {

private final Map<Requests, Permissions> registered;
private boolean isElseRequestPermit;

public AuthorizedRequestRegistry(
public AuthPatternsRegistry(
Map<Requests, Permissions> registered,
boolean isElseRequestPermit
) {
this.registered = registered;
this.isElseRequestPermit = isElseRequestPermit;
}

public static AuthorizedRequestRegistry of() {
return new AuthorizedRequestRegistry(
public static AuthPatternsRegistry of() {
return new AuthPatternsRegistry(
new LinkedHashMap<>(),
RegistryOptions.DEFAULT_ELSE_REQUEST_OPTION
);
Expand All @@ -59,16 +59,16 @@ public void elseRequestAuthenticated() {
this.isElseRequestPermit = false;
}

protected AuthorizedRequest apply() {
return new AuthorizedRequest(registered, isElseRequestPermit);
protected AuthPatterns apply() {
return new AuthPatterns(registered, isElseRequestPermit);
}

public static class HttpMethodAppender {

private final AuthorizedRequestRegistry registry;
private final AuthPatternsRegistry registry;
private final Set<String> uriPatterns;

HttpMethodAppender(AuthorizedRequestRegistry registry, Set<String> uriPatterns) {
HttpMethodAppender(AuthPatternsRegistry registry, Set<String> uriPatterns) {
this.registry = registry;
this.uriPatterns = uriPatterns;
}
Expand Down Expand Up @@ -104,12 +104,12 @@ public RolesAppender httpMethodDelete() {

public static class RolesAppender {

private final AuthorizedRequestRegistry registry;
private final AuthPatternsRegistry registry;
private final Set<String> uriPatterns;
private final Set<HttpMethod> httpMethods;

RolesAppender(
AuthorizedRequestRegistry registry,
AuthPatternsRegistry registry,
Set<String> uriPatterns,
Set<HttpMethod> httpMethods
) {
Expand All @@ -118,17 +118,17 @@ public static class RolesAppender {
this.httpMethods = httpMethods;
}

public AuthorizedRequestRegistry permitAll() {
public AuthPatternsRegistry permitAll() {
appendRolesPerPattern(RegistryOptions.ALL_ROLES);
return registry;
}

public AuthorizedRequestRegistry authenticated() {
public AuthPatternsRegistry authenticated() {
appendRolesPerPattern(RegistryOptions.EMPTY_ROLES);
return registry;
}

public AuthorizedRequestRegistry hasRoles(Object... roles) {
public AuthPatternsRegistry hasRoles(Object... roles) {
appendRolesPerPattern(Set.of(roles));
return registry;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package io.wwan13.wintersecurity.auth.provider;

import io.wwan13.wintersecurity.auth.RequestAccessManager;
import io.wwan13.wintersecurity.auth.authorizedrequest.AuthorizedRequest;
import io.wwan13.wintersecurity.auth.authpattern.AuthPatterns;
import io.wwan13.wintersecurity.constant.DefaultAuthPattern;
import io.wwan13.wintersecurity.exception.forbidden.ForbiddenException;
import io.wwan13.wintersecurity.exception.unauthirized.UnauthorizedException;
Expand All @@ -28,26 +28,26 @@

public class HttpRequestAccessManager implements RequestAccessManager {

private final AuthorizedRequest authorizedRequest;
private final AuthPatterns authPatterns;

public HttpRequestAccessManager(AuthorizedRequest authorizedRequest) {
this.authorizedRequest = authorizedRequest;
public HttpRequestAccessManager(AuthPatterns authPatterns) {
this.authPatterns = authPatterns;
}

public void manageWithAuthentication(
HttpMethod method,
String uri,
Set<String> roles
) {
if (!authorizedRequest.isAccessibleRequest(method, uri, roles)) {
if (!authPatterns.isAccessibleRequest(method, uri, roles)) {
throw new ForbiddenException();
}
}

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

if (!authorizedRequest.isAccessibleRequest(method, uri, role)) {
if (!authPatterns.isAccessibleRequest(method, uri, role)) {
throw new UnauthorizedException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import io.wwan13.wintersecurity.auth.RequestAccessManager;
import io.wwan13.wintersecurity.auth.TokenExtractor;
import io.wwan13.wintersecurity.auth.authorizedrequest.AuthorizedRequest;
import io.wwan13.wintersecurity.auth.authpattern.AuthPatterns;
import io.wwan13.wintersecurity.auth.processor.AbstractInterceptorAuthProcessor;
import io.wwan13.wintersecurity.auth.processor.InterceptorAuthProcessor;
import io.wwan13.wintersecurity.auth.provider.BearerTokenExtractor;
Expand All @@ -45,8 +45,8 @@ public TokenExtractor tokenExtractor() {
}

@Bean
public RequestAccessManager requestAccessManager(AuthorizedRequest authorizedRequest) {
return new HttpRequestAccessManager(authorizedRequest);
public RequestAccessManager requestAccessManager(AuthPatterns authPatterns) {
return new HttpRequestAccessManager(authPatterns);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

package io.wwan13.wintersecurity.config;

import io.wwan13.wintersecurity.auth.authorizedrequest.AuthorizedRequest;
import io.wwan13.wintersecurity.auth.authorizedrequest.support.AuthorizedRequestApplier;
import io.wwan13.wintersecurity.auth.authorizedrequest.support.AuthorizedRequestRegistry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import io.wwan13.wintersecurity.auth.authpattern.AuthPatterns;
import io.wwan13.wintersecurity.auth.authpattern.support.AuthPatternsApplier;
import io.wwan13.wintersecurity.auth.authpattern.support.AuthPatternsRegistry;
import org.springframework.context.annotation.Bean;

public class AuthorizedRequestRegistrar {
Expand All @@ -31,9 +30,9 @@ public AuthorizedRequestRegistrar(SecureRequestConfigurer secureRequestConfigure
}

@Bean
public AuthorizedRequest authorizedRequest() {
AuthorizedRequestRegistry registry = AuthorizedRequestRegistry.of();
public AuthPatterns authorizedRequest() {
AuthPatternsRegistry registry = AuthPatternsRegistry.of();
secureRequestConfigurer.registerAuthPatterns(registry);
return AuthorizedRequestApplier.apply(registry);
return AuthPatternsApplier.apply(registry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package io.wwan13.wintersecurity.config;

import io.wwan13.wintersecurity.auth.authorizedrequest.support.AuthorizedRequestRegistry;
import io.wwan13.wintersecurity.auth.authpattern.support.AuthPatternsRegistry;
import io.wwan13.wintersecurity.resolve.support.TargetAnnotationsRegistry;

public interface SecureRequestConfigurer extends SecretKeyConfigurer {

void registerAuthPatterns(AuthorizedRequestRegistry registry);
void registerAuthPatterns(AuthPatternsRegistry registry);

void registerTargetAnnotations(TargetAnnotationsRegistry registry);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* limitations under the License.
*/

package io.wwan13.wintersecurity.auth.authorizedrequest;
package io.wwan13.wintersecurity.auth.authpattern;

import io.wwan13.wintersecurity.UnitTest;
import io.wwan13.wintersecurity.auth.authorizedrequest.support.AuthorizedRequestApplier;
import io.wwan13.wintersecurity.auth.authorizedrequest.support.AuthorizedRequestRegistry;
import io.wwan13.wintersecurity.auth.authpattern.support.AuthPatternsApplier;
import io.wwan13.wintersecurity.auth.authpattern.support.AuthPatternsRegistry;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.springframework.http.HttpMethod;
Expand All @@ -29,7 +29,7 @@
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;

class AuthorizedRequestTest extends UnitTest {
class AuthPatternsTest extends UnitTest {

@ParameterizedTest
@CsvSource({
Expand All @@ -48,7 +48,7 @@ void should_JudgeRequestIsAccessible(
final boolean expected
) {
// given
AuthorizedRequestRegistry registry = AuthorizedRequestRegistry.of();
AuthPatternsRegistry registry = AuthPatternsRegistry.of();
registry
.uriPatterns("/api/admin/**")
.httpMethods(GET, POST)
Expand All @@ -72,10 +72,10 @@ void should_JudgeRequestIsAccessible(

.elseRequestAuthenticated();

AuthorizedRequest authorizedRequest = AuthorizedRequestApplier.apply(registry);
AuthPatterns authPatterns = AuthPatternsApplier.apply(registry);

// when
boolean result = authorizedRequest
boolean result = authPatterns
.isAccessibleRequest(HttpMethod.resolve(requestMethod), requestUri, Set.of(requestRole));

// then
Expand All @@ -97,18 +97,18 @@ void should_AcceptUnregisteredRequest_when_AnyRequestPermitAll(
final boolean expected
) {
// given
AuthorizedRequestRegistry registry = AuthorizedRequestRegistry.of();
AuthPatternsRegistry registry = AuthPatternsRegistry.of();
registry
.uriPatterns("/api/test")
.httpMethods(GET)
.hasRoles("ROLE_ADMIN")

.elseRequestPermit();

AuthorizedRequest authorizedRequest = AuthorizedRequestApplier.apply(registry);
AuthPatterns authPatterns = AuthPatternsApplier.apply(registry);

// when
boolean result = authorizedRequest
boolean result = authPatterns
.isAccessibleRequest(HttpMethod.resolve(requestMethod), requestUri, Set.of(requestRole));

// then
Expand All @@ -131,18 +131,18 @@ void should_BlockUnregisteredRequest_when_AnyRequestAuthenticated(
final boolean expected
) {
// given
AuthorizedRequestRegistry registry = AuthorizedRequestRegistry.of();
AuthPatternsRegistry registry = AuthPatternsRegistry.of();
registry
.uriPatterns("/api/test")
.httpMethods(GET)
.hasRoles("ROLE_ADMIN")

.elseRequestAuthenticated();

AuthorizedRequest authorizedRequest = AuthorizedRequestApplier.apply(registry);
AuthPatterns authPatterns = AuthPatternsApplier.apply(registry);

// when
boolean result = authorizedRequest
boolean result = authPatterns
.isAccessibleRequest(HttpMethod.resolve(requestMethod), requestUri, Set.of(requestRole));

// then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.wwan13.wintersecurity.auth.authorizedrequest;
package io.wwan13.wintersecurity.auth.authpattern;

import io.wwan13.wintersecurity.UnitTest;
import io.wwan13.wintersecurity.constant.DefaultAuthPattern;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.wwan13.wintersecurity.auth.authorizedrequest;
package io.wwan13.wintersecurity.auth.authpattern;

import io.wwan13.wintersecurity.UnitTest;
import org.junit.jupiter.api.Test;
Expand Down
Loading

0 comments on commit 3a40b43

Please sign in to comment.