-
Notifications
You must be signed in to change notification settings - Fork 796
/
fake_auth.go
78 lines (66 loc) · 2.39 KB
/
fake_auth.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
// Package fakeauth provides middlewares that injects a fake userID, so the rest of the code
// can continue to be multitenant.
package fakeauth
import (
"context"
"net/http"
"github.com/weaveworks/common/middleware"
"github.com/weaveworks/common/server"
"github.com/weaveworks/common/user"
"google.golang.org/grpc"
)
// SetupAuthMiddleware for the given server config.
func SetupAuthMiddleware(config *server.Config, enabled bool, noGRPCAuthOn []string) middleware.Interface {
if enabled {
ignoredMethods := map[string]bool{}
for _, m := range noGRPCAuthOn {
ignoredMethods[m] = true
}
config.GRPCMiddleware = append(config.GRPCMiddleware, func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
if ignoredMethods[info.FullMethod] {
return handler(ctx, req)
}
return middleware.ServerUserHeaderInterceptor(ctx, req, info, handler)
})
config.GRPCStreamMiddleware = append(config.GRPCStreamMiddleware,
func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
if ignoredMethods[info.FullMethod] {
return handler(srv, ss)
}
return middleware.StreamServerUserHeaderInterceptor(srv, ss, info, handler)
},
)
return middleware.AuthenticateUser
}
config.GRPCMiddleware = append(config.GRPCMiddleware,
fakeGRPCAuthUniaryMiddleware,
)
config.GRPCStreamMiddleware = append(config.GRPCStreamMiddleware,
fakeGRPCAuthStreamMiddleware,
)
return fakeHTTPAuthMiddleware
}
var fakeHTTPAuthMiddleware = middleware.Func(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := user.InjectOrgID(r.Context(), "fake")
next.ServeHTTP(w, r.WithContext(ctx))
})
})
var fakeGRPCAuthUniaryMiddleware = func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
ctx = user.InjectOrgID(ctx, "fake")
return handler(ctx, req)
}
var fakeGRPCAuthStreamMiddleware = func(srv interface{}, ss grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
ctx := user.InjectOrgID(ss.Context(), "fake")
return handler(srv, serverStream{
ctx: ctx,
ServerStream: ss,
})
}
type serverStream struct {
ctx context.Context
grpc.ServerStream
}
func (ss serverStream) Context() context.Context {
return ss.ctx
}