Use lombok and correctly return a signingrequest from the database
This commit is contained in:
48
openapi.yml
48
openapi.yml
@ -37,6 +37,39 @@ paths:
|
|||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
$ref: "#/components/schemas/HelloResponse"
|
$ref: "#/components/schemas/HelloResponse"
|
||||||
|
/signing-request/{id}:
|
||||||
|
get:
|
||||||
|
tags:
|
||||||
|
- signing-request
|
||||||
|
summary: Get a signing request
|
||||||
|
operationId: getSigningRequest
|
||||||
|
parameters:
|
||||||
|
- name: id
|
||||||
|
in: path
|
||||||
|
required: true
|
||||||
|
description: The ID of the signing request
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: "Signing request was found"
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/GetSigningRequestResponse"
|
||||||
|
401:
|
||||||
|
description: "Unauthorized"
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/ErrorResponse"
|
||||||
|
404:
|
||||||
|
description: "Signing request was not found"
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/ErrorResponse"
|
||||||
|
|
||||||
/token:
|
/token:
|
||||||
post:
|
post:
|
||||||
tags:
|
tags:
|
||||||
@ -89,6 +122,21 @@ components:
|
|||||||
token:
|
token:
|
||||||
type: string
|
type: string
|
||||||
|
|
||||||
|
GetSigningRequestResponse:
|
||||||
|
type: object
|
||||||
|
required:
|
||||||
|
- id
|
||||||
|
- signed
|
||||||
|
properties:
|
||||||
|
id:
|
||||||
|
type: string
|
||||||
|
signingRequestDocumentIds:
|
||||||
|
type: array
|
||||||
|
items:
|
||||||
|
type: string
|
||||||
|
signed:
|
||||||
|
type: boolean
|
||||||
|
|
||||||
ErrorResponse:
|
ErrorResponse:
|
||||||
type: object
|
type: object
|
||||||
required:
|
required:
|
||||||
|
@ -0,0 +1,21 @@
|
|||||||
|
package ch.dlmw.swisssignchallenge.controllers;
|
||||||
|
|
||||||
|
import ch.dlmw.swisssignchallenge.services.impl.SigningRequestService;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import org.openapitools.api.SigningRequestApi;
|
||||||
|
import org.openapitools.model.GetSigningRequestResponse;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class SigningRequestController implements SigningRequestApi {
|
||||||
|
private final SigningRequestService signingRequestService;
|
||||||
|
|
||||||
|
public ResponseEntity<GetSigningRequestResponse> getSigningRequest(String id) {
|
||||||
|
var signingRequest = signingRequestService.getSigningRequest(id);
|
||||||
|
var response = new GetSigningRequestResponse(signingRequest.getId().toString(), signingRequest.isSigned());
|
||||||
|
|
||||||
|
return ResponseEntity.ok(response);
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package ch.dlmw.swisssignchallenge.controllers;
|
package ch.dlmw.swisssignchallenge.controllers;
|
||||||
|
|
||||||
import ch.dlmw.swisssignchallenge.utils.JwtUtil;
|
import ch.dlmw.swisssignchallenge.utils.JwtUtil;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import org.openapitools.api.TokenApi;
|
import org.openapitools.api.TokenApi;
|
||||||
import org.openapitools.model.CreateSessionRequest;
|
import org.openapitools.model.CreateSessionRequest;
|
||||||
import org.openapitools.model.CreateSessionResponse;
|
import org.openapitools.model.CreateSessionResponse;
|
||||||
@ -12,17 +13,12 @@ import org.springframework.security.core.userdetails.UserDetailsService;
|
|||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@AllArgsConstructor
|
||||||
public class TokenController implements TokenApi {
|
public class TokenController implements TokenApi {
|
||||||
private final AuthenticationManager authenticationManager;
|
private final AuthenticationManager authenticationManager;
|
||||||
private final UserDetailsService userDetailsService;
|
private final UserDetailsService userDetailsService;
|
||||||
private final JwtUtil jwtUtil;
|
private final JwtUtil jwtUtil;
|
||||||
|
|
||||||
public TokenController(AuthenticationManager authenticationManager, UserDetailsService userDetailsService, JwtUtil jwtUtil) {
|
|
||||||
this.authenticationManager = authenticationManager;
|
|
||||||
this.userDetailsService = userDetailsService;
|
|
||||||
this.jwtUtil = jwtUtil;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ResponseEntity<CreateSessionResponse> createToken(CreateSessionRequest createSessionRequest) {
|
public ResponseEntity<CreateSessionResponse> createToken(CreateSessionRequest createSessionRequest) {
|
||||||
authenticationManager.authenticate(
|
authenticationManager.authenticate(
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package ch.dlmw.swisssignchallenge.entities;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "signing_request")
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class SigningRequest {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(generator = "UUID")
|
||||||
|
@Column(name = "id", updatable = false, nullable = false)
|
||||||
|
private UUID id;
|
||||||
|
|
||||||
|
@Column(name = "signed", nullable = false)
|
||||||
|
private boolean signed;
|
||||||
|
|
||||||
|
@OneToMany(mappedBy = "signingRequest", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||||
|
private List<SigningRequestDocument> documents;
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package ch.dlmw.swisssignchallenge.entities;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "signing_request_document")
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class SigningRequestDocument {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(generator = "UUID")
|
||||||
|
@Column(name = "id", updatable = false, nullable = false)
|
||||||
|
private UUID id;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
|
@JoinColumn(name = "signing_request_id", nullable = false)
|
||||||
|
private SigningRequest signingRequest;
|
||||||
|
|
||||||
|
@Column(name = "name", nullable = false)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@Column(name = "confirmed", nullable = false)
|
||||||
|
private boolean confirmed;
|
||||||
|
|
||||||
|
@Lob
|
||||||
|
@Column(name = "data", nullable = false)
|
||||||
|
private byte[] data;
|
||||||
|
}
|
@ -8,14 +8,9 @@ import java.util.UUID;
|
|||||||
@Entity
|
@Entity
|
||||||
@Table(name = "users")
|
@Table(name = "users")
|
||||||
public class User {
|
public class User {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(generator = "UUID")
|
@GeneratedValue(generator = "UUID")
|
||||||
@GenericGenerator(
|
@Column(name = "id", updatable = false, nullable = false)
|
||||||
name = "UUID",
|
|
||||||
strategy = "org.hibernate.id.UUIDGenerator"
|
|
||||||
)
|
|
||||||
@Column(name = "user_id", updatable = false, nullable = false)
|
|
||||||
private UUID userId;
|
private UUID userId;
|
||||||
|
|
||||||
@Column(name = "username", unique = true, nullable = false, length = 50)
|
@Column(name = "username", unique = true, nullable = false, length = 50)
|
||||||
|
@ -0,0 +1,11 @@
|
|||||||
|
package ch.dlmw.swisssignchallenge.repositories;
|
||||||
|
|
||||||
|
import ch.dlmw.swisssignchallenge.entities.SigningRequest;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface SigningRequestRepository extends JpaRepository<SigningRequest, UUID> {
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package ch.dlmw.swisssignchallenge.services;
|
||||||
|
|
||||||
|
import ch.dlmw.swisssignchallenge.entities.SigningRequest;
|
||||||
|
|
||||||
|
public interface SigningRequestService {
|
||||||
|
SigningRequest getSigningRequest(String id);
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package ch.dlmw.swisssignchallenge.services.impl;
|
||||||
|
|
||||||
|
import ch.dlmw.swisssignchallenge.entities.SigningRequest;
|
||||||
|
import ch.dlmw.swisssignchallenge.repositories.SigningRequestRepository;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class SigningRequestService implements ch.dlmw.swisssignchallenge.services.SigningRequestService {
|
||||||
|
|
||||||
|
private final SigningRequestRepository signingRequestRepository;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SigningRequest getSigningRequest(String id) {
|
||||||
|
return signingRequestRepository.findById(UUID.fromString(id)).orElseThrow();
|
||||||
|
}
|
||||||
|
}
|
@ -1,20 +1,18 @@
|
|||||||
package ch.dlmw.swisssignchallenge.services;
|
package ch.dlmw.swisssignchallenge.services.impl;
|
||||||
|
|
||||||
import ch.dlmw.swisssignchallenge.entities.User;
|
import ch.dlmw.swisssignchallenge.entities.User;
|
||||||
import ch.dlmw.swisssignchallenge.repositories.UserRepository;
|
import ch.dlmw.swisssignchallenge.repositories.UserRepository;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@AllArgsConstructor
|
||||||
public class UserDetailsServiceImpl implements UserDetailsService {
|
public class UserDetailsServiceImpl implements UserDetailsService {
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
public UserDetailsServiceImpl(UserRepository userRepository) {
|
|
||||||
this.userRepository = userRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||||
User user = userRepository.findByUsername(username)
|
User user = userRepository.findByUsername(username)
|
||||||
@ -23,7 +21,6 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
|||||||
return org.springframework.security.core.userdetails.User
|
return org.springframework.security.core.userdetails.User
|
||||||
.withUsername(user.getUsername())
|
.withUsername(user.getUsername())
|
||||||
.password(user.getPasswordHash())
|
.password(user.getPasswordHash())
|
||||||
// .roles("USER")
|
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,5 +1,18 @@
|
|||||||
CREATE TABLE users (
|
CREATE TABLE users (
|
||||||
user_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
username VARCHAR(50) UNIQUE NOT NULL,
|
username VARCHAR(50) UNIQUE NOT NULL,
|
||||||
password_hash TEXT NOT NULL
|
password_hash TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE signing_request (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
signed BOOLEAN NOT NULL DEFAULT FALSE
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE signing_request_document (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
signing_request_id UUID NOT NULL REFERENCES signing_request(id) ON DELETE CASCADE,
|
||||||
|
name VARCHAR(255) NOT NULL,
|
||||||
|
confirmed BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
|
data BYTEA NOT NULL
|
||||||
);
|
);
|
6
src/main/resources/db/migration/V2__data_insert.sql
Normal file
6
src/main/resources/db/migration/V2__data_insert.sql
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
INSERT INTO users(id, username, password_hash)
|
||||||
|
VALUES ('6313d131-f8b6-41f0-8e1c-c07adc6e0cc1', 'john', '$2a$10$urXz.BOEG0BtbvU7VGOZw.picxDXmIHFYBxW7N.alXTeVCOkPMCTy');
|
||||||
|
|
||||||
|
INSERT INTO signing_request (id, signed)
|
||||||
|
VALUES
|
||||||
|
('a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11', FALSE);
|
Reference in New Issue
Block a user