diff --git a/main.go b/main.go index d4f35b5..5940fe3 100644 --- a/main.go +++ b/main.go @@ -554,7 +554,7 @@ func runRPC(wg *sync.WaitGroup, ctx context.Context) { var opts []_grpc.ServerOption if !dev { _log.Infow("adding auth interceptor") - ac := authv3.NewAuthContext() + ac := authv3.NewAuthContext(kc, ks, as) o := authv3.Option{} opts = append(opts, _grpc.UnaryInterceptor( ac.NewAuthUnaryInterceptor(o), diff --git a/pkg/auth/v3/auth.go b/pkg/auth/v3/auth.go index 884b4b5..01bddea 100644 --- a/pkg/auth/v3/auth.go +++ b/pkg/auth/v3/auth.go @@ -37,8 +37,10 @@ type authContext struct { as service.AuthzService } -// NewAuthContext setup authentication and authorization dependencies. -func NewAuthContext() authContext { +// SetupAuthContext sets up new authContext along with its +// dependencies. If the caller already has instances of authContext +// fields created then use NewAuthContext instead. +func SetupAuthContext() authContext { var ( kc *kclient.APIClient kratosScheme string @@ -93,3 +95,19 @@ func getEnvWithDefault(env, def string) string { } return val } + +// NewAuthContext instantiate authContext. NewAuthContext creates +// authContext reusing dependency instances from calling function +// instead of creating new instances. To create authContext along with +// its dependencies, use SetupAuthContext. +func NewAuthContext( + kc *kclient.APIClient, + apiKeySvc service.ApiKeyService, + authzSvc service.AuthzService, +) authContext { + return authContext{ + kc: kc, + ks: apiKeySvc, + as: authzSvc, + } +} diff --git a/pkg/auth/v3/middleware.go b/pkg/auth/v3/middleware.go index 0b1db40..c49d48c 100644 --- a/pkg/auth/v3/middleware.go +++ b/pkg/auth/v3/middleware.go @@ -20,7 +20,7 @@ type authMiddleware struct { func NewAuthMiddleware(opt Option) negroni.Handler { return &authMiddleware{ - ac: NewAuthContext(), + ac: SetupAuthContext(), opt: opt, } }