-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthenticate.go
More file actions
128 lines (107 loc) · 3.51 KB
/
authenticate.go
File metadata and controls
128 lines (107 loc) · 3.51 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
package main
import (
"encoding/json"
"fmt"
"html"
"log"
"net/http"
"net/url"
"github.com/kataras/jwt"
)
func authenticate(w http.ResponseWriter, r *http.Request) {
/* GET parameters from URL */
var tokenString string
var service string
// Variables
var parsedURI *url.URL
var err error
// Figure out if this is an authentication request from Traefik
// If it is from Traefik, we recognize it by the X-Forwarded-Uri header being set
forwardedURI := r.Header.Get("X-Forwarded-Uri")
if forwardedURI != "" {
// This is a Traefik request
// Parse URI first
parsedURI, err = url.Parse(forwardedURI)
if err != nil {
// Can not parse URI -> Invalid
log.Println("Can not parse URI: ", forwardedURI)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
// Set service (we can do this hardcoded here as the noVNC service is the only consumer of these statements)
service = "noVNC"
// Check if the VNC cookie is already present
cookie, err := r.Cookie("vnc_token")
// Cookie is not set -> Store the JWT token as VNC cookie and retry
if err != nil {
// Get token from parsed URI
tokenString = parsedURI.Query().Get("token")
// Set cookie
http.SetCookie(w, &http.Cookie{
Name: "vnc_token",
Value: tokenString,
Path: "/",
MaxAge: 3600 * 12,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
})
// To be able to actually store the cookie we must redirect now
// As we want to retry with the original request, go back to the original URL now
// Reconstruct original URL from headers and noVNC subdir variable
x_forwarded_proto := r.Header.Get("X-Forwarded-Proto")
x_forwarded_host := r.Header.Get("X-Forwarded-Host")
x_forwarded_uri := r.Header.Get("X-Forwarded-Uri")
redirectTargetURL := fmt.Sprintf("%s://%s%s%s",
x_forwarded_proto, x_forwarded_host, noVNCSubdir, x_forwarded_uri)
// Send the redirect
http.Redirect(w, r, redirectTargetURL, http.StatusFound)
// The browser now redirects back and sets the cookie
// By the redirection, the request will be sent once again
// As the cookie is now set, this routine will be skipped then
return
}
// Cookie is set -> hand over cookie value as JWT token value
tokenString = cookie.Value
} else {
// Get token and service strings from URL
log.Println("Received request: ", r.URL)
tokenString = r.URL.Query().Get("token")
service = r.URL.Query().Get("service")
}
// escape input
tokenString = html.EscapeString(tokenString)
service = html.EscapeString(service)
// Prepare response
result := AuthenticationResponse{false}
/* Check if token and service are present */
if tokenString != "" && service != "" {
/* Verify token */
verifiedToken, err := jwt.Verify(jwt.HS256, []byte(jwtKey), []byte(tokenString))
if err != nil {
log.Println(err)
} else {
var claims RulesCustomClaims
err := verifiedToken.Claims(&claims)
if err != nil {
log.Println(err)
} else {
result.Allowed = decide(service, claims)
}
}
} else {
http.Error(w, "Token and/or service were not provided", http.StatusBadRequest)
}
resultText, err := json.Marshal(result)
if err != nil {
errorMessage := "Error while encoding json data"
log.Println(errorMessage, err)
http.Error(w, errorMessage, http.StatusInternalServerError)
} else {
/* Set result headers */
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
/* Show result */
fmt.Fprint(w, string(resultText))
}
}