forked from go-pkgz/auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev_provider.go
318 lines (279 loc) · 7.76 KB
/
dev_provider.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package provider
import (
"context"
"fmt"
"html/template"
"net/http"
"strings"
"sync"
"time"
"golang.org/x/oauth2"
"github.com/go-pkgz/auth/avatar"
"github.com/go-pkgz/auth/logger"
"github.com/go-pkgz/auth/token"
)
const (
defDevAuthPort = 8084
defDevAuthHost = "127.0.0.1"
)
// DevAuthServer is a fake oauth server for development
// it provides stand-alone server running on its own port and pretending to be the real oauth2. It also provides
// Dev Provider the same way as normal providers do, i.e. like github, google and others.
// can run in interactive and non-interactive mode. In interactive mode login attempts will show login form to select
// desired user name, this is the mode used for development. Non-interactive mode for tests only.
type DevAuthServer struct {
logger.L
Provider Oauth2Handler
Automatic bool
GetEmailFn func(string) string
username string // unsafe, but fine for dev
httpServer *http.Server
lock sync.Mutex
}
// Run oauth2 dev server on port devAuthPort
func (d *DevAuthServer) Run(ctx context.Context) { // nolint (gocyclo)
if d.Provider.Port == 0 {
d.Provider.Port = defDevAuthPort
}
if d.Provider.Host == "" {
d.Provider.Host = defDevAuthHost
}
d.username = "dev_user"
d.Logf("[INFO] run local oauth2 dev server on %d, redirect url=%s", d.Provider.Port, d.Provider.conf.RedirectURL)
d.lock.Lock()
var err error
userFormTmpl, err := template.New("page").Parse(devUserFormTmpl)
if err != nil {
d.Logf("[WARN] can't parse user form template, %s", err)
return
}
d.httpServer = &http.Server{
Addr: fmt.Sprintf(":%d", d.Provider.Port),
ReadHeaderTimeout: 5 * time.Second,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
d.Logf("[DEBUG] dev oauth request %s %s %+v", r.Method, r.URL, r.Header)
switch {
case strings.HasPrefix(r.URL.Path, "/login/oauth/authorize"):
// first time it will be called without username and will ask for one
if !d.Automatic && (r.ParseForm() != nil || r.Form.Get("username") == "") {
formData := struct{ Query template.URL }{Query: template.URL(r.URL.RawQuery)} //nolint:gosec // query is safe
if err = userFormTmpl.Execute(w, formData); err != nil {
d.Logf("[WARN] can't write, %s", err)
}
return
}
if !d.Automatic {
d.username = r.Form.Get("username")
}
state := r.URL.Query().Get("state")
callbackURL := fmt.Sprintf("%s?code=g0ZGZmNjVmOWI&state=%s", d.Provider.conf.RedirectURL, state)
d.Logf("[DEBUG] callback url=%s", callbackURL)
w.Header().Add("Location", callbackURL)
w.WriteHeader(http.StatusFound)
case strings.HasPrefix(r.URL.Path, "/login/oauth/access_token"):
res := `{
"access_token":"MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI3",
"token_type":"bearer",
"expires_in":3600,
"refresh_token":"IwOGYzYTlmM2YxOTQ5MGE3YmNmMDFkNTVk",
"scope":"create",
"state":"12345678"
}`
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if _, err = w.Write([]byte(res)); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
case strings.HasPrefix(r.URL.Path, "/user"):
ava := fmt.Sprintf("http://%s:%d/avatar?user=%s", d.Provider.Host, d.Provider.Port, d.username)
res := fmt.Sprintf(`{
"id": "%s",
"name":"%s",
"picture":"%s"
}`, d.username, d.username, ava)
if d.GetEmailFn != nil {
email := d.GetEmailFn(d.username)
res = fmt.Sprintf(`{
"id": "%s",
"name":"%s",
"picture":"%s",
"email": "%s"
}`, d.username, d.username, ava, email)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if _, err = w.Write([]byte(res)); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
case strings.HasPrefix(r.URL.Path, "/avatar"):
user := r.URL.Query().Get("user")
b, e := avatar.GenerateAvatar(user)
if e != nil {
w.WriteHeader(http.StatusNotFound)
return
}
if _, err = w.Write(b); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
default:
w.WriteHeader(http.StatusBadRequest)
}
}),
}
d.lock.Unlock()
go func() {
<-ctx.Done()
d.Logf("[DEBUG] cancellation via context, %v", ctx.Err())
d.Shutdown()
}()
err = d.httpServer.ListenAndServe()
d.Logf("[WARN] dev oauth2 server terminated, %s", err)
}
// Shutdown oauth2 dev server
func (d *DevAuthServer) Shutdown() {
d.Logf("[WARN] shutdown oauth2 dev server")
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
d.lock.Lock()
if d.httpServer != nil {
if err := d.httpServer.Shutdown(ctx); err != nil {
d.Logf("[DEBUG] oauth2 dev shutdown error, %s", err)
}
}
d.Logf("[DEBUG] shutdown dev oauth2 server completed")
d.lock.Unlock()
}
// NewDev makes dev oauth2 provider for admin user
func NewDev(p Params) Oauth2Handler {
if p.Port == 0 {
p.Port = defDevAuthPort
}
if p.Host == "" {
p.Host = defDevAuthHost
}
oh := initOauth2Handler(p, Oauth2Handler{
name: "dev",
endpoint: oauth2.Endpoint{
AuthURL: fmt.Sprintf("http://%s:%d/login/oauth/authorize", p.Host, p.Port),
TokenURL: fmt.Sprintf("http://%s:%d/login/oauth/access_token", p.Host, p.Port),
},
scopes: []string{"user:email"},
infoURL: fmt.Sprintf("http://%s:%d/user", p.Host, p.Port),
mapUser: func(data UserData, _ []byte) token.User {
userInfo := token.User{
ID: data.Value("id"),
Name: data.Value("name"),
Picture: data.Value("picture"),
Email: data.Value("email"),
}
return userInfo
},
})
oh.conf.RedirectURL = p.URL + "/auth/dev/callback"
return oh
}
var devUserFormTmpl = `
<html>
<head>
<title>Dev OAuth</title>
<style>
body {
text-align: center;
}
a {
color: hsl(200, 50%, 50%);
text-decoration-color: hsla(200, 50%, 50%, 0.5);
}
a:hover {
color: hsl(200, 50%, 70%);
text-decoration-color: hsla(200, 50%, 70%, 0.5);
}
form {
font-family: Helvetica, Arial, sans-serif;
margin: 100px auto;
display: inline-block;
padding: 1em;
box-shadow: 0 0 0.1rem rgba(0, 0, 0, 0.2), 0 0 0.4rem rgba(0, 0, 0, 0.1);
}
.form-header {
text-align: center;
}
.form-header h1 {
margin: 0;
}
.form-header h1 a:not(:hover) {
text-decoration: none;
}
.form-header p {
opacity: 0.6;
margin-top: 0;
margin-bottom: 2rem;
}
.username-label {
opacity: 0.6;
font-size: 0.8em;
}
.username-input {
font-size: inherit;
margin: 0;
width: 100%;
text-align: inherit;
}
.form-submit {
border: none;
background: hsl(200, 50%, 50%);
color: white;
font: inherit;
padding: 0.4em 0.8em 0.3em 0.8em;
border-radius: 0.2em;
width: 100%;
}
.form-submit:hover,
.form-submit:focus {
background-color: hsl(200, 50%, 70%);
}
.form-submit:active {
background-color: hsl(200, 80%, 70%);
}
.username-label,
.username-input,
.form-submit {
display: block;
margin-bottom: 0.4rem;
}
.notice {
margin: 0;
margin-top: 2rem;
font-size: 0.8em;
opacity: 0.6;
}
</style>
</head>
<body>
<form action="/login/oauth/authorize?{{.Query}}" method="post">
<header class="form-header">
<h1><a href="https://github.com/go-pkgz/auth">GO-PKGZ/AUTH</a></h1>
<p>Dev Provider</p>
</header>
<label>
<span class="username-label">Username</span>
<input
class="username-input"
type="text"
name="username"
value="dev_user"
autofocus
/>
</label>
<input type="submit" class="form-submit" value="Authorize" />
<p class="notice">Not for production use</p>
</form>
</body>
<script>
var input = document.querySelector(".username-input");
input.focus();
input.setSelectionRange(0, input.value.length)
</script>
</html>
`