Merge pull request #5 from Rudi9719/master
Added authentication for card creation
This commit is contained in:
@ -1,5 +1,6 @@
|
|||||||
package MTGClone;
|
package MTGClone;
|
||||||
|
|
||||||
|
import java.security.MessageDigest;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@ -16,7 +17,26 @@ public class SQLDriver {
|
|||||||
}
|
}
|
||||||
return true;
|
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() {
|
public Card getRandomCard() {
|
||||||
try {
|
try {
|
||||||
c = DriverManager.getConnection("jdbc:sqlite:cards.db");
|
c = DriverManager.getConnection("jdbc:sqlite:cards.db");
|
||||||
|
|||||||
@ -24,10 +24,12 @@ public class CardController {
|
|||||||
@PostMapping({"/card"})
|
@PostMapping({"/card"})
|
||||||
public String greet(@RequestParam("cardname") String cardname, @RequestParam("manacost") int manacost,
|
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,
|
@RequestParam("power") int power, @RequestParam("toughness") int toughness, @RequestParam("description") String description, @RequestParam("creaturetype") String creaturetype,
|
||||||
ModelMap modelMap) {
|
@RequestParam("username") String username, @RequestParam("password") String password, ModelMap modelMap) {
|
||||||
Card newCard = new Card(cardname, manacost, power, toughness, description, "", creaturetype);
|
SQLDriver d = new SQLDriver();
|
||||||
SQLDriver d = new SQLDriver();
|
if (d.authenticateUser(username, password)) {
|
||||||
d.insertCard(newCard);
|
Card newCard = new Card(cardname, manacost, power, toughness, description, "", creaturetype);
|
||||||
|
d.insertCard(newCard);
|
||||||
|
}
|
||||||
return "card";
|
return "card";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -42,6 +42,11 @@ table, td, th{
|
|||||||
<input type="text" id="description" name="description"><br>
|
<input type="text" id="description" name="description"><br>
|
||||||
<label for="creaturetype">Creature Type:</label>
|
<label for="creaturetype">Creature Type:</label>
|
||||||
<input type="text" id="creaturetype" name="creaturetype"><br>
|
<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">
|
<br> <input style="width:30%;display:block;margin:0 auto;" type="submit" value="Submit">
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -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>
|
|
||||||
Reference in New Issue
Block a user