Browse Source

Merge pull request #5 from Rudi9719/master

Added authentication for card creation
pull/6/head
jordanmason-stleo 4 years ago committed by GitHub
parent
commit
32436f97ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      src/main/java/MTGClone/SQLDriver.java
  2. 10
      src/main/java/MTGClone/controller/CardController.java
  3. 23
      src/main/java/MTGClone/controller/HelloController.java
  4. 5
      src/main/webapp/WEB-INF/jsp/card.jsp
  5. 16
      src/main/webapp/WEB-INF/jsp/hello.jsp

22
src/main/java/MTGClone/SQLDriver.java

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
package MTGClone;
import java.security.MessageDigest;
import java.sql.*;
import java.util.*;
@ -16,7 +17,26 @@ public class SQLDriver { @@ -16,7 +17,26 @@ public class SQLDriver {
}
return true;
}
public boolean authenticateUser(String username, String password) {
return sha256(password).equalsIgnoreCase("c109e7af71c435d32afb75e334e417ddeba82dbde609d4c47f2e3c717057e458");
}
public static String sha256(String base) {
try{
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(base.getBytes("UTF-8"));
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < hash.length; i++) {
String hex = Integer.toHexString(0xff & hash[i]);
if(hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
} catch(Exception ex){
throw new RuntimeException(ex);
}
}
public Card getRandomCard() {
try {
c = DriverManager.getConnection("jdbc:sqlite:cards.db");

10
src/main/java/MTGClone/controller/CardController.java

@ -24,10 +24,12 @@ public class CardController { @@ -24,10 +24,12 @@ public class CardController {
@PostMapping({"/card"})
public String greet(@RequestParam("cardname") String cardname, @RequestParam("manacost") int manacost,
@RequestParam("power") int power, @RequestParam("toughness") int toughness, @RequestParam("description") String description, @RequestParam("creaturetype") String creaturetype,
ModelMap modelMap) {
Card newCard = new Card(cardname, manacost, power, toughness, description, "", creaturetype);
SQLDriver d = new SQLDriver();
d.insertCard(newCard);
@RequestParam("username") String username, @RequestParam("password") String password, ModelMap modelMap) {
SQLDriver d = new SQLDriver();
if (d.authenticateUser(username, password)) {
Card newCard = new Card(cardname, manacost, power, toughness, description, "", creaturetype);
d.insertCard(newCard);
}
return "card";
}

23
src/main/java/MTGClone/controller/HelloController.java

@ -1,23 +0,0 @@ @@ -1,23 +0,0 @@
package MTGClone.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@GetMapping({"/hello"})
public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {
model.addAttribute("name", name);
return "hello";
}
@PostMapping({"/greet"})
public String greet(@RequestParam("name") String name, ModelMap modelMap) {
modelMap.put("name", name);
return "hello";
}
}

5
src/main/webapp/WEB-INF/jsp/card.jsp

@ -42,6 +42,11 @@ table, td, th{ @@ -42,6 +42,11 @@ table, td, th{
<input type="text" id="description" name="description"><br>
<label for="creaturetype">Creature Type:</label>
<input type="text" id="creaturetype" name="creaturetype"><br>
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br>
<br> <input style="width:30%;display:block;margin:0 auto;" type="submit" value="Submit">
</form>
</div>

16
src/main/webapp/WEB-INF/jsp/hello.jsp

@ -1,16 +0,0 @@ @@ -1,16 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello ${name}!</title>
</head>
<body>
<h2 class="hello-title">Hello ${name}!</h2>
<form action="/greet" method="post">
<label for="fname">First name:</label>
<input type="text" id="name" name="name"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Loading…
Cancel
Save