Skip to content

Commit

Permalink
Merge pull request #29 from Nikola-Mircic/form
Browse files Browse the repository at this point in the history
Form validation
  • Loading branch information
Nikola-Mircic authored Jul 19, 2024
2 parents e8667b5 + 2ddff7c commit 74dee14
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 21 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
/samples/
/PDFs/
/XMLs/
user.enc
user.enc
pass.enc
efakturaplus.iml

2 changes: 1 addition & 1 deletion efakturaplus.iml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="22" jdkType="JavaSDK" />
<orderEntry type="module-library" exported="">
<library name="pdf-renderer-1.0.5.1.jar">
<CLASSES>
Expand All @@ -22,7 +23,6 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="jdk" jdkName="JavaSE-20" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" exported="">
<library name="json-tool.jar">
Expand Down
Empty file modified include/pdf-renderer-1.0.5.1.jar
100755 → 100644
Empty file.
111 changes: 94 additions & 17 deletions src/efakturaplus/gui/KeyPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.AlgorithmParameters;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Arrays;

import javax.crypto.*;
import javax.crypto.spec.*;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.*;
import javax.swing.border.Border;

import efakturaplus.models.User;

Expand Down Expand Up @@ -70,7 +69,6 @@ private void addComponents(int width, int height) {

passLabel = new JLabel("Please enter your password here:");
passLabel.setFont(font);
passLabel.setForeground(Color.black);

passInput = new JPasswordField();
addBorder(passInput, Color.black);
Expand All @@ -84,11 +82,17 @@ public void actionPerformed(ActionEvent e) {
});

File userData = new File("user.enc");


JButton signInBtn = makeButton("Sign in", new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
submitData();
}
});

if(!userData.exists()) {
keyLabel = new JLabel("Please enter your API key here:");
keyLabel.setFont(font);
keyLabel.setForeground(Color.black);
keyLabel.setBounds(width/2-150, height/4-75, 350, 50);

keyInput = new JTextField();
Expand All @@ -110,7 +114,6 @@ public void actionPerformed(ActionEvent e) {

passLabel2 = new JLabel("Please enter your password again:");
passLabel2.setFont(font);
passLabel2.setForeground(Color.black);
passLabel2.setBounds(width/2-150, height*3/4-75, 350, 50);


Expand All @@ -132,11 +135,13 @@ public void actionPerformed(ActionEvent e) {
this.add(passInput, gbc(0, 3));
this.add(passLabel2, gbc(0, 4));
this.add(passInput2, gbc(0, 5));
this.add(signInBtn, gbc(0, 6));
}else {
passLabel.setBounds(width/2-150, height/2-75, 350, 50);
passInput.setBounds(width/2-150, height/2-25, 300, 60);
this.add(passLabel, gbc(0, 0));
this.add(passInput, gbc(0, 1));
this.add(signInBtn, gbc(0, 2));
}
}

Expand All @@ -149,10 +154,27 @@ private GridBagConstraints gbc(int x, int y) {
constr.insets = new Insets(10, 5, 10, 5);

constr.fill = GridBagConstraints.HORIZONTAL;
constr.gridwidth = GridBagConstraints.REMAINDER;
constr.gridwidth = GridBagConstraints.CENTER;
return constr;
}


private JButton makeButton(String text, ActionListener listener) {
JButton btn = new JButton();

btn.setLayout(new BoxLayout(btn, BoxLayout.Y_AXIS));

btn.add(centeredLabel(text));
btn.addActionListener(listener);

return btn;
}

private JLabel centeredLabel(String text) {
JLabel label = new JLabel(text);
label.setAlignmentX(CENTER_ALIGNMENT);
return label;
}

private void addBorder(JComponent comp, Color c) {
comp.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(c, 3, true),
Expand All @@ -164,7 +186,16 @@ private void submitData() {

if(!userData.exists()) {
User.API_KEY = keyInput.getText();


if(!Arrays.equals(passInput.getPassword(), passInput2.getPassword())){
addBorder(passInput, Color.RED);
addBorder(passInput2, Color.RED);

JOptionPane.showMessageDialog(null, "Passwords do not match!");

return;
}

try {
encryptData();
} catch (Exception e) {
Expand All @@ -174,6 +205,15 @@ private void submitData() {
addComponents(getWidth(), getHeight());
}
else {
if(!validateInput()){
addBorder(passInput, Color.RED);
passInput.setText("");

JOptionPane.showMessageDialog(null, "Incorrect password!");

return;
}

try {
String data = decryptData();
System.out.println("Decrypted: "+data);
Expand All @@ -188,14 +228,51 @@ private void submitData() {
}

private boolean validateInput() {
boolean valid = true;


try {
FileInputStream fis = new FileInputStream("pass.enc");

String password = new String(passInput.getPassword());

MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
byte[] passEnc = messageDigest.digest(password.getBytes(StandardCharsets.UTF_8));

byte[] loadedPass = fis.readAllBytes();

if(Arrays.equals(passEnc, loadedPass)){
return true;
}

fis.close();
} catch (Exception e){
e.printStackTrace();
}

return valid;
return false;
}

private void encryptData() throws NoSuchAlgorithmException, InvalidKeySpecException {
/* SAVING PASSWORD
* The password is saved as SHA-256 encoded string
* */

String password = passInput.getText();

MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
byte[] passEnc = messageDigest.digest(password.getBytes(StandardCharsets.UTF_8));

try {
FileOutputStream fos = new FileOutputStream("pass.enc");

fos.write(passEnc);

fos.close();
} catch (Exception e) {
e.printStackTrace();
}

/* SAVING EFAKTURA API KEY
* The API key is encrypted with AES256 algorithm, and saved alongside with IV
* */
SecureRandom random = new SecureRandom();
byte[] salt = new byte[8];
random.nextBytes(salt);
Expand Down
5 changes: 4 additions & 1 deletion src/efakturaplus/gui/MainPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ private JButton makeButton(Image ico, String text, ActionListener listener) {
JButton btn = new JButton();

btn.setLayout(new BoxLayout(btn, BoxLayout.Y_AXIS));
btn.add(centeredLabel(new StretchIcon(ico)));

if(ico != null)
btn.add(centeredLabel(new StretchIcon(ico)));

btn.add(centeredLabel(text));
btn.addActionListener(listener);

Expand Down
2 changes: 1 addition & 1 deletion src/efakturaplus/util/EFakturaUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private synchronized HttpResponse<String> sendRequest(HttpRequest request) {
HttpClient client = HttpClient.newHttpClient();
try {
// GetInvoiceRequest
Thread.sleep(500);
Thread.sleep(1000);
HttpResponse<String> res = client.send(request, HttpResponse.BodyHandlers.ofString());

return res;
Expand Down

0 comments on commit 74dee14

Please sign in to comment.