Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kan 77/backend basic authentication #35

Merged
merged 7 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add security.jpa package for authentication with username and p…
…assword
  • Loading branch information
tungkhanhh committed Oct 6, 2024
commit ba1ea544da576c9d37af923a1f66bb0f6c0770d0
3,506 changes: 3,506 additions & 0 deletions backend/logs/quarkus.log

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions backend/logs/quarkus.log.1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2024-10-06 12:44:23,924 tk-legion5pro C:\Program Files\Java\jdk-22\bin\java.exe[4416] DEBUG [org.jbo.logging] (main) Logging Provider: org.jboss.logging.JBossLogManagerProvider
124 changes: 0 additions & 124 deletions backend/og-pom.txt

This file was deleted.

4 changes: 4 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-security-jpa</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
5 changes: 4 additions & 1 deletion backend/src/main/java/org/acme/TimetableResource.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.acme;

import ai.timefold.solver.core.api.solver.SolverManager;
import jakarta.annotation.security.RolesAllowed;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.Consumes;
Expand Down Expand Up @@ -36,6 +37,7 @@ public class TimetableResource {
SolverManager<Timetable, String> solverManager;

@POST
// @RolesAllowed({"user"})
@Transactional
public Timetable handleRequest(Timetable problem) throws ExecutionException, InterruptedException {
UUID uuid = UUID.randomUUID();
Expand Down Expand Up @@ -81,6 +83,7 @@ public void findByCampusAndDelete(String campusName) {
}

@GET
// @RolesAllowed({"user"})
@Transactional
@Produces(MediaType.APPLICATION_JSON)
public Timetable solveExample() throws ExecutionException, InterruptedException {
Expand Down Expand Up @@ -130,7 +133,7 @@ public Timetable solveExample() throws ExecutionException, InterruptedException

/*
* During this solving phase, new Unit objects will be created with the
* alloted date and Room assignment.
* allotted date and Room assignment.
*
* Currently, the 'old' Unit objects in the 'problem' variable and the
* 'new' Unit objects in the 'solution' variable are stored as different
Expand Down
19 changes: 19 additions & 0 deletions backend/src/main/java/org/acme/security/jpa/Startup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.acme.security.jpa;

import jakarta.enterprise.event.Observes;
import jakarta.inject.Singleton;
import jakarta.transaction.Transactional;

import io.quarkus.runtime.StartupEvent;


@Singleton
public class Startup {
@Transactional
public void loadUsers(@Observes StartupEvent evt) {
// reset and load all test users
User.deleteAll();
User.add("admin", "admin", "admin");
User.add("user", "user", "user");
}
}
36 changes: 36 additions & 0 deletions backend/src/main/java/org/acme/security/jpa/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.acme.security.jpa;

import io.quarkus.elytron.security.common.BcryptUtil;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
import io.quarkus.security.jpa.Password;
import io.quarkus.security.jpa.Roles;
import io.quarkus.security.jpa.UserDefinition;
import io.quarkus.security.jpa.Username;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;

@Entity
@Table(name = "test_user")
@UserDefinition
public class User extends PanacheEntity {
@Username
public String username;
@Password
public String password;
@Roles
public String role;

/**
* Adds a new user to the database
* @param username the username
* @param password the unencrypted password (it is encrypted with bcrypt)
* @param role the comma-separated roles
*/
public static void add(String username, String password, String role) {
User user = new User();
user.username = username;
user.password = BcryptUtil.bcryptHash(password);
user.role = role;
user.persist();
}
}
18 changes: 18 additions & 0 deletions backend/src/main/java/org/acme/security/jpa/UserResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.acme.security.jpa;

import jakarta.annotation.security.RolesAllowed;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.SecurityContext;

@Path("/api/users")
public class UserResource {

@GET
@RolesAllowed({"user"})
@Path("/me")
public String me(@Context SecurityContext securityContext) {
return securityContext.getUserPrincipal().getName();
}
}