Skip to content
This repository was archived by the owner on May 22, 2021. It is now read-only.

Commit 0323663

Browse files
committed
Created a class to map JWTScopes (#18)
1 parent c899ff6 commit 0323663

4 files changed

Lines changed: 112 additions & 0 deletions

File tree

auth/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
<version>${project.parent.version}</version>
1919
<scope>compile</scope>
2020
</dependency>
21+
<dependency>
22+
<groupId>com.gewia.common</groupId>
23+
<artifactId>scope</artifactId>
24+
<version>${project.parent.version}</version>
25+
</dependency>
2126

2227
<dependency>
2328
<groupId>com.auth0</groupId>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.gewia.common.auth.jwt;
2+
3+
import com.gewia.common.util.Executor;
4+
import java.util.ArrayList;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import lombok.AccessLevel;
8+
import lombok.Getter;
9+
import lombok.NoArgsConstructor;
10+
11+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
12+
public class JwtScopes {
13+
14+
@Getter private List<String> scopes = new ArrayList<>();
15+
private boolean containing = false;
16+
17+
public JwtScopes(List<String> scopes) {
18+
this.scopes = Collections.unmodifiableList(scopes);
19+
}
20+
21+
public JwtScopes includes(String scope, Executor executor) {
22+
if (this.scopes.contains(scope)) {
23+
containing = true;
24+
if (executor != null) executor.action();
25+
} else containing = false;
26+
27+
return this;
28+
}
29+
30+
public void orElse(Executor executor) {
31+
if (!containing && executor != null) executor.action();
32+
containing = false;
33+
}
34+
35+
public boolean getResult() {
36+
return containing;
37+
}
38+
39+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.gewia.common.auth.jwt;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.atomic.AtomicInteger;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
public class JwtScopesTest {
10+
11+
@Test
12+
public void testScopesUnmodifiable() {
13+
JwtScopes jwtScopes = this.getExampleJwtScopes();
14+
15+
Assert.assertThrows(UnsupportedOperationException.class, () -> jwtScopes.getScopes().add("another.example.scope"));
16+
}
17+
18+
@Test
19+
public void testResultOperation() {
20+
JwtScopes jwtScopes = this.getExampleJwtScopes();
21+
22+
Assert.assertTrue(jwtScopes.includes("microservice.topic.mode.limitation.extra", null).getResult());
23+
Assert.assertFalse(jwtScopes.includes("another.exmaple.scope", null).getResult());
24+
}
25+
26+
@Test
27+
public void testOrElseOperation() {
28+
JwtScopes jwtScopes = this.getExampleJwtScopes();
29+
30+
AtomicInteger i = new AtomicInteger();
31+
jwtScopes.includes("another.example.scope", i::getAndIncrement).orElse(i::getAndDecrement);
32+
Assert.assertEquals(-1, i.get());
33+
}
34+
35+
@Test
36+
public void testIncludeOperation() {
37+
JwtScopes jwtScopes = this.getExampleJwtScopes();
38+
39+
AtomicInteger i = new AtomicInteger();
40+
jwtScopes.includes("microservice.topic.mode.limitation.extra", i::getAndIncrement).orElse(i::getAndDecrement);
41+
Assert.assertEquals(1, i.get());
42+
}
43+
44+
@Test
45+
public void testInitializing() {
46+
List<String> exampleScopes = new ArrayList<>();
47+
exampleScopes.add("microservice.topic.mode.limitation.extra");
48+
JwtScopes jwtScopes = new JwtScopes(exampleScopes);
49+
50+
Assert.assertEquals(exampleScopes, jwtScopes.getScopes());
51+
}
52+
53+
private JwtScopes getExampleJwtScopes() {
54+
List<String> exampleScopes = new ArrayList<>();
55+
exampleScopes.add("microservice.topic.mode.limitation.extra");
56+
57+
return new JwtScopes(exampleScopes);
58+
}
59+
60+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.gewia.common.util;
2+
3+
@FunctionalInterface
4+
public interface Executor {
5+
6+
void action();
7+
8+
}

0 commit comments

Comments
 (0)