@@ -25,14 +25,13 @@ import (
2525 grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
2626 grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
2727 grpc_tags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
28+ "github.com/reeflective/team/example/transports/grpc/common"
29+ "github.com/reeflective/team/server"
2830 "github.com/sirupsen/logrus"
2931 "google.golang.org/grpc"
3032 "google.golang.org/grpc/codes"
3133 "google.golang.org/grpc/credentials"
3234 "google.golang.org/grpc/status"
33-
34- "github.com/reeflective/team/example/transports/grpc/common"
35- "github.com/reeflective/team/server"
3635)
3736
3837// BufferingOptions returns a list of server options with max send/receive
@@ -139,6 +138,7 @@ func (ts *Teamserver) initAuthMiddleware() ([]grpc.ServerOption, error) {
139138 requestOpts = append (requestOpts ,
140139 grpc_auth .UnaryServerInterceptor (serverAuthFunc ),
141140 )
141+
142142 streamOpts = append (streamOpts ,
143143 grpc_auth .StreamServerInterceptor (serverAuthFunc ),
144144 )
@@ -166,23 +166,27 @@ func serverAuthFunc(ctx context.Context) (context.Context, error) {
166166 return newCtx , nil
167167}
168168
169+ // tokenAuthFunc uses the core reeflective/team/server to authenticate user requests.
169170func (ts * Teamserver ) tokenAuthFunc (ctx context.Context ) (context.Context , error ) {
170171 log := ts .NamedLogger ("transport" , "grpc" )
171- log .Debugf ("Auth interceptor checking user token ..." )
172172
173173 rawToken , err := grpc_auth .AuthFromMD (ctx , "Bearer" )
174174 if err != nil {
175175 log .Errorf ("Authentication failure: %s" , err )
176176 return nil , status .Error (codes .Unauthenticated , "Authentication failure" )
177177 }
178178
179+ // Let our core teamserver driver authenticate the user.
180+ // The teamserver has its credentials, tokens and everything in database.
179181 user , authorized , err := ts .UserAuthenticate (rawToken )
180- if err != nil || ! authorized || user == "" {
182+ if err != nil || ! authorized || user . Name == "" {
181183 log .Errorf ("Authentication failure: %s" , err )
182184 return nil , status .Error (codes .Unauthenticated , "Authentication failure" )
183185 }
184186
185- newCtx := context .WithValue (ctx , Transport , "mtls" )
187+ // Fetch the user in database for permissions.
188+
189+ newCtx := context .WithValue (ctx , Transport , user )
186190 newCtx = context .WithValue (newCtx , User , user )
187191
188192 return newCtx , nil
0 commit comments