-
Notifications
You must be signed in to change notification settings - Fork 11
/
interceptors.go
116 lines (101 loc) · 3.44 KB
/
interceptors.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
/*This file is part of kuberpult.
Kuberpult is free software: you can redistribute it and/or modify
it under the terms of the Expat(MIT) License as published by
the Free Software Foundation.
Kuberpult is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MIT License for more details.
You should have received a copy of the MIT License
along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
Copyright 2023 freiheit.com*/
package interceptors
import (
"context"
"net/http"
"github.com/MicahParks/keyfunc/v2"
"github.com/freiheit-com/kuberpult/pkg/auth"
"github.com/freiheit-com/kuberpult/services/frontend-service/pkg/handler"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
// authorize returns an error when the authentication failed
// Note that it may return (nil,nil) if the authentication was ok, but had no userdata.
// Never returns the default user
func authorize(ctx context.Context, jwks *keyfunc.JWKS, clientId string, tenantId string) (*auth.User, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.Errorf(codes.InvalidArgument, "Retrieving metadata failed")
}
authHeader, ok := md["authorization"]
if !ok {
return nil, status.Errorf(codes.Unauthenticated, "Authorization token not supplied")
}
token := authHeader[0]
claims, err := auth.ValidateToken(token, jwks, clientId, tenantId)
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, "Invalid authorization token provided")
}
// here, everything is valid, but we way still have empty strings, so we use the defaultUser here
var u *auth.User = nil
if _, ok := claims["aud"]; ok && claims["aud"] == clientId {
u = &auth.User{
Email: claims["email"].(string),
Name: claims["name"].(string),
}
}
return u, nil
}
func UnaryAuthInterceptor(ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
jwks *keyfunc.JWKS,
clientId string,
tenantId string) (interface{}, error) {
if info.FullMethod != "/api.v1.FrontendConfigService/GetConfig" {
_, err := authorize(ctx, jwks, clientId, tenantId)
if err != nil {
return nil, err
}
}
h, err := handler(ctx, req)
return h, err
}
func StreamAuthInterceptor(
srv interface{},
stream grpc.ServerStream,
info *grpc.StreamServerInfo,
handler grpc.StreamHandler,
jwks *keyfunc.JWKS,
clientId string,
tenantId string,
) error {
_, err := authorize(stream.Context(), jwks, clientId, tenantId)
if err != nil {
return err
}
return handler(srv, stream)
}
// DexLoginInterceptor intercepts HTTP calls to the frontend service.
// DexLoginInterceptor must only be used if dex is enabled.
// If the user us not logged in, it redirected the calls to the Dex login page.
// If the user is already logged in, proceeds with the request.
func DexLoginInterceptor(
w http.ResponseWriter,
req *http.Request,
httpHandler handler.Server,
clientID, baseURL string,
) {
role, err := auth.VerifyToken(req.Context(), req, clientID, baseURL)
if err != nil {
// If user is not authenticated redirect to the login page.
http.Redirect(w, req, auth.LoginPATH, http.StatusFound)
}
auth.WriteUserRoleToHttpHeader(req, role)
httpCtx := auth.WriteUserRoleToGrpcContext(req.Context(), role)
req = req.WithContext(httpCtx)
httpHandler.Handle(w, req)
}