@@ -40,40 +40,47 @@ type GateToken struct {
40
40
Token string
41
41
}
42
42
43
- // SetToken handle callbacs from users manually setting the JWT cookie.
44
- func SetToken (w http.ResponseWriter , r * http.Request ) {
45
- // Log request
46
- glog .Infof ("%s %v: %+v" , r .RemoteAddr , r .Method , r .URL )
47
-
48
- var token string
49
- var then string
43
+ // HandlerFunc is a type alias for a function that handles HTTP requests.
44
+ type HandlerFunc func (w http.ResponseWriter , r * http.Request )
45
+
46
+ // Factory function that returns a SetTokenHandler
47
+ func SetTokenFactory (clientPath string , apiPath string ) HandlerFunc {
48
+ return func (w http.ResponseWriter , r * http.Request ) {
49
+ // Log request
50
+ glog .Infof ("%s %v: %+v" , r .RemoteAddr , r .Method , r .URL )
51
+
52
+ var token string
53
+ var name string
54
+ var namespace string
55
+
56
+ // Get token and redirect from get request
57
+ if r .Method == http .MethodGet {
58
+ query := r .URL .Query ()
59
+ token = query .Get ("token" )
60
+ name = query .Get ("name" )
61
+ namespace = query .Get ("namespace" )
62
+ }
50
63
51
- // Get token and redirect from get request
52
- if r .Method == http .MethodGet {
53
- query : = r .URL . Query ( )
54
- token = query . Get ( "token " )
55
- then = query . Get ( "then " )
56
- }
64
+ // Get token and redirect from post request
65
+ if r .Method == http .MethodPost {
66
+ token = r .FormValue ( "token" )
67
+ name = r . FormValue ( "name " )
68
+ namespace = r . FormValue ( "namespace " )
69
+ }
57
70
58
- // Get token and redirect from post request
59
- if r .Method == http .MethodPost {
60
- token = r .FormValue ("token" )
61
- then = r .FormValue ("then" )
62
- }
71
+ // Build the redirect URL
72
+ cleanApiPath := apiPath [1 :] // Remove prefix `/` from the begining of apiPath
73
+ then := fmt .Sprintf ("%s?path=%sapis/subresources.kubevirt.io/v1/namespaces/%s/virtualmachineinstances/%s/vnc" , clientPath , cleanApiPath , namespace , name )
63
74
64
- // Empty redirect, means go home
65
- if then == "" {
66
- then = "/"
75
+ // Set session cookie.
76
+ http .SetCookie (w , & http.Cookie {
77
+ Name : CookieName ,
78
+ Value : token ,
79
+ Path : "/" ,
80
+ SameSite : http .SameSiteLaxMode ,
81
+ HttpOnly : true })
82
+ http .Redirect (w , r , then , http .StatusFound )
67
83
}
68
-
69
- // Set session cookie.
70
- http .SetCookie (w , & http.Cookie {
71
- Name : CookieName ,
72
- Value : token ,
73
- Path : "/" ,
74
- SameSite : http .SameSiteLaxMode ,
75
- HttpOnly : true })
76
- http .Redirect (w , r , then , http .StatusFound )
77
84
}
78
85
79
86
// GetToken handle callbacs from OAuth2 authtorization server.
0 commit comments