-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.go
171 lines (144 loc) · 4.28 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"context"
"fmt"
"html/template"
"log"
"net/http"
)
func callbackHandler(w http.ResponseWriter, r *http.Request) {
if len(r.URL.Query().Get("error")) > 0 {
fmt.Printf("Got error: %s\n", r.URL.Query().Get("error_description"))
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "<html><body><h1>An error occurred</h1><h2>%s</h2><p>%s</p><p>%s</p><p>%s</p></body></html>", r.URL.Query().Get("error"), r.URL.Query().Get("error_description"), r.URL.Query().Get("error_hint"), r.URL.Query().Get("error_debug"))
return
}
code := r.URL.Query().Get("code")
token, err := h.Conf.Exchange(context.Background(), code)
if err != nil {
log.Print(err)
http.Error(w, "token exchange error", http.StatusBadRequest)
return
}
// Add token to user session.
session, _ := store.Get(r, sessionName)
session.Values["hydra-token"] = token.AccessToken
session.Values["refresh-token"] = token.RefreshToken
// Store the session in the cookie
if err := store.Save(r, w, session); err != nil {
http.Error(w, "Could not persist cookie", http.StatusBadRequest)
return
}
fmt.Println(token.AccessToken)
http.Redirect(w, r, "/", 301)
}
func logoutHandler(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, sessionName)
if err != nil {
fmt.Println(err)
}
session.Options.MaxAge = -1
session.Save(r, w)
w.Write([]byte(`
<html>
<head></head>
<body>
<h2>You are now logged out. Go to <a href="/">main</a></h2>
</body>
</html>`))
return
}
func consentHandler(w http.ResponseWriter, r *http.Request) {
consentRequestId := r.URL.Query().Get("consent_challenge")
if consentRequestId == "" {
http.Error(w, "Consent endpoint was called without a consent request id", http.StatusBadRequest)
return
}
consentRequest, response, err := h.Client.GetConsentRequest(consentRequestId)
if err != nil {
http.Error(w, "The consent request endpoint does not respond", http.StatusBadRequest)
return
} else if response.StatusCode != http.StatusOK {
http.Error(w, "Consent request endpoint", http.StatusBadRequest)
return
}
// TODO: Check if user is authenticated.
if r.Method == "POST" {
// Parse the form
if err := r.ParseForm(); err != nil {
http.Error(w, "Could not parse form", http.StatusBadRequest)
return
}
var grantedScopes = []string{}
for key := range r.PostForm {
grantedScopes = append(grantedScopes, key)
}
redirect := h.AcceptConsent(consentRequestId, grantedScopes)
http.Redirect(w, r, redirect, http.StatusFound)
}
// TODO: Check POST method.
values := map[string]interface{}{
"Scopes": consentRequest.RequestedScope,
"ClientID": consentRequest.Client.ClientId,
}
t, _ := template.New("consent.html").ParseFiles("templates/consent.html")
t.Execute(w, &values)
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
access_token := authenticated(r)
if access_token == "" {
// Perform hydra auth workflow.
authURL := h.AuthURL()
w.Write([]byte(fmt.Sprintf(`
<html>
<head></head>
<body>
<h2>You are not logged in</h2>
<p><a href="%s">Authorize application</a></p>
</body>
</html>`, authURL)))
return
}
// Validate token with Hydra server.
values := map[string]string{
"proxy_url": GetEnv("PROXY_URL", "http://okproxy.logi.co"),
"access_token": access_token,
}
t, err := template.New("index.html").ParseFiles("templates/index.html")
if err != nil {
fmt.Println(err)
}
t.Execute(w, &values)
// w.Write([]byte(fmt.Sprintf(`
// <html>
// <head></head>
// <body>
// <p>You are now logged in and token %s is Active!</p>
// </body>
// </html>`, access_token)))
}
func loginHandler(w http.ResponseWriter, r *http.Request) {
challenge := r.URL.Query().Get("login_challenge")
values := map[string]string{
"Challenge": challenge,
"Error": "",
}
t, _ := template.New("login.html").ParseFiles("templates/login.html")
if r.Method == "POST" {
if err := r.ParseForm(); err != nil {
http.Error(w, "Could not parse form", 500)
return
}
// Checking user credentials
email := r.Form.Get("email")
pw := r.Form.Get("password")
if !d.Validate(email, pw) {
values["Error"] = "Incorrect password"
t.Execute(w, &values)
return
}
redirect := h.AcceptLogin(email, challenge)
http.Redirect(w, r, redirect, http.StatusFound)
}
t.Execute(w, &values)
}