Basic authentication using static sha256 password. TODO: Update for MariaDB
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
package MTGClone;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
|
||||
@ -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");
|
||||
|
||||
@ -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);
|
||||
@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";
|
||||
}
|
||||
|
||||
|
||||
@ -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>
|
||||
|
||||
Reference in New Issue
Block a user