-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (66 loc) · 2.06 KB
/
main.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
package main
import (
"encoding/json"
"log"
"net/http"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", validateToken(homeHandler))
log.Println("Starting backend server on :8082")
log.Fatal(http.ListenAndServe(":8082", mux))
}
func validateToken(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
authHeader := r.Header.Get("Authorization")
if authHeader == "" {
http.Error(w, "No token provided", http.StatusUnauthorized)
return
}
// Call auth server's introspection endpoint
req, err := http.NewRequest("POST", "http://localhost:8080/introspect", nil)
if err != nil {
http.Error(w, "Server error", http.StatusInternalServerError)
return
}
// Forward the same Authorization header
req.Header.Set("Authorization", authHeader)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("Error calling introspection: %v", err)
http.Error(w, "Failed to validate token", http.StatusUnauthorized)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("Introspection returned status: %d", resp.StatusCode)
http.Error(w, "Invalid token", http.StatusUnauthorized)
return
}
var introspectionResp struct {
Usable bool `json:"usable"`
Subject string `json:"subject"`
Scopes []string `json:"scopes"`
}
if err := json.NewDecoder(resp.Body).Decode(&introspectionResp); err != nil {
log.Printf("Error decoding introspection response: %v", err)
http.Error(w, "Server error", http.StatusInternalServerError)
return
}
if !introspectionResp.Usable {
http.Error(w, "Token not active", http.StatusUnauthorized)
return
}
log.Printf("Token validated successfully for subject: %s", introspectionResp.Subject)
next(w, r)
}
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
response := map[string]string{
"message": "Welcome to Protected Backend API Server",
"status": "authenticated",
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}