Skip to content

Commit eff57d4

Browse files
committed
feat: Implementing JWT auth to API
Issue: https://github.com/GeovaniTech/InvestMe.git
1 parent 2f3bd02 commit eff57d4

File tree

7 files changed

+101
-0
lines changed

7 files changed

+101
-0
lines changed

server-investme-api/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,6 @@ nb-configuration.xml
4242
# Plugin directory
4343
/.quarkus/cli/plugins/
4444
application.properties
45+
publicKey.pem
46+
privateKey.pem
47+
rsaPrivateKey.pem

server-investme-api/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@
7777
<groupId>io.quarkus</groupId>
7878
<artifactId>quarkus-rest-jackson</artifactId>
7979
</dependency>
80+
81+
<dependency>
82+
<groupId>io.quarkus</groupId>
83+
<artifactId>quarkus-smallrye-jwt</artifactId>
84+
</dependency>
85+
86+
<dependency>
87+
<groupId>io.quarkus</groupId>
88+
<artifactId>quarkus-smallrye-jwt-build</artifactId>
89+
</dependency>
8090
</dependencies>
8191

8292
<build>

server-investme-api/src/main/java/br/com/devpree/investme/api/category/wsrest/WSCategory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import br.com.devpree.investme.api.category.service.CategoryService;
77
import br.com.devpree.investme.api.category.transferobject.TOCategoryRestModel;
8+
import jakarta.annotation.security.RolesAllowed;
89
import jakarta.inject.Inject;
910
import jakarta.ws.rs.Consumes;
1011
import jakarta.ws.rs.GET;
@@ -24,6 +25,7 @@ public class WSCategory implements Serializable {
2425
@GET
2526
@Consumes(MediaType.APPLICATION_JSON)
2627
@Produces(MediaType.APPLICATION_JSON)
28+
@RolesAllowed({"User"})
2729
@Path("/list")
2830
public List<TOCategoryRestModel> list(@QueryParam("email") String email) {
2931
return categoryService.list(email);

server-investme-api/src/main/java/br/com/devpree/investme/api/payments/wsrest/WSPayment.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import br.com.devpree.investme.api.payments.service.PaymentService;
77
import br.com.devpree.investme.api.payments.transferobject.TOPaymentRestModel;
8+
import jakarta.annotation.security.RolesAllowed;
89
import jakarta.inject.Inject;
910
import jakarta.ws.rs.Consumes;
1011
import jakarta.ws.rs.GET;
@@ -24,6 +25,7 @@ public class WSPayment implements Serializable {
2425
@GET
2526
@Consumes(MediaType.APPLICATION_JSON)
2627
@Produces(MediaType.APPLICATION_JSON)
28+
@RolesAllowed({"User"})
2729
@Path("/list")
2830
public List<TOPaymentRestModel> list(@QueryParam("email") String email) {
2931
return paymentService.list(email);

server-investme-api/src/main/java/br/com/devpree/investme/api/transaction/wsrest/WSTransaction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import br.com.devpree.investme.api.transaction.service.TransactionService;
77
import br.com.devpree.investme.api.transaction.transferobject.TOTransactionRestModel;
8+
import jakarta.annotation.security.RolesAllowed;
89
import jakarta.inject.Inject;
910
import jakarta.ws.rs.Consumes;
1011
import jakarta.ws.rs.GET;
@@ -24,6 +25,7 @@ public class WSTransaction implements Serializable {
2425
@GET
2526
@Consumes(MediaType.APPLICATION_JSON)
2627
@Produces(MediaType.APPLICATION_JSON)
28+
@RolesAllowed({"User"})
2729
@Path("/list")
2830
public List<TOTransactionRestModel> list(@QueryParam("email") String email) {
2931
return transactionService.list(email);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.acme.security.jwt;
2+
3+
import java.util.Arrays;
4+
import java.util.Date;
5+
import java.util.HashSet;
6+
7+
import org.eclipse.microprofile.jwt.Claims;
8+
9+
import io.smallrye.jwt.build.Jwt;
10+
11+
public class GenerateToken {
12+
/**
13+
* Generate JWT token
14+
*/
15+
public static void main(String[] args) {
16+
String token =
17+
Jwt.issuer("https://www.devpree.com.br/investme/issuer")
18+
.upn("investme-api")
19+
.groups(new HashSet<>(Arrays.asList("User", "Admin")))
20+
.claim(Claims.birthdate.name(), new Date())
21+
.sign();
22+
System.out.println(token);
23+
}
24+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.acme.security.jwt;
2+
3+
import org.eclipse.microprofile.jwt.JsonWebToken;
4+
5+
import jakarta.annotation.security.PermitAll;
6+
import jakarta.annotation.security.RolesAllowed;
7+
import jakarta.inject.Inject;
8+
import jakarta.ws.rs.GET;
9+
import jakarta.ws.rs.InternalServerErrorException;
10+
import jakarta.ws.rs.Path;
11+
import jakarta.ws.rs.Produces;
12+
import jakarta.ws.rs.core.Context;
13+
import jakarta.ws.rs.core.MediaType;
14+
import jakarta.ws.rs.core.SecurityContext;
15+
16+
17+
@Path("/secured")
18+
public class TokenSecuredResource {
19+
20+
@Inject
21+
JsonWebToken jwt;
22+
23+
@GET()
24+
@Path("permit-all")
25+
@PermitAll
26+
@Produces(MediaType.TEXT_PLAIN)
27+
public String hello(@Context SecurityContext ctx) {
28+
return getResponseString(ctx);
29+
}
30+
31+
@GET
32+
@Path("roles-allowed")
33+
@RolesAllowed({"User"})
34+
@Produces(MediaType.TEXT_PLAIN)
35+
public String helloRolesAllowed(@Context SecurityContext ctx) {
36+
return getResponseString(ctx) + ", birthdate: " + jwt.getClaim("birthdate").toString();
37+
}
38+
39+
private String getResponseString(SecurityContext ctx) {
40+
String name;
41+
if (ctx.getUserPrincipal() == null) {
42+
name = "anonymous";
43+
} else if (!ctx.getUserPrincipal().getName().equals(jwt.getName())) {
44+
throw new InternalServerErrorException("Principal and JsonWebToken names do not match");
45+
} else {
46+
name = ctx.getUserPrincipal().getName();
47+
}
48+
return String.format("hello + %s,"
49+
+ " isHttps: %s,"
50+
+ " authScheme: %s,"
51+
+ " hasJWT: %s",
52+
name, ctx.isSecure(), ctx.getAuthenticationScheme(), hasJwt());
53+
}
54+
55+
private boolean hasJwt() {
56+
return jwt.getClaimNames() != null;
57+
}
58+
}

0 commit comments

Comments
 (0)