Add two constructors for authContext

This includes:
- SetupAuthContext:  Setups authContext with new authContext fields.
- NewAuthContext: Create AuthContext with using authContext fields
from caller function.
This commit is contained in:
Akshay Gaikwad
2022-03-30 13:00:35 +05:30
parent c972e26d93
commit 5de4ba9765
3 changed files with 22 additions and 4 deletions

View File

@@ -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),

View File

@@ -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,
}
}

View File

@@ -20,7 +20,7 @@ type authMiddleware struct {
func NewAuthMiddleware(opt Option) negroni.Handler {
return &authMiddleware{
ac: NewAuthContext(),
ac: SetupAuthContext(),
opt: opt,
}
}