Skip to content

Commit

Permalink
zalando#951 Replace AuthMode.OFF by AuthMode.NONE
Browse files Browse the repository at this point in the history
  • Loading branch information
fbrns committed Nov 9, 2018
1 parent 80f23d7 commit 75e1c35
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private static Status fromStatusCode(final int code) throws UnknownStatusCodeExc
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(final WebSecurity web) throws Exception {
if (settings.getAuthMode() == SecuritySettings.AuthMode.OFF) {
if (settings.getAuthMode().isNoAuthentication()) {
web.ignoring().anyRequest();
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/zalando/nakadi/config/SecuritySettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ public class SecuritySettings {

public enum AuthMode {
OFF, // no authentication at all
NONE, // same as OFF
BASIC, // only checks that the token is valid (has "uid" scope)
REALM, // checks that the token is valid and contains at least one required realm
FULL // full authentication and authorization using oauth2 scopes
;

public boolean isNoAuthentication() {
return AuthMode.OFF == this || AuthMode.NONE == this;
}
}

private final String tokenInfoUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import java.util.Map;
import java.util.Optional;

import static org.zalando.nakadi.config.SecuritySettings.AuthMode.OFF;

@Component
public class ClientResolver implements HandlerMethodArgumentResolver {

Expand All @@ -44,7 +42,7 @@ public Client resolveArgument(final MethodParameter parameter,
final WebDataBinderFactory binderFactory) throws Exception {
final Optional<String> clientId = Optional.ofNullable(request.getUserPrincipal()).map(Principal::getName);
if (clientId.filter(settings.getAdminClientId()::equals).isPresent()
|| settings.getAuthMode() == OFF) {
|| settings.getAuthMode().isNoAuthentication()) {
return new FullAccessClient(clientId.orElse(FULL_ACCESS_CLIENT_ID));
}

Expand Down
28 changes: 28 additions & 0 deletions src/test/java/org/zalando/nakadi/config/SecuritySettingsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.zalando.nakadi.config;

import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class SecuritySettingsTest {

@Test
public void doesNotRequireAuthentication() {
Assert.assertTrue(SecuritySettings.AuthMode.NONE.isNoAuthentication());
Assert.assertTrue(SecuritySettings.AuthMode.OFF.isNoAuthentication());
}

@Test
public void mustRequireAuthentication() {
final List<SecuritySettings.AuthMode> authModesWithoutAuthentication =
new LinkedList<>(Arrays.asList(SecuritySettings.AuthMode.values()));
authModesWithoutAuthentication.remove(SecuritySettings.AuthMode.NONE);
authModesWithoutAuthentication.remove(SecuritySettings.AuthMode.OFF);
authModesWithoutAuthentication.forEach(authMode ->
Assert.assertFalse(authMode.isNoAuthentication()));
}

}

0 comments on commit 75e1c35

Please sign in to comment.