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

add authentication #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 12 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-qute</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-elytron-security</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-reactive-jackson</artifactId>
Expand Down Expand Up @@ -83,7 +87,14 @@
<artifactId>font-awesome</artifactId>
<version>${webjars-font-awesome.version}</version>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-elytron-security-jdbc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-elytron-security</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<UsernamePasswordAuthenticationRequest> {


@Override
public Class<UsernamePasswordAuthenticationRequest> getRequestType() {
return UsernamePasswordAuthenticationRequest.class;
}

@Override
public Uni<SecurityIdentity> authenticate(UsernamePasswordAuthenticationRequest usernamePasswordAuthenticationRequest, AuthenticationRequestContext authenticationRequestContext) {
Supplier<SecurityIdentity> s = () -> new QuarkusSecurityIdentity.Builder().setPrincipal(new QuarkusPrincipal(usernamePasswordAuthenticationRequest.getUsername())).addRole("User")
.build();
return authenticationRequestContext.runBlocking(s);

}
}
17 changes: 17 additions & 0 deletions src/main/java/org/quarkus/samples/petclinic/login/User.java
Original file line number Diff line number Diff line change
@@ -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;


}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -36,6 +37,7 @@ public class OwnersResource {

@GET
@Path("/find")
@Authenticated
@Produces(MediaType.TEXT_HTML)
/**
* Renders the findOwners.html
Expand All @@ -48,6 +50,7 @@ public TemplateInstance findTemplate() {

@GET
@Path("new")
@Authenticated
@Produces(MediaType.TEXT_HTML)
/**
* Renders the createOrUpdateOwnerForm.html
Expand All @@ -60,6 +63,7 @@ public TemplateInstance createTemplate() {

@GET
@Path("{ownerId}/edit")
@Authenticated
@Produces(MediaType.TEXT_HTML)
/**
* Renders the createOrUpdateOwnerForm.html
Expand All @@ -72,6 +76,7 @@ public TemplateInstance editTemplate(@PathParam("ownerId") Long ownerId) {

@GET
@Path("{ownerId}")
@Authenticated
@Produces(MediaType.TEXT_HTML)
/**
* Renders the createOrUpdateOwnerForm.html
Expand All @@ -85,6 +90,7 @@ public TemplateInstance showOwner(@PathParam("ownerId") Long ownerId) {
@POST
@Path("new")
@Produces(MediaType.TEXT_HTML)
@Authenticated
@Transactional
/**
* Renders the createOrUpdateOwnerForm.html
Expand All @@ -111,6 +117,7 @@ public TemplateInstance processCreationForm(@BeanParam Owner owner) {
@POST
@Path("{ownerId}/edit")
@Transactional
@Authenticated
@Produces(MediaType.TEXT_HTML)
/**
* Renders the createOrUpdateOwnerForm.html
Expand All @@ -136,6 +143,7 @@ public TemplateInstance processUpdateOwnerForm(@BeanParam Owner owner, @PathPara

@GET
@Produces(MediaType.TEXT_HTML)
@Authenticated
/**
* Process the findOwners form
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -24,6 +25,7 @@
import io.quarkus.qute.TemplateInstance;

@Path("/owners")
@Authenticated
public class PetResource {

@Inject
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,6 +24,7 @@
import io.quarkus.qute.TemplateInstance;

@Path("/owners")
@Authenticated
public class VisitResource {

@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javax.ws.rs.core.MediaType;

import io.quarkus.qute.TemplateInstance;
import io.quarkus.security.Authenticated;

@Path("/")
public class WelcomeResource {
Expand All @@ -18,6 +19,7 @@ public class WelcomeResource {

@GET
@Produces(MediaType.TEXT_HTML)
@Authenticated
public TemplateInstance get() {
return templates.welcome();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import javax.ws.rs.core.MediaType;

@Path("/")

public class VetResource {

@Inject
Expand Down
55 changes: 55 additions & 0 deletions src/main/resources/META-INF/resources/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Authentication Failed</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
margin: 0;
padding: 0;
}

.container {
max-width: 400px;
margin: 50px auto;
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0px 0px 10px rgba(0,0,0,0.1);
}

.error-message {
color: #d9534f;
font-size: 18px;
margin-bottom: 15px;
}

a {
display: inline-block;
margin-top: 10px;
background-color: #007bff;
color: white;
padding: 10px 20px;
text-decoration: none;
border-radius: 4px;
}

a:hover {
background-color: #0056b3;
}
</style>
</head>
<body>

<div class="container">
<div class="error-message">
<strong>Authentication Failed!</strong>
<p>Your username or password is incorrect.</p>
</div>
<a href="/login.html">Back to Login</a>
</div>

</body>
</html>
68 changes: 68 additions & 0 deletions src/main/resources/META-INF/resources/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f5f5f5;
}

.container {
max-width: 300px;
margin: 50px auto;
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0px 0px 10px rgba(0,0,0,0.1);
}

label {
display: block;
margin-bottom: 8px;
font-weight: bold;
}

input[type="text"],
input[type="password"] {
width: 100%;
padding: 12px;
margin: 8px 0;
border: 1px solid #ccc;
border-radius: 4px;
}

input[type="submit"] {
background-color: #007bff;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
}

input[type="submit"]:hover {
background-color: #0056b3;
}
</style>
</head>
<body>

<div class="container">
<form action="/j_security_check" method="post">
<label for="username">Email</label>
<input type="text" id="username" name="j_username" placeholder="Email">

<label for="password">Password</label>
<input type="password" id="password" name="j_password" placeholder="Password">

<input type="submit" value="Login">
</form>
</div>

</body>
</html>
20 changes: 19 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
3 changes: 3 additions & 0 deletions src/main/resources/import.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
('[email protected]', '$2a$10$1o1y7Q2Qf8k5g94u804yX.b0o12y.oS.i18.827d0716417721329484'),
('[email protected]', '$2a$10$1o1y7Q2Qf8k5g94u804yX.b0o12y.oS.i18.827d0716417721329484');