-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler.go
More file actions
164 lines (130 loc) · 4.58 KB
/
handler.go
File metadata and controls
164 lines (130 loc) · 4.58 KB
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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/golang-jwt/jwt"
)
func (s *Server) Login(w http.ResponseWriter, r *http.Request) {
var (
authorizeURL string
encodedCookieValue string
storage AppStorage
err error
)
if authorizeURL, storage.CSRF, err = s.Client.AuthorizeURL(); err != nil {
s.renderError(w, ErrorDetails{http.StatusInternalServerError, fmt.Sprintf("failed to get authorization url: %+v", err)})
return
}
if encodedCookieValue, err = s.SecureCookie.Encode("app", storage); err != nil {
s.renderError(w, ErrorDetails{http.StatusInternalServerError, fmt.Sprintf("error while encoding cookie: %+v", err)})
return
}
http.SetCookie(w, &http.Cookie{
Name: "app",
Value: encodedCookieValue,
Path: "/",
})
http.Redirect(w, r, authorizeURL, http.StatusTemporaryRedirect)
}
func (s *Server) Callback(w http.ResponseWriter, r *http.Request) {
var (
code = r.URL.Query().Get("code")
cookie *http.Cookie
errVal = r.URL.Query().Get("error")
err error
)
if errVal != "" {
s.renderError(w, ErrorDetails{http.StatusBadRequest, fmt.Sprintf("acp returned an error: %s: %s", errVal, r.URL.Query().Get("error_description"))})
return
}
if cookie, err = r.Cookie("app"); err != nil {
s.renderError(w, ErrorDetails{http.StatusBadRequest, fmt.Sprintf("failed to get app cookie: %+v", err)})
return
}
if err = s.SecureCookie.Decode("app", cookie.Value, &s.AppStorage); err != nil {
s.renderError(w, ErrorDetails{http.StatusBadRequest, fmt.Sprintf("failed to decode app storage: %+v", err)})
return
}
if s.AppStorage.Token, err = s.Client.Exchange(code, r.URL.Query().Get("state"), s.AppStorage.CSRF); err != nil {
s.renderError(w, ErrorDetails{http.StatusBadRequest, fmt.Sprintf("failed to get token: %+v", err)})
return
}
http.Redirect(w, r, "/home", http.StatusFound)
}
func (s *Server) Home(w http.ResponseWriter, r *http.Request) {
var (
token *jwt.Token
claims []byte
err error
)
if s.AppStorage.Token.AccessToken == "" {
s.renderError(w, ErrorDetails{http.StatusBadRequest, "missing access token"})
return
}
parser := new(jwt.Parser)
if token, _, err = parser.ParseUnverified(s.AppStorage.Token.AccessToken, jwt.MapClaims{}); err != nil {
s.renderError(w, ErrorDetails{http.StatusBadRequest, fmt.Sprintf("unable to parse token %v", err)})
return
}
if claims, err = json.MarshalIndent(token.Claims, "", "\t"); err != nil {
s.renderError(w, ErrorDetails{http.StatusBadRequest, fmt.Sprintf("unable to format claims from token %v", err)})
return
}
s.Tmpl.ExecuteTemplate(w, "bootstrap", map[string]interface{}{"Token": token.Raw, "UsePyron": s.Config.UsePyron, "FormattedClaims": string(claims)})
}
func (s *Server) Resource(w http.ResponseWriter, r *http.Request) {
var (
res *http.Response
resBodyBytes []byte
err error
)
if s.AppStorage.Token.AccessToken == "" {
s.Tmpl.ExecuteTemplate(w, "error", s.AppStorage.Token)
return
}
if res, err = s.fetchResource(); err != nil {
s.renderError(w, ErrorDetails{http.StatusBadRequest, fmt.Sprintf("client failed to fetch the resource %v", err)})
return
}
defer res.Body.Close()
if res.StatusCode != 200 && res.StatusCode != 403 {
s.renderError(w, ErrorDetails{http.StatusInternalServerError, fmt.Sprintf("unexpected status code returned from resource server %v", err)})
return
}
if resBodyBytes, err = io.ReadAll(res.Body); err != nil {
s.renderError(w, ErrorDetails{http.StatusBadRequest, fmt.Sprintf("unable to fetch the resource %v", err)})
return
}
resourceRes := map[string]interface{}{"Status": res.StatusCode, "Content": string(resBodyBytes)}
s.Tmpl.ExecuteTemplate(w, "resource", resourceRes)
}
func (s *Server) fetchResource() (res *http.Response, err error) {
var req *http.Request
if req, err = s.newHTTPRequest(); err != nil {
return nil, err
}
return s.HttpClient.Do(req)
}
func (s *Server) newHTTPRequest() (req *http.Request, err error) {
if req, err = http.NewRequest("GET", s.Config.ResourceURL, nil); err != nil {
return nil, err
}
req.Header = http.Header{
"Content-Type": []string{"application/json"},
"Authorization": []string{fmt.Sprintf("Bearer %s", s.AppStorage.Token.AccessToken)},
}
// This is for demo purposes only. This can be configured in the environment variables.
if s.Config.InjectCertMode {
req.Header.Add("x-ssl-cert-hash", s.Config.XSSLCertHash)
}
return req, err
}
type ErrorDetails struct {
Status int
Message string
}
func (s *Server) renderError(w http.ResponseWriter, details ErrorDetails) {
s.Tmpl.ExecuteTemplate(w, "error", details)
}