-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
main.go
335 lines (290 loc) · 11.4 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Package main starts the example server
package main
import (
"context"
"crypto/sha1"
"fmt"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-oauth2/oauth2/v4/errors"
"github.com/go-oauth2/oauth2/v4/generates"
"github.com/go-oauth2/oauth2/v4/manage"
"github.com/go-oauth2/oauth2/v4/models"
goauth2 "github.com/go-oauth2/oauth2/v4/server"
"github.com/go-oauth2/oauth2/v4/store"
log "github.com/go-pkgz/lgr"
"github.com/go-pkgz/rest"
"github.com/go-pkgz/rest/logger"
oldjwt "github.com/golang-jwt/jwt"
"golang.org/x/oauth2"
"github.com/go-pkgz/auth"
"github.com/go-pkgz/auth/avatar"
"github.com/go-pkgz/auth/middleware"
"github.com/go-pkgz/auth/provider"
"github.com/go-pkgz/auth/token"
)
func main() {
log.Setup(log.Debug, log.Msec, log.LevelBraces, log.CallerFile, log.CallerFunc) // setup default logger with go-pkgz/lgr
// define auth options
options := auth.Opts{
SecretReader: token.SecretFunc(func(_ string) (string, error) { // secret key for JWT, ignores aud
return "secret", nil
}),
TokenDuration: time.Minute, // short token, refreshed automatically
CookieDuration: time.Hour * 24, // cookie fine to keep for long time
DisableXSRF: true, // don't disable XSRF in real-life applications!
Issuer: "my-demo-service", // part of token, just informational
URL: "http://localhost:8080", // base url of the protected service
AvatarStore: avatar.NewLocalFS("/tmp/demo-auth-service"), // stores avatars locally
AvatarResizeLimit: 200, // resizes avatars to 200x200
ClaimsUpd: token.ClaimsUpdFunc(func(claims token.Claims) token.Claims { // modify issued token
if claims.User != nil && claims.User.Name == "dev_admin" { // set attributes for dev_admin
claims.User.SetAdmin(true)
claims.User.SetStrAttr("custom-key", "some value")
}
return claims
}),
Validator: token.ValidatorFunc(func(_ string, claims token.Claims) bool { // rejects some tokens
if claims.User != nil {
if strings.HasPrefix(claims.User.ID, "github_") { // allow all users with github auth
return true
}
if strings.HasPrefix(claims.User.ID, "microsoft_") { // allow all users with ms auth
return true
}
if strings.HasPrefix(claims.User.ID, "patreon_") { // allow all users with patreon auth
return true
}
if strings.HasPrefix(claims.User.ID, "discord_") { // allow all users with discord auth
return true
}
if strings.HasPrefix(claims.User.Name, "dev_") { // non-guthub allow only dev_* names
return true
}
return strings.HasPrefix(claims.User.Name, "custom123_")
}
return false
}),
Logger: log.Default(), // optional logger for auth library
UseGravatar: true, // for verified provider use gravatar service
}
// create auth service
service := auth.NewService(options)
service.AddProvider("dev", "", "") // add dev provider
service.AddProvider("github", os.Getenv("AEXMPL_GITHUB_CID"), os.Getenv("AEXMPL_GITHUB_CSEC")) // add github provider
service.AddProvider("twitter", os.Getenv("AEXMPL_TWITTER_APIKEY"), os.Getenv("AEXMPL_TWITTER_APISEC"))
service.AddProvider("microsoft", os.Getenv("AEXMPL_MS_APIKEY"), os.Getenv("AEXMPL_MS_APISEC"))
service.AddProvider("patreon", os.Getenv("AEXMPL_PATREON_CID"), os.Getenv("AEXMPL_PATREON_CSEC"))
service.AddProvider("discord", os.Getenv("AEXMPL_DISCORD_CID"), os.Getenv("AEXMPL_DISCORD_CSEC"))
// allow sign with apple id
appleCfg := provider.AppleConfig{
ClientID: os.Getenv("AEXMPL_APPLE_CID"),
TeamID: os.Getenv("AEXMPL_APPLE_TID"),
KeyID: os.Getenv("AEXMPL_APPLE_KEYID"), // private key identifier
ResponseMode: "query", // see https://developer.apple.com/documentation/sign_in_with_apple/request_an_authorization_to_the_sign_in_with_apple_server?changes=_1_2#4066168
}
if err := service.AddAppleProvider(appleCfg, provider.LoadApplePrivateKeyFromFile(os.Getenv("AEXMPL_APPLE_PRIVKEY_PATH"))); err != nil {
log.Printf("[ERROR] create AppleProvider failed: %v", err)
}
// allow anonymous user via custom (direct) provider
service.AddDirectProvider("anonymous", anonymousAuthProvider())
// add verified provider
service.AddVerifProvider("email",
"To confirm use {{.Token}}\nor follow http://localhost:8080/auth/email/login?token={{.Token}}",
provider.SenderFunc(func(address string, text string) error { // sender just prints token
fmt.Printf("CONFIRMATION for %s\n%s\n", address, text)
return nil
}),
)
if tkn := os.Getenv("TELEGRAM_TOKEN"); tkn != "" {
// add telegram provider
telegram := provider.TelegramHandler{
ProviderName: "telegram",
ErrorMsg: "❌ Invalid auth request. Please try clicking link again.",
SuccessMsg: "✅ You have successfully authenticated!",
Telegram: provider.NewTelegramAPI(tkn, http.DefaultClient),
L: log.Default(),
TokenService: service.TokenService(),
AvatarSaver: service.AvatarProxy(),
}
go func() {
err := telegram.Run(context.Background())
if err != nil {
log.Fatalf("[PANIC] failed to start telegram: %v", err)
}
}()
service.AddCustomHandler(&telegram)
}
// run dev/test oauth2 server on :8084
go func() {
devAuthServer, err := service.DevAuth() // peak dev oauth2 server
if err != nil {
log.Printf("[PANIC] failed to start dev oauth2 server, %v", err)
}
devAuthServer.Run(context.Background())
}()
// Example: start custom oauth2 server, add to handlers
srv := initGoauth2Srv()
sopts := provider.CustomServerOpt{
URL: "http://127.0.0.1:9096",
L: options.Logger,
WithLoginPage: true,
}
// create custom provider and prepare params for handler
prov := provider.NewCustomServer(srv, sopts)
// Start server
go prov.Run(context.Background())
service.AddCustomProvider("custom123", auth.Client{Cid: "cid", Csecret: "csecret"}, prov.HandlerOpt)
// Example: add different oauth2 provider
c := auth.Client{
Cid: os.Getenv("AEXMPL_BITBUCKET_CID"),
Csecret: os.Getenv("AEXMPL_BITBUCKET_CSEC"),
}
service.AddCustomProvider("bitbucket", c, provider.CustomHandlerOpt{
Endpoint: oauth2.Endpoint{
AuthURL: "https://bitbucket.org/site/oauth2/authorize",
TokenURL: "https://bitbucket.org/site/oauth2/access_token",
},
InfoURL: "https://api.bitbucket.org/2.0/user/",
MapUserFn: func(data provider.UserData, _ []byte) token.User {
userInfo := token.User{
ID: "bitbucket_" + token.HashID(sha1.New(),
data.Value("username")),
Name: data.Value("nickname"),
}
return userInfo
},
Scopes: []string{"account"},
})
// retrieve auth middleware
m := service.Middleware()
// setup http server
router := chi.NewRouter()
// add some external middlewares from go-pkgz/rest
router.Use(rest.AppInfo("auth-example", "umputun", "1.0.0"), rest.Ping)
router.Use(logger.New(logger.Log(log.Default()), logger.WithBody, logger.Prefix("[INFO]")).Handler) // log all http requests
router.Get("/open", openRouteHandler) // open page
router.Group(func(r chi.Router) {
r.Use(m.Auth)
r.Use(m.UpdateUser(middleware.UserUpdFunc(func(user token.User) token.User {
user.SetStrAttr("some_attribute", "attribute value")
return user
})))
r.Get("/private_data", protectedDataHandler) // protected api
})
// static files under ~/web
workDir, _ := os.Getwd()
filesDir := filepath.Join(workDir, "frontend")
fileServer(router, "/web", http.Dir(filesDir))
// setup auth routes
authRoutes, avaRoutes := service.Handlers()
router.Mount("/auth", authRoutes) // add auth handlers
router.Mount("/avatar", avaRoutes) // add avatar handler
httpServer := &http.Server{
Addr: ":8080",
ReadHeaderTimeout: 5 * time.Second,
Handler: router,
}
if err := httpServer.ListenAndServe(); err != nil {
log.Printf("[PANIC] failed to start http server, %v", err)
}
}
// anonymousAuthProvider allows auth-free login with any valid user name
func anonymousAuthProvider() provider.CredCheckerFunc {
log.Printf("[WARN] anonymous access enabled")
var isValidAnonName = regexp.MustCompile(`^[a-zA-Z][\w ]+$`).MatchString
return func(user, _ string) (ok bool, err error) {
user = strings.TrimSpace(user)
if len(user) < 3 {
log.Printf("[WARN] name %q is too short, should be at least 3 characters", user)
return false, nil
}
if !isValidAnonName(user) {
log.Printf("[WARN] name %q should have letters, digits, underscores and spaces only", user)
return false, nil
}
return true, nil
}
}
// FileServer conveniently sets up a http.FileServer handler to serve static files from a http.FileSystem.
// Borrowed from https://github.com/go-chi/chi/blob/master/_examples/fileserver/main.go
func fileServer(r chi.Router, path string, root http.FileSystem) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit URL parameters.")
}
log.Printf("[INFO] serving static files from %v", root)
fs := http.StripPrefix(path, http.FileServer(root))
if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", http.StatusMovedPermanently).ServeHTTP)
path += "/"
}
path += "*"
r.Get(path, func(w http.ResponseWriter, r *http.Request) {
fs.ServeHTTP(w, r)
})
}
// GET /open returns a page available without authorization
func openRouteHandler(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("this is an open route, no token needed\n"))
}
// GET /private_data returns json with user info and ts
func protectedDataHandler(w http.ResponseWriter, r *http.Request) {
userInfo, err := token.GetUserInfo(r)
if err != nil {
log.Printf("failed to get user info, %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
res := struct {
TS time.Time `json:"ts"`
Field1 string `json:"fld1"`
Field2 int `json:"fld2"`
User token.User `json:"userInfo"`
}{
TS: time.Now(),
Field1: "some private thing",
Field2: 42,
User: userInfo,
}
rest.RenderJSON(w, res)
}
// initialize go-oauth2/oauth2 server
func initGoauth2Srv() *goauth2.Server {
manager := manage.NewDefaultManager()
manager.SetAuthorizeCodeTokenCfg(manage.DefaultAuthorizeCodeTokenCfg)
// token store
manager.MustTokenStorage(store.NewMemoryTokenStore())
// generate jwt access token
manager.MapAccessGenerate(generates.NewJWTAccessGenerate("custom", []byte("00000000"), oldjwt.SigningMethodHS512))
// client memory store
clientStore := store.NewClientStore()
err := clientStore.Set("cid", &models.Client{
ID: "cid",
Secret: "csecret",
Domain: "http://localhost:8080",
})
if err != nil {
log.Printf("failed to set up a client store for go-oauth2/oauth2 server, %s", err)
}
manager.MapClientStorage(clientStore)
srv := goauth2.NewServer(goauth2.NewConfig(), manager)
srv.SetUserAuthorizationHandler(func(_ http.ResponseWriter, r *http.Request) (string, error) {
if r.Form.Get("username") != "admin" || r.Form.Get("password") != "admin" {
return "", fmt.Errorf("wrong creds. Use: admin admin")
}
return "custom123_admin", nil
})
srv.SetInternalErrorHandler(func(err error) (re *errors.Response) {
log.Printf("Internal Error: %s", err.Error())
return
})
srv.SetResponseErrorHandler(func(re *errors.Response) {
log.Printf("Response Error: %s", re.Error.Error())
})
return srv
}