-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.go
More file actions
375 lines (335 loc) · 10.5 KB
/
Copy pathhandlers.go
File metadata and controls
375 lines (335 loc) · 10.5 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"time"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
// Server holds the HTTP server configuration and dependencies.
type Server struct {
store *TokenStore
oauthConfig *oauth2.Config
mux *http.ServeMux
exchangeFunc func(ctx context.Context, code string) (*oauth2.Token, error)
m365Enabled bool
m365ClientID string
m365TenantID string
m365AdminToken string
publicBaseURL string
m365Sessions *m365SessionStore
}
// NewServer creates a new Server with the given configuration.
func NewServer(store *TokenStore, clientID, clientSecret, redirectURL string) *Server {
config := &oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
Endpoint: google.Endpoint,
RedirectURL: redirectURL,
Scopes: []string{
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/gmail.modify",
"https://www.googleapis.com/auth/calendar",
},
}
s := &Server{
store: store,
oauthConfig: config,
mux: http.NewServeMux(),
}
// Default exchange function uses the real OAuth config
s.exchangeFunc = func(ctx context.Context, code string) (*oauth2.Token, error) {
return config.Exchange(ctx, code, oauth2.AccessTypeOffline)
}
s.registerRoutes()
return s
}
// registerRoutes sets up all HTTP routes.
func (s *Server) registerRoutes() {
s.mux.HandleFunc("/health", s.handleHealth)
s.mux.HandleFunc("/callback", s.handleCallback)
s.mux.HandleFunc("/token/", s.handleToken)
s.mux.HandleFunc("/status/", s.handleStatus)
}
// ServeHTTP implements the http.Handler interface.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.mux.ServeHTTP(w, r)
}
// HealthResponse represents the JSON response from /health.
type HealthResponse struct {
Status string `json:"status"`
Timestamp string `json:"timestamp"`
}
// handleHealth returns a simple health check response.
func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
resp := HealthResponse{
Status: "ok",
Timestamp: time.Now().UTC().Format(time.RFC3339),
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error encoding health response: %v", err)
}
}
// handleCallback processes the OAuth callback from Google.
func (s *Server) handleCallback(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
code := r.URL.Query().Get("code")
state := r.URL.Query().Get("state")
if code == "" {
s.renderErrorPage(w, "Missing authorization code", http.StatusBadRequest)
return
}
if state == "" {
s.renderErrorPage(w, "Missing state parameter", http.StatusBadRequest)
return
}
// Check for OAuth error response
if errParam := r.URL.Query().Get("error"); errParam != "" {
errDesc := r.URL.Query().Get("error_description")
s.renderErrorPage(w, fmt.Sprintf("OAuth error: %s - %s", errParam, errDesc), http.StatusBadRequest)
return
}
// Exchange the authorization code for tokens
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
defer cancel()
token, err := s.exchangeFunc(ctx, code)
if err != nil {
log.Printf("Token exchange failed for state %s: %v", state, err)
s.renderErrorPage(w, "Failed to exchange authorization code for token", http.StatusInternalServerError)
return
}
// Store the token
s.store.Store(state, token)
log.Printf("Token stored for state: %s", state)
// Return success HTML page
s.renderSuccessPage(w, state)
}
// handleToken returns the token for the given state.
func (s *Server) handleToken(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Extract state from path: /token/{state}
state := strings.TrimPrefix(r.URL.Path, "/token/")
if state == "" {
http.Error(w, `{"error": "Missing state parameter"}`, http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
token, status := s.store.Get(state)
switch status {
case TokenStatusReady:
resp := TokenResponse{
AccessToken: token.AccessToken,
RefreshToken: token.RefreshToken,
TokenType: token.TokenType,
Expiry: token.Expiry.Format(time.RFC3339),
}
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error encoding token response: %v", err)
}
case TokenStatusPending:
w.WriteHeader(http.StatusAccepted)
if err := json.NewEncoder(w).Encode(map[string]string{
"status": "pending",
"message": "Token not yet available, please try again",
}); err != nil {
log.Printf("Error encoding pending response: %v", err)
}
case TokenStatusConsumed:
w.WriteHeader(http.StatusGone)
if err := json.NewEncoder(w).Encode(map[string]string{
"error": "consumed",
"message": "Token has already been retrieved",
}); err != nil {
log.Printf("Error encoding consumed response: %v", err)
}
case TokenStatusNotFound:
w.WriteHeader(http.StatusNotFound)
if err := json.NewEncoder(w).Encode(map[string]string{
"error": "not_found",
"message": "Token not found or expired",
}); err != nil {
log.Printf("Error encoding not found response: %v", err)
}
}
}
// handleStatus checks the status of a token without consuming it.
func (s *Server) handleStatus(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Extract state from path: /status/{state}
state := strings.TrimPrefix(r.URL.Path, "/status/")
if state == "" {
http.Error(w, `{"error": "Missing state parameter"}`, http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
status := s.store.Status(state)
var statusStr string
switch status {
case TokenStatusReady:
statusStr = "ready"
case TokenStatusPending:
statusStr = "pending"
case TokenStatusConsumed:
statusStr = "consumed"
case TokenStatusNotFound:
statusStr = "not_found"
}
if err := json.NewEncoder(w).Encode(map[string]string{
"status": statusStr,
}); err != nil {
log.Printf("Error encoding status response: %v", err)
}
}
// TokenResponse represents the JSON response containing OAuth tokens.
type TokenResponse struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
TokenType string `json:"token_type"`
Expiry string `json:"expiry"`
}
// renderSuccessPage renders an HTML success page for the OAuth callback.
func (s *Server) renderSuccessPage(w http.ResponseWriter, state string) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
html := fmt.Sprintf(`<!DOCTYPE html>
<html>
<head>
<title>Authorization Successful</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
background: linear-gradient(135deg, #667eea 0%%, #764ba2 100%%);
}
.container {
background: white;
padding: 40px;
border-radius: 16px;
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
text-align: center;
max-width: 400px;
}
.success-icon {
font-size: 64px;
margin-bottom: 20px;
}
h1 {
color: #22c55e;
margin-bottom: 16px;
}
p {
color: #666;
line-height: 1.6;
}
.state {
font-family: monospace;
background: #f3f4f6;
padding: 8px 12px;
border-radius: 6px;
font-size: 14px;
word-break: break-all;
}
</style>
</head>
<body>
<div class="container">
<div class="success-icon">✅</div>
<h1>Authorization Successful</h1>
<p>You have successfully authorized the application.</p>
<p>You can close this window and return to your terminal.</p>
<p style="margin-top: 24px; font-size: 12px; color: #999;">
State: <span class="state">%s</span>
</p>
</div>
</body>
</html>`, state)
if _, err := w.Write([]byte(html)); err != nil {
log.Printf("Error writing success page: %v", err)
}
}
// renderErrorPage renders an HTML error page.
func (s *Server) renderErrorPage(w http.ResponseWriter, message string, statusCode int) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(statusCode)
html := fmt.Sprintf(`<!DOCTYPE html>
<html>
<head>
<title>Authorization Failed</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
background: linear-gradient(135deg, #667eea 0%%, #764ba2 100%%);
}
.container {
background: white;
padding: 40px;
border-radius: 16px;
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
text-align: center;
max-width: 400px;
}
.error-icon {
font-size: 64px;
margin-bottom: 20px;
}
h1 {
color: #ef4444;
margin-bottom: 16px;
}
p {
color: #666;
line-height: 1.6;
}
.error-message {
background: #fef2f2;
color: #b91c1c;
padding: 12px;
border-radius: 8px;
margin-top: 16px;
}
</style>
</head>
<body>
<div class="container">
<div class="error-icon">❌</div>
<h1>Authorization Failed</h1>
<p>There was a problem completing the authorization.</p>
<div class="error-message">%s</div>
<p style="margin-top: 24px;">Please try again or contact support.</p>
</div>
</body>
</html>`, message)
if _, err := w.Write([]byte(html)); err != nil {
log.Printf("Error writing error page: %v", err)
}
}