summaryrefslogtreecommitdiff
path: root/eaaf-springboot-utils/src
diff options
context:
space:
mode:
Diffstat (limited to 'eaaf-springboot-utils/src')
-rw-r--r--eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/security/BasicAuthUser.java56
-rw-r--r--eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/security/IBasicAuthUserCollector.java24
-rw-r--r--eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/utils/BasicAuthUserTest.java41
3 files changed, 121 insertions, 0 deletions
diff --git a/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/security/BasicAuthUser.java b/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/security/BasicAuthUser.java
new file mode 100644
index 00000000..7644c7ce
--- /dev/null
+++ b/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/security/BasicAuthUser.java
@@ -0,0 +1,56 @@
+package at.gv.egiz.eaaf.utils.springboot.security;
+
+import java.util.List;
+
+import org.apache.commons.lang3.StringUtils;
+
+import lombok.Builder;
+import lombok.Getter;
+import lombok.Singular;
+
+/**
+ * Simple user configuration object.
+ *
+ * @author tlenz
+ *
+ */
+@Builder
+@Getter
+public class BasicAuthUser {
+
+ /**
+ * Username of an entity.
+ */
+ private final String username;
+
+ /**
+ * Password of an entity.
+ */
+ private final String password;
+
+ /**
+ * Roles of an entity.
+ */
+ @Singular
+ private List<String> roles;
+
+ /**
+ * Get roles as Array.
+ *
+ * @return Array of roles
+ */
+ public String[] getRolesArray() {
+ return roles.stream().toArray(String[]::new);
+ }
+
+ /**
+ * Check if username, password are not blank and roles is not empty.
+ *
+ * @return <code>true</code> if it looks like a valid user
+ */
+ public boolean isValid() {
+ return StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password) && !roles.isEmpty();
+
+ }
+
+}
diff --git a/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/security/IBasicAuthUserCollector.java b/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/security/IBasicAuthUserCollector.java
new file mode 100644
index 00000000..1986d52b
--- /dev/null
+++ b/eaaf-springboot-utils/src/main/java/at/gv/egiz/eaaf/utils/springboot/security/IBasicAuthUserCollector.java
@@ -0,0 +1,24 @@
+package at.gv.egiz.eaaf.utils.springboot.security;
+
+import java.util.List;
+
+import javax.annotation.Nonnull;
+
+/**
+ * Extension interface to collect Spring Security Basic Auth. users in
+ * multi-module projects.
+ *
+ * @author tlenz
+ *
+ */
+public interface IBasicAuthUserCollector {
+
+ /**
+ * Get all users that shall be added for Spring Security Basic-Auth.
+ *
+ * @return
+ */
+ @Nonnull
+ List<BasicAuthUser> getEnabledUsers();
+
+}
diff --git a/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/utils/BasicAuthUserTest.java b/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/utils/BasicAuthUserTest.java
new file mode 100644
index 00000000..95bd5dcb
--- /dev/null
+++ b/eaaf-springboot-utils/src/test/java/at/gv/egiz/eaaf/utils/springboot/test/utils/BasicAuthUserTest.java
@@ -0,0 +1,41 @@
+package at.gv.egiz.eaaf.utils.springboot.test.utils;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.commons.lang3.RandomStringUtils;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.BlockJUnit4ClassRunner;
+
+import at.gv.egiz.eaaf.utils.springboot.security.BasicAuthUser;
+
+@RunWith(BlockJUnit4ClassRunner.class)
+public class BasicAuthUserTest {
+
+ @Test
+ public void simpleTest() {
+ assertFalse("username",
+ BasicAuthUser.builder().password(rand()).role(rand()).build().isValid());
+ assertFalse("password",
+ BasicAuthUser.builder().username(rand()).role(rand()).build().isValid());
+ assertFalse("role",
+ BasicAuthUser.builder().password(rand()).username(rand()).build().isValid());
+
+ assertTrue("valid",
+ BasicAuthUser.builder().username(rand()).password(rand()).role(rand()).build().isValid());
+
+ assertEquals("role size", 2,
+ BasicAuthUser.builder().username(rand()).password(rand())
+ .role(rand()).role(rand()).build()
+ .getRolesArray().length);
+
+ }
+
+ private String rand() {
+ return RandomStringUtils.randomAlphanumeric(5);
+
+ }
+
+}