/
handler.go
104 lines (87 loc) · 2.93 KB
/
handler.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
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
package handler
import (
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/Bisnode/kubectl-login/util"
"github.com/golang-jwt/jwt"
"k8s.io/client-go/tools/clientcmd/api"
)
// IDTokenWebhookHandler carries configuration and any other state (like the nonce) between the main initialization and
// the subsequent fetching of the ID token passed to the server after authenticating.
type IDTokenWebhookHandler struct {
ClientCfg *api.Config
ForceLogin bool
ExecCredentialMode bool
Nonce string
QuitChan chan struct{}
}
// StdClaimsWithNonce - since all verification is done server side by the kubernetes API, all we are really interested
// in here is that:
// 1) the token is not expired or else we shouldn't store it and
// 2) that the nonce in the ID token is the same as that provided in the authorization request
type StdClaimsWithNonce struct {
Nonce string `json:"nonce"`
jwt.StandardClaims
}
func badRequest(w http.ResponseWriter, message string) {
log.Println(message)
w.WriteHeader(http.StatusBadRequest)
log.Print(fmt.Fprintf(w, message)) //nolint
}
// Extract ID token from form POST parameter, store it in kubeconf, send 200 OK response and then exit
func (h *IDTokenWebhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
log.Print(fmt.Fprintf(w, ""))
return
}
if r.URL.Path != "/redirect" {
log.Println("POST request received to other endpoint than /redirect. Skipping.")
return
}
if r.Header.Get("Content-Length") == "" || r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
badRequest(w, "Content-Length or Content-Type not provided or invalid")
return
}
err := r.ParseForm()
if err != nil {
log.Println("Unable to parse form body")
return
}
idToken := r.PostForm.Get("id_token")
if idToken == "" {
badRequest(w, "No id_token provided in request. Aborting.")
return
}
parser := &jwt.Parser{}
claims := &StdClaimsWithNonce{}
token, _, err := parser.ParseUnverified(idToken, claims)
if err != nil {
log.Printf("Failed decoding claims %s", err)
return
}
if claims.Nonce != h.Nonce {
log.Fatal("Nonce in ID token not identical to that in authorization request. Aborting.")
}
exp := time.Unix(claims.ExpiresAt, 0)
// Print the results if run as an exec credential plugin
if h.ExecCredentialMode {
fmt.Println(fmt.Sprintf(util.ExecCredentialObject, token.Raw, exp.Format(time.RFC3339)))
}
err = util.WriteToken(token.Raw, h.ClientCfg.CurrentContext)
if err != nil {
log.Println(err)
}
if !h.ExecCredentialMode {
_, _ = fmt.Fprintf(os.Stdout,
"Authenticated for context %v. Token valid until %v.\n", h.ClientCfg.CurrentContext, exp)
}
// Return control to shell at this point
h.QuitChan <- struct{}{}
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(200)
_, _ = w.Write([]byte("Authentication complete. You may close this browser tab."))
}