summaryrefslogtreecommitdiff
path: root/signup.go
diff options
context:
space:
mode:
authorNikolaus Gotsche <n@softwarefools.com>2017-09-24 22:51:36 +0200
committerNikolaus Gotsche <n@softwarefools.com>2017-09-24 22:51:36 +0200
commit7e33d4d446d6b7d9a136290993363f833fc13908 (patch)
treedd7ee817baecdbf7105862a3e1ce5277020323b4 /signup.go
First Commit of Project Hexfool
getuuid - Demonstration how linux's uuidgen can be used in go jsonrest - api for restful json usage signup - login on top of an mysql db
Diffstat (limited to 'signup.go')
-rw-r--r--signup.go105
1 files changed, 105 insertions, 0 deletions
diff --git a/signup.go b/signup.go
new file mode 100644
index 0000000..e659505
--- /dev/null
+++ b/signup.go
@@ -0,0 +1,105 @@
+package main
+
+import "database/sql"
+import _ "github.com/go-sql-driver/mysql"
+
+import "golang.org/x/crypto/bcrypt"
+
+import "net/http"
+
+import "os/exec"
+
+var db *sql.DB
+var err error
+
+func signupPage(res http.ResponseWriter, req *http.Request) {
+ if req.Method != "POST" {
+ http.ServeFile(res, req, "html/signup.html")
+ return
+ }
+
+ username := req.FormValue("username")
+ password := req.FormValue("password")
+
+ var user string
+
+ err := db.QueryRow("SELECT username FROM players WHERE username=?",username).Scan(&user)
+
+ switch {
+ case err == sql.ErrNoRows:
+ hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
+ if err != nil {
+ http.Error(res, "Server Error creating Password. Unable to create account! You Fool...", 500)
+ return
+ }
+ newuuid, uiderr := exec.Command("uuidgen").Output()
+ if uiderr != nil{
+ http.Error(res, "Server Error creating UUID. Unable to create account! You Fool...", 500)
+ return
+ }
+
+ _, err = db.Exec("INSERT INTO players(username, password, uuid) VALUES(?, ?, ?)", username, hashedPassword, newuuid)
+ if err != nil {
+ http.Error(res, "Server Error Inserting User. Unable to create account! You Fool...", 500)
+ return
+ }
+ res.Write([]byte("User Created Successfully!"))
+ return
+ case err != nil:
+ http.Error(res, "Server Error! Something fucked up", 500)
+ return
+ default:
+ http.Redirect(res, req, "/", 301)
+ }
+}
+
+func loginPage(res http.ResponseWriter, req *http.Request) {
+ if req.Method != "POST" {
+ http.ServeFile(res, req, "html/login.html")
+ return
+ }
+
+ username := req.FormValue("username")
+ password := req.FormValue("password")
+
+ var databaseUsername string
+ var databasePassword string
+ var databaseUUID string
+
+ err := db.QueryRow("SELECT username, password, uuid FROM players WHERE username=?", username).Scan(&databaseUsername, &databasePassword, &databaseUUID)
+
+ if err != nil {
+ http.Redirect(res, req, "/login", 301)
+ return
+ }
+
+ err = bcrypt.CompareHashAndPassword([]byte(databasePassword), []byte(password))
+ if err != nil {
+ http.Redirect(res, req, "/login", 301)
+ return
+ }
+
+ res.Write([]byte("Hello "+ databaseUsername + "! Your UUID is "+ databaseUUID))
+}
+
+func homePage(res http.ResponseWriter, req *http.Request) {
+ http.ServeFile(res, req, "html/index.html")
+}
+
+func main() {
+ db, err = sql.Open("mysql", "hexmaster:waSIdocHneTdUobeRoarscH@tcp(192.168.0.16)/hexmaster")
+ if err != nil {
+ panic(err.Error())
+ }
+ defer db.Close()
+
+ err = db.Ping()
+ if err != nil {
+ panic(err.Error())
+ }
+
+ http.HandleFunc("/signup", signupPage)
+ http.HandleFunc("/login", loginPage)
+ http.HandleFunc("/", homePage)
+ http.ListenAndServe(":8088", nil)
+}