forked from ubccr/mokey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
183 lines (161 loc) · 4.7 KB
/
main.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
// This is an example OAuth 2.0 client app using Open ID Connect. It is not
// meant for use in a production environment and only for testing
// FreeIPA->mokey->hydra integration. Configuration is via environment
// variables (see mokey-oidc.conf)
//
// This code was adopted from https://github.com/ory/hydra-consent-app-go
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"html/template"
"net/http"
"os"
oidc "github.com/coreos/go-oidc"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/urfave/negroni"
"golang.org/x/oauth2"
)
// A state for performing the OAuth 2.0 flow
var state = "mokeydemostate"
var (
cert string
key string
authUrl *oauth2.Config
homeTmpl *template.Template
callbackTmpl *template.Template
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/", handleHome)
r.HandleFunc("/callback", handleCallback)
n := negroni.New()
n.UseHandler(r)
http.ListenAndServeTLS(fmt.Sprintf(":%s", os.Getenv("MOKEY_OIDC_PORT")), cert, key, n)
fmt.Println(fmt.Sprintf("Listening on :%s", os.Getenv("MOKEY_OIDC_PORT")))
}
func handleHome(w http.ResponseWriter, _ *http.Request) {
var u = authUrl.AuthCodeURL(state) + "&nonce=" + state
err := homeTmpl.Execute(w, u)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// After the user gave his consent, they will hit this endpoint. The mokey
// consent app includes some extra user info in the id_token.
func handleCallback(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
})
provider, err := oidc.NewProvider(ctx, os.Getenv("MOKEY_OIDC_PROVIDER"))
if err != nil {
http.Error(w, errors.Wrap(err, "Could not create provider").Error(), http.StatusBadRequest)
return
}
oidcConfig := &oidc.Config{
ClientID: authUrl.ClientID,
}
verifier := provider.Verifier(oidcConfig)
token, err := authUrl.Exchange(ctx, r.URL.Query().Get("code"))
if err != nil {
http.Error(w, errors.Wrap(err, "Could not exhange token").Error(), http.StatusBadRequest)
return
}
rawIDToken, ok := token.Extra("id_token").(string)
if !ok {
http.Error(w, errors.Wrap(err, "No id_token field in oauth2 token").Error(), http.StatusBadRequest)
return
}
idToken, err := verifier.Verify(ctx, rawIDToken)
if err != nil {
http.Error(w, errors.Wrap(err, "Failed to verify id token").Error(), http.StatusBadRequest)
return
}
resp := struct {
OAuth2Token *oauth2.Token
IDTokenClaims *json.RawMessage
}{token, new(json.RawMessage)}
if err := idToken.Claims(&resp.IDTokenClaims); err != nil {
http.Error(w, errors.Wrap(err, "Failed to get claims").Error(), http.StatusBadRequest)
return
}
data, err := json.MarshalIndent(resp, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = callbackTmpl.Execute(w, struct {
*oauth2.Token
UserInfo string
IDToken interface{}
}{
Token: token,
UserInfo: string(data),
IDToken: token.Extra("id_token"),
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func init() {
authUrl = &oauth2.Config{
ClientID: os.Getenv("MOKEY_OIDC_CLIENT_ID"),
ClientSecret: os.Getenv("MOKEY_OIDC_CLIENT_SECRET"),
Endpoint: oauth2.Endpoint{
TokenURL: os.Getenv("MOKEY_OIDC_TOKEN_URL"),
AuthURL: os.Getenv("MOKEY_OIDC_AUTH_URL"),
},
Scopes: []string{"openid", "offline", "hydra.clients"},
RedirectURL: os.Getenv("MOKEY_OIDC_REDIRECT_URL"),
}
cert = os.Getenv("MOKEY_OIDC_CERT")
key = os.Getenv("MOKEY_OIDC_KEY")
homeTmpl = template.Must(template.New("home.html").Parse(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome!</title>
</head>
<body>
<p>
Click <a href="{{.}}">here</a> to perform the exemplary authorize code flow.
</p>
</body>
</html>`))
callbackTmpl = template.Must(template.New("callback.html").Parse(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Success!</title>
</head>
<body>
<p>
OAuth2 authorize code flow was performed successfully!
</p>
<dl>
<dt>AccessToken</dt>
<dd><code>{{.AccessToken}}</code></dd>
<dt>TokenType</dt>
<dd><code>{{.TokenType}}</code></dd>
<dt>RefreshToken</dt>
<dd><code>{{.RefreshToken}}</code></dd>
<dt>Expiry</dt>
<dd><code>{{.Expiry}}</code></dd>
<dt>ID Token</dt>
<dd><code>{{.IDToken}}</code></dd>
<dt>UserInfo</dt>
<dd><code>{{.UserInfo}}</code></dd>
</dl>
<p>
<a href="/">Do it again</a>
</p>
</body>
</html>`))
}