diff options
Diffstat (limited to 'charakterin.go')
-rw-r--r-- | charakterin.go | 66 |
1 files changed, 59 insertions, 7 deletions
diff --git a/charakterin.go b/charakterin.go index bb7323d..202e8b2 100644 --- a/charakterin.go +++ b/charakterin.go | |||
@@ -2,8 +2,11 @@ package charakterin | |||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "database/sql" | 4 | "database/sql" |
5 | "io/ioutil" | ||
5 | "log" | 6 | "log" |
6 | "net/http" | 7 | "net/http" |
8 | "net/url" | ||
9 | "time" | ||
7 | 10 | ||
8 | _ "github.com/lib/pq" | 11 | _ "github.com/lib/pq" |
9 | ) | 12 | ) |
@@ -39,8 +42,8 @@ func (c *Charakterin) UseRenderer(renderer Renderer) { | |||
39 | c.renderer = renderer | 42 | c.renderer = renderer |
40 | } | 43 | } |
41 | 44 | ||
42 | // DisplayLogin zeigt die Route für den Login an, wenn der User nicht bereits eingeloggt ist. | 45 | // DisplayLoginWithData rendert die Loginseite mit Daten (vorheriger Benutzer, Fehlermeldung) |
43 | func (c *Charakterin) DisplayLogin(w http.ResponseWriter, r *http.Request) { | 46 | func (c *Charakterin) DisplayLoginWithData(w http.ResponseWriter, r *http.Request, data map[string]interface{}) { |
44 | if c.IsLoggedIn(r) { | 47 | if c.IsLoggedIn(r) { |
45 | http.Redirect(w, r, c.FallbackRoute, 302) | 48 | http.Redirect(w, r, c.FallbackRoute, 302) |
46 | return | 49 | return |
@@ -51,17 +54,66 @@ func (c *Charakterin) DisplayLogin(w http.ResponseWriter, r *http.Request) { | |||
51 | return | 54 | return |
52 | } | 55 | } |
53 | 56 | ||
54 | http.SetCookie(w, &http.Cookie{ | 57 | if _, ok := data["previous_user"]; !ok { |
55 | Name: "session", | 58 | data["previous_user"] = "" |
56 | Value: "benis", | 59 | } |
57 | }) | 60 | if _, ok := data["error"]; !ok { |
61 | data["error"] = "" | ||
62 | } | ||
63 | |||
64 | c.renderer.RenderLoginPage(w, data) | ||
65 | } | ||
58 | 66 | ||
59 | c.renderer.RenderLoginPage(w, make(map[string]interface{})) | 67 | // DisplayLogin zeigt die Route für den Login an, wenn der User nicht bereits eingeloggt ist. |
68 | func (c *Charakterin) DisplayLogin(w http.ResponseWriter, r *http.Request) { | ||
69 | c.DisplayLoginWithData(w, r, make(map[string]interface{})) | ||
60 | } | 70 | } |
61 | 71 | ||
62 | // Login versucht einen User einzuloggen. | 72 | // Login versucht einen User einzuloggen. |
63 | func (c *Charakterin) Login(w http.ResponseWriter, r *http.Request) { | 73 | func (c *Charakterin) Login(w http.ResponseWriter, r *http.Request) { |
74 | if r.Method != "POST" { | ||
75 | return | ||
76 | } | ||
77 | |||
78 | // POST-Data lesen | ||
79 | defer r.Body.Close() | ||
80 | data, err := ioutil.ReadAll(r.Body) | ||
81 | if err != nil { | ||
82 | http.Error(w, err.Error(), http.StatusInternalServerError) | ||
83 | return | ||
84 | } | ||
85 | |||
86 | values, err := url.ParseQuery(string(data)) | ||
87 | if err != nil { | ||
88 | http.Error(w, err.Error(), http.StatusInternalServerError) | ||
89 | return | ||
90 | } | ||
91 | |||
92 | username := values.Get("username") | ||
93 | var result string | ||
94 | err = c.Database.QueryRow("SELECT * FROM login.new_session($1, $2)", username, values.Get("password")).Scan(&result) | ||
95 | if err != nil { | ||
96 | errStr := err.Error() | ||
97 | |||
98 | if errStr == "pq: no_such_user" || errStr == "pq: invalid_password" { | ||
99 | log.Printf("invalid login attempt by '%s': %s\n", username, errStr[4:]) | ||
100 | data := make(map[string]interface{}) | ||
101 | data["previous_user"] = username | ||
102 | data["error"] = "Ungültiger Benutzername oder Passwort. Oder Lukas hats mal wieder kaputt gemacht." | ||
103 | c.DisplayLoginWithData(w, r, data) | ||
104 | return | ||
105 | } | ||
64 | 106 | ||
107 | http.Error(w, errStr, http.StatusInternalServerError) | ||
108 | return | ||
109 | } | ||
110 | |||
111 | http.SetCookie(w, &http.Cookie{ | ||
112 | Name: "session", | ||
113 | Value: result, | ||
114 | Expires: time.Now().AddDate(1, 0, 0), | ||
115 | }) | ||
116 | http.Redirect(w, r, c.FallbackRoute, 302) | ||
65 | } | 117 | } |
66 | 118 | ||
67 | // IsLoggedIn überprüft anhand eines Request, ob der User eingeloggt ist. | 119 | // IsLoggedIn überprüft anhand eines Request, ob der User eingeloggt ist. |