Photoblog 1: auth from authcookie exercise
This commit is contained in:
parent
3df5ff763d
commit
f20e3d6aed
1
.gitignore
vendored
1
.gitignore
vendored
@ -63,3 +63,4 @@ webcounter/webcounter
|
||||
authcookie/authcookie
|
||||
cert.pem
|
||||
key.pem
|
||||
photoblog/photoblog
|
||||
|
52
photoblog/admin/admin.go
Normal file
52
photoblog/admin/admin.go
Normal file
@ -0,0 +1,52 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
)
|
||||
|
||||
type AuthCookie struct {
|
||||
Templates *template.Template
|
||||
Store *sessions.CookieStore
|
||||
}
|
||||
|
||||
func (app *AuthCookie) LoginPage(res http.ResponseWriter, req *http.Request) {
|
||||
formErr := make(map[string]string)
|
||||
switch req.Method {
|
||||
case "POST":
|
||||
username := req.FormValue("username")
|
||||
if username == "" {
|
||||
formErr["username"] = "Empty username"
|
||||
}
|
||||
password := req.FormValue("password")
|
||||
if password == "" {
|
||||
formErr["password"] = "Empty password"
|
||||
}
|
||||
if len(formErr) == 0 {
|
||||
app.SaveUsername(username, res, req)
|
||||
RedirectHome(res, req)
|
||||
return
|
||||
}
|
||||
fallthrough
|
||||
case "GET":
|
||||
app.Templates.ExecuteTemplate(res, "login.html", formErr)
|
||||
}
|
||||
}
|
||||
func (app *AuthCookie) LogoutPage(res http.ResponseWriter, req *http.Request) {
|
||||
app.SaveUsername("", res, req)
|
||||
RedirectHome(res, req)
|
||||
}
|
||||
func (app *AuthCookie) CurrentSession(res http.ResponseWriter, req *http.Request) *sessions.Session {
|
||||
session, _ := app.Store.Get(req, "session")
|
||||
return session
|
||||
}
|
||||
func (app *AuthCookie) SaveUsername(username string, res http.ResponseWriter, req *http.Request) {
|
||||
session := app.CurrentSession(res, req)
|
||||
session.Values["username"] = username
|
||||
session.Save(req, res)
|
||||
}
|
||||
func RedirectHome(res http.ResponseWriter, req *http.Request) {
|
||||
http.Redirect(res, req, "/", http.StatusSeeOther)
|
||||
}
|
39
photoblog/main.go
Normal file
39
photoblog/main.go
Normal file
@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"meutel.net/meutel/go-examples/photoblog/admin"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
)
|
||||
|
||||
type PhotoBlog struct {
|
||||
*admin.AuthCookie
|
||||
Templates *template.Template
|
||||
}
|
||||
|
||||
func (app *PhotoBlog) HomePage(res http.ResponseWriter, req *http.Request) {
|
||||
app.Templates.ExecuteTemplate(res, "home.html", app.CurrentSession(res, req).Values)
|
||||
}
|
||||
|
||||
func main() {
|
||||
tpl, err := template.ParseGlob("templates/*.html")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
admin := admin.AuthCookie{
|
||||
Templates: tpl,
|
||||
Store: sessions.NewCookieStore([]byte("flQ6QzM/c3Jtdl9ycDx6OXRIfFgK")),
|
||||
}
|
||||
app := PhotoBlog{
|
||||
AuthCookie: &admin,
|
||||
Templates: tpl,
|
||||
}
|
||||
http.HandleFunc("/", app.HomePage)
|
||||
http.HandleFunc("/login", app.LoginPage)
|
||||
http.HandleFunc("/logout", app.LogoutPage)
|
||||
http.ListenAndServeTLS(":9443", "../cert.pem", "../key.pem", nil)
|
||||
}
|
2
photoblog/templates/footer.html
Normal file
2
photoblog/templates/footer.html
Normal file
@ -0,0 +1,2 @@
|
||||
</body>
|
||||
</html>
|
4
photoblog/templates/header.html
Normal file
4
photoblog/templates/header.html
Normal file
@ -0,0 +1,4 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head><title>Auth cookie</title></head>
|
||||
<body>
|
10
photoblog/templates/home.html
Normal file
10
photoblog/templates/home.html
Normal file
@ -0,0 +1,10 @@
|
||||
{{ template "header.html" }}
|
||||
<div>
|
||||
{{ if .username }}
|
||||
<a href="/logout">Logout</a>
|
||||
{{ else }}
|
||||
<a href="/login">Login</a>
|
||||
{{ end }}
|
||||
</div>
|
||||
<h1>Hello {{ .username }}</h1>
|
||||
{{ template "footer.html" }}
|
16
photoblog/templates/login.html
Normal file
16
photoblog/templates/login.html
Normal file
@ -0,0 +1,16 @@
|
||||
{{ template "header.html" }}
|
||||
<h1>Authentification</h1>
|
||||
<form action="/login" method="POST">
|
||||
<label>
|
||||
Username: {{ if .username }}<b>{{ .username }}</b>{{ end }}
|
||||
<input type="text" name="username">
|
||||
</label>
|
||||
<br>
|
||||
<label>
|
||||
Password: {{ if .password }}<b>{{ .password }}</b>{{ end }}
|
||||
<input type="password" name="password">
|
||||
</label>
|
||||
<br>
|
||||
<input type="submit">
|
||||
</form>
|
||||
{{ template "footer.html" }}
|
Loading…
Reference in New Issue
Block a user