forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoints.go
128 lines (105 loc) · 3.82 KB
/
endpoints.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
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 tokenrequest
import (
"encoding/json"
"fmt"
"html/template"
"io"
"net/http"
"path"
"github.com/RangelReale/osincli"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/openshift/origin/pkg/auth/server/login"
)
const (
RequestTokenEndpoint = "/token/request"
DisplayTokenEndpoint = "/token/display"
)
type endpointDetails struct {
publicMasterURL string
originOAuthClient *osincli.Client
}
type Endpoints interface {
Install(mux login.Mux, paths ...string)
}
func NewEndpoints(publicMasterURL string, originOAuthClient *osincli.Client) Endpoints {
return &endpointDetails{publicMasterURL, originOAuthClient}
}
// Install registers the request token endpoints into a mux. It is expected that the
// provided prefix will serve all operations
func (endpoints *endpointDetails) Install(mux login.Mux, paths ...string) {
for _, prefix := range paths {
mux.HandleFunc(path.Join(prefix, RequestTokenEndpoint), endpoints.requestToken)
mux.HandleFunc(path.Join(prefix, DisplayTokenEndpoint), endpoints.displayToken)
}
}
// requestToken works for getting a token in your browser and seeing what your token is
func (endpoints *endpointDetails) requestToken(w http.ResponseWriter, req *http.Request) {
authReq := endpoints.originOAuthClient.NewAuthorizeRequest(osincli.CODE)
oauthURL := authReq.GetAuthorizeUrlWithParams("")
http.Redirect(w, req, oauthURL.String(), http.StatusFound)
}
func (endpoints *endpointDetails) displayToken(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/html")
data := tokenData{RequestURL: "request", PublicMasterURL: endpoints.publicMasterURL}
authorizeReq := endpoints.originOAuthClient.NewAuthorizeRequest(osincli.CODE)
authorizeData, err := authorizeReq.HandleRequest(req)
if err != nil {
data.Error = fmt.Sprintf("Error handling auth request: %v", err)
w.WriteHeader(http.StatusInternalServerError)
renderToken(w, data)
return
}
accessReq := endpoints.originOAuthClient.NewAccessRequest(osincli.AUTHORIZATION_CODE, authorizeData)
accessData, err := accessReq.GetToken()
if err != nil {
data.Error = fmt.Sprintf("Error getting token: %v", err)
w.WriteHeader(http.StatusInternalServerError)
renderToken(w, data)
return
}
jsonBytes, err := json.MarshalIndent(accessData.ResponseData, "", " ")
if err != nil {
data.Error = fmt.Sprintf("Error marshalling json: %v", err)
w.WriteHeader(http.StatusInternalServerError)
renderToken(w, data)
return
}
data.OAuthJSON = string(jsonBytes)
data.AccessToken = accessData.AccessToken
renderToken(w, data)
}
func renderToken(w io.Writer, data tokenData) {
if err := tokenTemplate.Execute(w, data); err != nil {
util.HandleError(fmt.Errorf("unable to render token template: %v", err))
}
}
type tokenData struct {
Error string
OAuthJSON string
AccessToken string
RequestURL string
PublicMasterURL string
}
// TODO: allow template to be read from an external file
var tokenTemplate = template.Must(template.New("tokenTemplate").Parse(`
<style>
body { font-family: sans-serif; font-size: 12pt; margin: 2em 5%; background-color: #F9F9F9; }
pre { padding-left: 1em; border-left: .25em solid #eee; }
a { color: #00f; text-decoration: none; }
a:hover { text-decoration: underline; }
</style>
{{ if .Error }}
{{ .Error }}
{{ else }}
<h3>Here is your brand new OAuth access token:</h3>
<pre>{{.OAuthJSON}}</pre>
<h3>How do I use this token?</h3>
<pre>oc login --token={{.AccessToken}} --server={{.PublicMasterURL}}</pre>
<pre>curl -H "Authorization: Bearer {{.AccessToken}}" …</pre>
<h3>How do I delete this token when I'm done?</h3>
<pre>oc delete oauthaccesstoken {{.AccessToken}}</pre>
<pre>curl -X DELETE …/oapi/v1/oauthaccesstokens/{{.AccessToken}}</pre>
{{ end }}
<br><br>
<a href="{{.RequestURL}}">Request another token</a>
`))