Browse Source

Basic authentication using static sha256 password. TODO: Update for MariaDB

pull/5/head
Gregory Rudolph 4 years ago
parent
commit
d0d96b959c
Signed by: rudi
GPG Key ID: EF64F3CBD1A1EBDD
  1. 20
      src/main/java/MTGClone/SQLDriver.java
  2. 6
      src/main/java/MTGClone/controller/CardController.java
  3. 5
      src/main/webapp/WEB-INF/jsp/card.jsp

20
src/main/java/MTGClone/SQLDriver.java

@ -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");

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

@ -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)) {
Card newCard = new Card(cardname, manacost, power, toughness, description, "", creaturetype);
d.insertCard(newCard); d.insertCard(newCard);
}
return "card"; return "card";
} }

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

@ -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>

Loading…
Cancel
Save