-
Notifications
You must be signed in to change notification settings - Fork 3
/
token.go
80 lines (68 loc) · 1.64 KB
/
token.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
package main
import (
"fmt"
"net/http"
"github.com/pkg/errors"
)
func getJS() []byte {
return []byte(`
<script>
var hash = location.hash;
if (hash.startsWith("#")) {
window.location = "http://localhost:49999/?"+hash.slice(1);
}
</script>
`)
}
func getClosingPage() []byte {
return []byte(`<!DOCTYPE html>
<html style="background: #E5E0DC;">
<head>
<title>Processing response</title>
<style type="text/css">
body {
font-family: "Arial", "sans-serif";
background: #00404D;
color: #fff;
padding: 4em;
margin: 4em;
border: 1px solid #aaa;
}
</style>
</head>
<body>
<h1>Kubed has successfully processed response.</h1>
<p>Please close this window and return to the command line.</p>
</body>
</html>`)
}
func getToken(port int) (string, error) {
done := make(chan string)
// This server waits for the redirect coming back from API server, populates
// reqErr and returns the token from that request, and then stops itself.
srv := &http.Server{
Addr: fmt.Sprintf("localhost:%d", port),
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// This is to handle fragment parsing in implicit code flow
if r.RequestURI == "/" {
w.Write(getJS())
return
}
if r.Method != "GET" {
reqErr = errors.New("The server made a bad request: Only GET is allowed")
}
token := r.URL.Query().Get("access_token")
if token != "" {
w.Write(getClosingPage())
done <- token
}
}),
}
go srv.ListenAndServe()
token := <-done
err := srv.Close()
if err != nil {
return token, errors.Wrap(err, "Error shutting down server")
}
return token, nil
}