Skip to content
This repository was archived by the owner on Feb 6, 2026. It is now read-only.

Commit 6970352

Browse files
authored
Merge pull request #1 from ModaOperandi/code_refactor
Code refactor
2 parents 1fa77cf + 6a00f7b commit 6970352

File tree

10 files changed

+193
-194
lines changed

10 files changed

+193
-194
lines changed

META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Manifest-Version: 1.0
2-
Implementation-Title: java-jwt-tools
2+
Implementation-Title: mo-jwt-java
33
Implementation-Version: 0.1.0
44

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# java-jwt-tools
22

3-
This is a library used to get access token(JWT) from your front end, decode it, and get user's Scopes, Groups, Roles, and Permissions information.
3+
This is a library currently used to get access token(JWT) from your front end, decode it, and get user's Scopes, Groups, Roles, and Permissions information.
44

55
To install the library, please add dependencies to your build file:
66

@@ -9,14 +9,16 @@ To install the library, please add dependencies to your build file:
99
}
1010

1111
dependencies {
12-
compile 'com.github.YangCaoModa:java-jwt-tools:v0.1.1'
12+
compile 'com.github.ModaOperandi:mo-jwt-java:v0.1.0'
1313
}
1414

1515

16-
To use JWTDecoder and JWTGetter in your controllers, please add these packages:
16+
To use JWTDecoder and JWTGetter in your classes, please add these packages:
1717

18-
import com.modaoperandi.java.jwt.tools.JWTGetter;
19-
import com.modaoperandi.java.jwt.tools.JWTDecoder;
18+
import com.modaoperandi.jwt.java.JWTGetter;
19+
import com.modaoperandi.jwt.java.JWTDecoder;
20+
21+
Here is an example codes to add in your controllers:
2022

2123
@Autowired
2224
private HttpServletRequest request;

build.gradle

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ plugins {
22
id 'java-library'
33
}
44

5-
repositories { mavenCentral() }
5+
repositories {
6+
mavenCentral()
7+
maven {
8+
url "https://jitpack.io"
9+
credentials { username 'jp_am1gb3ih8jb1jq01hdtbo1q23t' }
10+
}
11+
}
612

713
version = '0.1.0'
814

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
* in the user guide at https://docs.gradle.org/5.0/userguide/multi_project_builds.html
88
*/
99

10-
rootProject.name = 'java-jwt-tools'
10+
rootProject.name = 'mo-jwt-java'

src/main/java/com/modaoperandi/java/jwt/tools/JWTDecoder.java

Lines changed: 0 additions & 104 deletions
This file was deleted.

src/main/java/com/modaoperandi/java/jwt/tools/JWTGetter.java

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.modaoperandi.jwt.java;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
5+
import com.auth0.jwt.JWT;
6+
import com.auth0.jwt.exceptions.JWTDecodeException;
7+
import com.auth0.jwt.interfaces.DecodedJWT;
8+
9+
public class DecodedToken {
10+
11+
private static final String namespace = "https://perms.moda.com/";
12+
private String authToken;
13+
private String scopes;
14+
private String[] groups;
15+
private String[] roles;
16+
private String[] permissions;
17+
18+
public DecodedToken() {
19+
this.authToken = null;
20+
this.scopes = null;
21+
this.groups = null;
22+
this.roles = null;
23+
this.permissions = null;
24+
}
25+
26+
public DecodedToken(HttpServletRequest request) {
27+
helper(request);
28+
decodeClaims(this.authToken);
29+
}
30+
31+
public DecodedToken(String token) {
32+
this.authToken = token;
33+
decodeClaims(token);
34+
}
35+
36+
public String getToken() {
37+
return this.authToken;
38+
}
39+
40+
public String getScopes() {
41+
return this.scopes;
42+
}
43+
44+
public String[] getGroups() {
45+
return this.groups;
46+
}
47+
48+
public String[] getRoles() {
49+
return this.roles;
50+
}
51+
52+
public String[] getPermissions() {
53+
return this.permissions;
54+
}
55+
56+
public void printToken() {
57+
System.out.println("-------------ACCESS TOKEN-------------");
58+
System.out.println(this.authToken);
59+
System.out.println("-------------ACCESS TOKEN-------------");
60+
}
61+
62+
public void printScopes() {
63+
System.out.println("Scopes: " + this.scopes);
64+
}
65+
66+
public void printGroups() {
67+
System.out.println("Groups: " + arrayToString(this.groups));
68+
}
69+
70+
public void printRoles() {
71+
System.out.println("Roles: " + arrayToString(this.roles));
72+
}
73+
74+
public void printPermissions() {
75+
System.out.println("Permissions: " + arrayToString(this.permissions));
76+
}
77+
78+
public void printUserInfo() {
79+
try {
80+
printScopes();
81+
printGroups();
82+
printRoles();
83+
printPermissions();
84+
} catch (Exception e) {
85+
System.err.println(e.getMessage());
86+
}
87+
}
88+
89+
private void helper(HttpServletRequest request) {
90+
try {
91+
String header = request.getHeader("Authorization");
92+
this.authToken = header != null ? header.substring(7) : null;
93+
} catch (Exception e) {
94+
System.err.println(e.getMessage());
95+
}
96+
}
97+
98+
private void decodeClaims(String token) {
99+
try {
100+
DecodedJWT jwt = JWT.decode(token);
101+
this.scopes = jwt.getClaim("scope").asString();
102+
this.groups = jwt.getClaim(namespace + "groups").asArray(String.class);
103+
this.roles = jwt.getClaim(namespace + "roles").asArray(String.class);
104+
this.permissions = jwt.getClaim(namespace + "permissions").asArray(String.class);
105+
} catch (NullPointerException e) {
106+
System.out.println("Token is null.");
107+
} catch (JWTDecodeException e){
108+
System.out.println("Invalid token.");
109+
}
110+
}
111+
112+
private String arrayToString(String[] array) {
113+
StringBuilder sb = new StringBuilder();
114+
for (String str : array) {
115+
sb.append(str).append(" ");
116+
}
117+
return sb.toString().trim();
118+
}
119+
}

src/test/java/com/modaoperandi/java/jwt/tools/JWTDecoderTest.java

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/test/java/com/modaoperandi/java/jwt/tools/JWTGetterTest.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)