diff --git a/Variables/Variables.go.sample b/Variables/Variables.go.sample index 7533f10..0b1c858 100644 --- a/Variables/Variables.go.sample +++ b/Variables/Variables.go.sample @@ -1,14 +1,32 @@ package Variables +import ( + "github.com/gorilla/sessions" +) + const ( ProjectRoot string = "/path/to/project/root" DbHost string = "localhost" DbPort int = 5432 - DbName string = "personal-website" + DbName string = "db" DbUser string = "postgres" DbPass string = "" DbOpts string = "sslmode=disable" WebserverHost string = ":8080" + + AdminUsername string = "tovi" + // Generate password with bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost) + AdminPassword string = "$2a$04$Q.FgFAfUfqezReI3JyStD.VP8KdjB6UXc8s.PgTgflLG/2yFq.4Hi" +) + +var ( + DefaultSidebarLinks = map[string]string{ + "Email": "mailto:email@example.com", + } + + CookieKey = []byte("super-secret-key") + CookieStore = sessions.NewCookieStore(CookieKey) + CookieName string = "someones-blog-auth" ) diff --git a/Webserver/Admin.go b/Webserver/Admin.go index 97f9428..35c4d38 100644 --- a/Webserver/Admin.go +++ b/Webserver/Admin.go @@ -14,16 +14,17 @@ import ( "github.com/gorilla/mux" "github.com/gorilla/sessions" + "golang.org/x/crypto/bcrypt" ) func CheckAuth(w http.ResponseWriter, r *http.Request) bool { var ( - session *sessions.Session - lastActiveUnix int64 - lastActive time.Time - auth bool - exists bool - e error + session *sessions.Session + lastLoginUnix int64 + lastLogin time.Time + auth bool + exists bool + e error ) session, e = Variables.CookieStore.Get(r, Variables.CookieName) if e != nil { @@ -35,14 +36,14 @@ func CheckAuth(w http.ResponseWriter, r *http.Request) bool { return false } - lastActiveUnix, exists = session.Values["lastActive"].(int64) + lastLoginUnix, exists = session.Values["lastLogin"].(int64) if !exists { return false } - lastActive = time.Unix(lastActiveUnix, 0) + lastLogin = time.Unix(lastLoginUnix, 0) + lastLogin = lastLogin.Add(3 * time.Hour) - lastActive.Add(12 * time.Hour) - if time.Now().Before(lastActive) { + if time.Now().After(lastLogin) { session.Values = make(map[interface{}]interface{}) session.Values["authenticated"] = false session.AddFlash("Login Expired") @@ -54,13 +55,6 @@ func CheckAuth(w http.ResponseWriter, r *http.Request) bool { return false } - session.Values["lastLogin"] = time.Now().Unix() - e = session.Save(r, w) - if e != nil { - log.Println(e.Error()) - return false - } - return true } @@ -107,6 +101,47 @@ func AdminView(w http.ResponseWriter, r *http.Request) { } } +func comparePasswords(hashedPwd, plainPwd string) bool { + var ( + e error + ) + e = bcrypt.CompareHashAndPassword( + []byte(hashedPwd), + []byte(plainPwd), + ) + if e != nil { + return false + } + + return true +} + +func AdminLogout(w http.ResponseWriter, r *http.Request) { + var ( + session *sessions.Session + e error + ) + + if !CheckAuth(w, r) { + http.Redirect(w, r, "/admin/login", 302) + return + } + + session, e = Variables.CookieStore.Get(r, Variables.CookieName) + if e != nil { + log.Println("Could not get session cookie") + http.Error(w, "Error", http.StatusInternalServerError) + return + } + + session.Values["authenticated"] = false + session.Values["lastLogin"] = nil + session.Save(r, w) + + http.Redirect(w, r, "/admin/logout", 302) + return +} + func AdminLogin(w http.ResponseWriter, r *http.Request) { var ( session *sessions.Session @@ -154,7 +189,7 @@ func AdminLogin(w http.ResponseWriter, r *http.Request) { username = r.FormValue("username") password = r.FormValue("password") - if username != Variables.AdminPassword && password != Variables.AdminPassword { + if username != Variables.AdminPassword && !comparePasswords(Variables.AdminPassword, password) { session.AddFlash("Invalid Username or Password") e = session.Save(r, w) if e != nil { @@ -166,7 +201,7 @@ func AdminLogin(w http.ResponseWriter, r *http.Request) { } session.Values["authenticated"] = true - session.Values["lastActive"] = time.Now().Unix() + session.Values["lastLogin"] = time.Now().Unix() session.Save(r, w) http.Redirect(w, r, "/admin", 302) diff --git a/Webserver/Index.go b/Webserver/Index.go index 38f355a..c43f828 100644 --- a/Webserver/Index.go +++ b/Webserver/Index.go @@ -1,6 +1,7 @@ package Webserver import ( + "log" "net/http" "PersonalWebsite/Database" @@ -15,9 +16,31 @@ func ViewIndex(w http.ResponseWriter, r *http.Request) { v["PageView"] = "index-intro.gohtml" v["Posts"], e = Database.GetPostsList(5, 0) if e != nil { - // TODO: Handle this - http.Error(w, "Error", http.StatusInternalServerError) + log.Println(e) + http.Redirect(w, r, "/error", 302) } ServeTemplate(w, r, "html/index.gohtml", v) } + +func ViewLinks(w http.ResponseWriter, r *http.Request) { + var ( + v = make(map[string]interface{}) + ) + + v["PageView"] = "index-links.gohtml" + + ServeTemplate(w, r, "html/index.gohtml", v) + +} + +func ViewError(w http.ResponseWriter, r *http.Request) { + var ( + v = make(map[string]interface{}) + ) + + v["PageView"] = "error.gohtml" + + ServeTemplate(w, r, "html/index.gohtml", v) + +} diff --git a/Webserver/Post.go b/Webserver/Post.go index 91cbfbe..8bde5b6 100644 --- a/Webserver/Post.go +++ b/Webserver/Post.go @@ -6,6 +6,7 @@ import ( "strconv" "PersonalWebsite/Database" + "PersonalWebsite/Helper" "github.com/gorilla/mux" ) @@ -22,9 +23,15 @@ func ViewPost(w http.ResponseWriter, r *http.Request) { post, e = Database.GetPostById(urlParams["id"]) if e != nil { - // TODO: Forward 404 - log.Println("Could not get post") - http.Error(w, "Error", http.StatusInternalServerError) + log.Println(e) + http.Redirect(w, r, "/error", 302) + return + } + + post.Body, e = Helper.GetFileContents(post.HtmlPath) + if e != nil { + log.Println(e) + http.Redirect(w, r, "/error", 302) return } @@ -58,8 +65,8 @@ func ViewPostList(w http.ResponseWriter, r *http.Request, subject string) { } else { page, e = strconv.Atoi(keys[0]) if e != nil { - // TODO: Handle this - http.Error(w, "Error", http.StatusInternalServerError) + log.Println(e) + http.Redirect(w, r, "/error", 302) return } } @@ -68,15 +75,15 @@ func ViewPostList(w http.ResponseWriter, r *http.Request, subject string) { posts, e = Database.GetPostsListBySubject(limit, offset, subject) if e != nil { - // TODO: Handle this - http.Error(w, "Error", http.StatusInternalServerError) + log.Println(e) + http.Redirect(w, r, "/error", 302) return } postsCount, e = Database.GetPostCountBySubject(subject) if e != nil { - // TODO: Handle this - http.Error(w, "Error", http.StatusInternalServerError) + log.Println(e) + http.Redirect(w, r, "/error", 302) return } diff --git a/Webserver/ServeFile.go b/Webserver/ServeFile.go index 9b81784..311fdba 100644 --- a/Webserver/ServeFile.go +++ b/Webserver/ServeFile.go @@ -1,6 +1,7 @@ package Webserver import ( + "log" "net/http" "path" "text/template" @@ -18,9 +19,11 @@ var ( "html/header.gohtml", "html/sidebar.gohtml", "html/index-intro.gohtml", + "html/index-links.gohtml", "html/index-post-list.gohtml", "html/post-list.gohtml", "html/post.gohtml", + "html/error.gohtml", } ) @@ -42,8 +45,9 @@ func ServeTemplate(w http.ResponseWriter, r *http.Request, mainFile string, v ma v["SidebarLinks"], e = Database.GetAllSidebarLinks() if e != nil { - // TODO: Handle - panic(e) + log.Println(e) + http.Redirect(w, r, "/error", 302) + return } files = []string{webRootJoin(mainFile)} @@ -64,14 +68,15 @@ func ServeTemplate(w http.ResponseWriter, r *http.Request, mainFile string, v ma }, ).ParseFiles(files...) if e != nil { - // TODO: Handle this - panic(e) + log.Println(e) + http.Redirect(w, r, "/error", 302) } w.Header().Set("Content-type", "text/html") e = tpl.Execute(w, v) if e != nil { - // TODO: Handle this - panic(e) + log.Println(e) + http.Redirect(w, r, "/error", 302) + return } } diff --git a/Webserver/Webserver.go b/Webserver/Webserver.go index 5a36e7e..34d3f50 100644 --- a/Webserver/Webserver.go +++ b/Webserver/Webserver.go @@ -58,13 +58,16 @@ func Start() error { // Interface endpoints r.HandleFunc("/", ViewIndex) + r.HandleFunc("/links", ViewLinks) r.HandleFunc("/programming", ViewPostListProgramming) r.HandleFunc("/pentesting", ViewPostListPentesting) r.HandleFunc("/post/{id}", ViewPost) + r.HandleFunc("/error", ViewError) // Administration r.HandleFunc("/admin", AdminView) r.HandleFunc("/admin/login", AdminLogin) + r.HandleFunc("/admin/logout", AdminLogout) r.HandleFunc("/admin/post/new", AdminNewPost) r.HandleFunc("/admin/post/{id}/edit", AdminEditPost) diff --git a/web/css/main.css b/web/css/main.css index 26f0c88..9f04fb3 100644 --- a/web/css/main.css +++ b/web/css/main.css @@ -243,7 +243,9 @@ pre::-webkit-scrollbar-color { } .post-icon { - max-width: 200px !important; + max-width: 50% !important; + max-height: 20em !important; + padding-bottom: 2em; } .icon-div { diff --git a/web/html/error.gohtml b/web/html/error.gohtml new file mode 100644 index 0000000..276dfd6 --- /dev/null +++ b/web/html/error.gohtml @@ -0,0 +1,3 @@ +

Whoops! It looks like you've run into an error!

+ +

Please click here to go back to the home screen

diff --git a/web/html/header.gohtml b/web/html/header.gohtml index 2ecdb50..4a3035a 100644 --- a/web/html/header.gohtml +++ b/web/html/header.gohtml @@ -7,7 +7,7 @@