From d23321634f6e08423e38c2b7d613b02bec345007 Mon Sep 17 00:00:00 2001 From: sharath Date: Mon, 28 Aug 2023 00:58:44 -0500 Subject: [PATCH] add authentication --- pom.xml | 13 +++- .../login/CustomIdentityProvider.java | 36 ++++++++++ .../quarkus/samples/petclinic/login/User.java | 17 +++++ .../petclinic/owner/OwnersResource.java | 8 +++ .../samples/petclinic/owner/PetResource.java | 2 + .../petclinic/owner/VisitResource.java | 2 + .../petclinic/system/WelcomeResource.java | 2 + .../samples/petclinic/vet/VetResource.java | 1 + .../resources/META-INF/resources/error.html | 55 +++++++++++++++ .../resources/META-INF/resources/login.html | 68 +++++++++++++++++++ src/main/resources/application.properties | 20 +++++- src/main/resources/import.sql | 3 + 12 files changed, 225 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/quarkus/samples/petclinic/login/CustomIdentityProvider.java create mode 100644 src/main/java/org/quarkus/samples/petclinic/login/User.java create mode 100644 src/main/resources/META-INF/resources/error.html create mode 100644 src/main/resources/META-INF/resources/login.html diff --git a/pom.xml b/pom.xml index fd9973a..10db913 100644 --- a/pom.xml +++ b/pom.xml @@ -39,6 +39,10 @@ io.quarkus quarkus-resteasy-reactive-qute + + io.quarkus + quarkus-elytron-security + io.quarkus quarkus-rest-client-reactive-jackson @@ -83,7 +87,14 @@ font-awesome ${webjars-font-awesome.version} - + + io.quarkus + quarkus-elytron-security-jdbc + + + io.quarkus + quarkus-elytron-security + io.quarkus quarkus-junit5 diff --git a/src/main/java/org/quarkus/samples/petclinic/login/CustomIdentityProvider.java b/src/main/java/org/quarkus/samples/petclinic/login/CustomIdentityProvider.java new file mode 100644 index 0000000..520bacd --- /dev/null +++ b/src/main/java/org/quarkus/samples/petclinic/login/CustomIdentityProvider.java @@ -0,0 +1,36 @@ +package org.quarkus.samples.petclinic.login; + +import com.sun.security.auth.UserPrincipal; +import io.quarkus.security.identity.AuthenticationRequestContext; +import io.quarkus.security.identity.IdentityProvider; +import io.quarkus.security.identity.SecurityIdentity; +import io.quarkus.security.identity.request.AuthenticationRequest; +import io.quarkus.security.identity.request.UsernamePasswordAuthenticationRequest; +import io.quarkus.security.runtime.QuarkusPrincipal; +import io.quarkus.security.runtime.QuarkusSecurityIdentity; +import io.smallrye.mutiny.Uni; +import io.vertx.ext.auth.authentication.UsernamePasswordCredentials; +import org.apache.sshd.common.config.keys.loader.openssh.kdf.BCrypt; + +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; +import javax.transaction.UserTransaction; +import java.util.function.Supplier; + +@ApplicationScoped +public class CustomIdentityProvider implements IdentityProvider { + + + @Override + public Class getRequestType() { + return UsernamePasswordAuthenticationRequest.class; + } + + @Override + public Uni authenticate(UsernamePasswordAuthenticationRequest usernamePasswordAuthenticationRequest, AuthenticationRequestContext authenticationRequestContext) { + Supplier s = () -> new QuarkusSecurityIdentity.Builder().setPrincipal(new QuarkusPrincipal(usernamePasswordAuthenticationRequest.getUsername())).addRole("User") + .build(); + return authenticationRequestContext.runBlocking(s); + + } +} diff --git a/src/main/java/org/quarkus/samples/petclinic/login/User.java b/src/main/java/org/quarkus/samples/petclinic/login/User.java new file mode 100644 index 0000000..d06daa6 --- /dev/null +++ b/src/main/java/org/quarkus/samples/petclinic/login/User.java @@ -0,0 +1,17 @@ +package org.quarkus.samples.petclinic.login; + +import io.quarkus.hibernate.orm.panache.PanacheEntity; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "users") +public class User { +@Id + public String email; + public String password; + + +} diff --git a/src/main/java/org/quarkus/samples/petclinic/owner/OwnersResource.java b/src/main/java/org/quarkus/samples/petclinic/owner/OwnersResource.java index 8ae0745..9281d65 100644 --- a/src/main/java/org/quarkus/samples/petclinic/owner/OwnersResource.java +++ b/src/main/java/org/quarkus/samples/petclinic/owner/OwnersResource.java @@ -1,5 +1,6 @@ package org.quarkus.samples.petclinic.owner; +import io.quarkus.security.Authenticated; import org.quarkus.samples.petclinic.system.TemplatesLocale; import org.quarkus.samples.petclinic.visit.Visit; @@ -36,6 +37,7 @@ public class OwnersResource { @GET @Path("/find") + @Authenticated @Produces(MediaType.TEXT_HTML) /** * Renders the findOwners.html @@ -48,6 +50,7 @@ public TemplateInstance findTemplate() { @GET @Path("new") + @Authenticated @Produces(MediaType.TEXT_HTML) /** * Renders the createOrUpdateOwnerForm.html @@ -60,6 +63,7 @@ public TemplateInstance createTemplate() { @GET @Path("{ownerId}/edit") + @Authenticated @Produces(MediaType.TEXT_HTML) /** * Renders the createOrUpdateOwnerForm.html @@ -72,6 +76,7 @@ public TemplateInstance editTemplate(@PathParam("ownerId") Long ownerId) { @GET @Path("{ownerId}") + @Authenticated @Produces(MediaType.TEXT_HTML) /** * Renders the createOrUpdateOwnerForm.html @@ -85,6 +90,7 @@ public TemplateInstance showOwner(@PathParam("ownerId") Long ownerId) { @POST @Path("new") @Produces(MediaType.TEXT_HTML) + @Authenticated @Transactional /** * Renders the createOrUpdateOwnerForm.html @@ -111,6 +117,7 @@ public TemplateInstance processCreationForm(@BeanParam Owner owner) { @POST @Path("{ownerId}/edit") @Transactional + @Authenticated @Produces(MediaType.TEXT_HTML) /** * Renders the createOrUpdateOwnerForm.html @@ -136,6 +143,7 @@ public TemplateInstance processUpdateOwnerForm(@BeanParam Owner owner, @PathPara @GET @Produces(MediaType.TEXT_HTML) + @Authenticated /** * Process the findOwners form */ diff --git a/src/main/java/org/quarkus/samples/petclinic/owner/PetResource.java b/src/main/java/org/quarkus/samples/petclinic/owner/PetResource.java index 8c6af38..2c84934 100644 --- a/src/main/java/org/quarkus/samples/petclinic/owner/PetResource.java +++ b/src/main/java/org/quarkus/samples/petclinic/owner/PetResource.java @@ -1,6 +1,7 @@ package org.quarkus.samples.petclinic.owner; +import io.quarkus.security.Authenticated; import org.quarkus.samples.petclinic.system.Templates; import org.quarkus.samples.petclinic.system.TemplatesLocale; @@ -24,6 +25,7 @@ import io.quarkus.qute.TemplateInstance; @Path("/owners") +@Authenticated public class PetResource { @Inject diff --git a/src/main/java/org/quarkus/samples/petclinic/owner/VisitResource.java b/src/main/java/org/quarkus/samples/petclinic/owner/VisitResource.java index 6796cc7..407b121 100644 --- a/src/main/java/org/quarkus/samples/petclinic/owner/VisitResource.java +++ b/src/main/java/org/quarkus/samples/petclinic/owner/VisitResource.java @@ -1,5 +1,6 @@ package org.quarkus.samples.petclinic.owner; +import io.quarkus.security.Authenticated; import org.quarkus.samples.petclinic.system.Templates; import org.quarkus.samples.petclinic.system.TemplatesLocale; import org.quarkus.samples.petclinic.visit.Visit; @@ -23,6 +24,7 @@ import io.quarkus.qute.TemplateInstance; @Path("/owners") +@Authenticated public class VisitResource { @Inject diff --git a/src/main/java/org/quarkus/samples/petclinic/system/WelcomeResource.java b/src/main/java/org/quarkus/samples/petclinic/system/WelcomeResource.java index 2cbe445..b768a21 100644 --- a/src/main/java/org/quarkus/samples/petclinic/system/WelcomeResource.java +++ b/src/main/java/org/quarkus/samples/petclinic/system/WelcomeResource.java @@ -9,6 +9,7 @@ import javax.ws.rs.core.MediaType; import io.quarkus.qute.TemplateInstance; +import io.quarkus.security.Authenticated; @Path("/") public class WelcomeResource { @@ -18,6 +19,7 @@ public class WelcomeResource { @GET @Produces(MediaType.TEXT_HTML) + @Authenticated public TemplateInstance get() { return templates.welcome(); } diff --git a/src/main/java/org/quarkus/samples/petclinic/vet/VetResource.java b/src/main/java/org/quarkus/samples/petclinic/vet/VetResource.java index 7c11ce3..fe9b7b6 100644 --- a/src/main/java/org/quarkus/samples/petclinic/vet/VetResource.java +++ b/src/main/java/org/quarkus/samples/petclinic/vet/VetResource.java @@ -14,6 +14,7 @@ import javax.ws.rs.core.MediaType; @Path("/") + public class VetResource { @Inject diff --git a/src/main/resources/META-INF/resources/error.html b/src/main/resources/META-INF/resources/error.html new file mode 100644 index 0000000..241a322 --- /dev/null +++ b/src/main/resources/META-INF/resources/error.html @@ -0,0 +1,55 @@ + + + + + Authentication Failed + + + + +
+
+ Authentication Failed! +

Your username or password is incorrect.

+
+ Back to Login +
+ + + diff --git a/src/main/resources/META-INF/resources/login.html b/src/main/resources/META-INF/resources/login.html new file mode 100644 index 0000000..e1c11b6 --- /dev/null +++ b/src/main/resources/META-INF/resources/login.html @@ -0,0 +1,68 @@ + + + + + Login + + + + +
+
+ + + + + + + +
+
+ + + diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 4dafe3b..1bbecc5 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,12 +2,14 @@ quarkus.datasource.db-kind=postgresql %prod.quarkus.datasource.username=developer %prod.quarkus.datasource.password=developer %prod.quarkus.datasource.jdbc.url=jdbc:postgresql://postgresql:5432/mydb +quarkus.datasource.username=petclinic +quarkus.datasource.password=petclinic +quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/petclinic quarkus.datasource.jdbc.min-size=5 quarkus.datasource.jdbc.max-size=15 %dev.quarkus.hibernate-orm.sql-load-script=import.sql %prod.quarkus.hibernate-orm.sql-load-script=import.sql - quarkus.hibernate-orm.log.sql=true quarkus.hibernate-orm.database.generation=drop-and-create @@ -20,3 +22,19 @@ quarkus.kubernetes.service-type=load-balancer quarkus.http.enable-compression=true quarkus.http.enable-decompression=true quarkus.qute.strict-rendering=true + +quarkus.security.jdbc.enabled=true +quarkus.security.jdbc.principal-query.sql=SELECT password FROM users WHERE email=? +quarkus.security.jdbc.principal-query.clear-password-mapper.password-index=1 +quarkus.security.jdbc.principal-query.clear-password-mapper.enabled=true +quarkus.log.category."io.quarkus.security".level=DEBUG +quarkus.http.auth.form.login-page=/login.html +quarkus.http.auth.form.error-page=/error.html +quarkus.http.auth.form.enabled=true +quarkus.http.auth.form.landing-page=/ +quarkus.se.timeout=30M # Sets a 30-minute session timeout +quarkus.http.auth.proactive=false +quarkus.http.error_page./401=401.html + + +quarkus.security.jdbc.principal-query.hash-algorithm=bcrypt \ No newline at end of file diff --git a/src/main/resources/import.sql b/src/main/resources/import.sql index 78c608d..c1d5a27 100644 --- a/src/main/resources/import.sql +++ b/src/main/resources/import.sql @@ -51,3 +51,6 @@ INSERT INTO visits(id, pet_id, visit_date, description) VALUES (1001, 1007, '201 INSERT INTO visits(id, pet_id, visit_date, description) VALUES (1002, 1008, '2013-01-02', 'rabies shot'); INSERT INTO visits(id, pet_id, visit_date, description) VALUES (1003, 1008, '2013-01-03', 'neutered'); INSERT INTO visits(id, pet_id, visit_date, description) VALUES (1004, 1007, '2013-01-04', 'spayed'); +INSERT INTO users (email, password) VALUES + ('example@email.com', '$2a$10$1o1y7Q2Qf8k5g94u804yX.b0o12y.oS.i18.827d0716417721329484'), + ('example1@email.com', '$2a$10$1o1y7Q2Qf8k5g94u804yX.b0o12y.oS.i18.827d0716417721329484');