diff --git a/internal/context/context.go b/internal/context/context.go index 5899b1e..757e07e 100644 --- a/internal/context/context.go +++ b/internal/context/context.go @@ -44,7 +44,7 @@ func Init() { } type NFContext interface { - AuthorizationCheck(token, serviceName string) error + AuthorizationCheck(token string, serviceName models.ServiceName) error } var _ NFContext = &NSSFContext{} @@ -136,22 +136,22 @@ func GetSelf() *NSSFContext { return &nssfContext } -func (c *NSSFContext) GetTokenCtx(scope string, targetNF models.NfType) ( +func (c *NSSFContext) GetTokenCtx(serviceName models.ServiceName, targetNF models.NfType) ( context.Context, *models.ProblemDetails, error, ) { if !c.OAuth2Required { return context.TODO(), nil, nil } return oauth.GetTokenCtx(models.NfType_NSSF, targetNF, - c.NfId, c.NrfUri, scope) + c.NfId, c.NrfUri, string(serviceName)) } -func (c *NSSFContext) AuthorizationCheck(token, serviceName string) error { +func (c *NSSFContext) AuthorizationCheck(token string, serviceName models.ServiceName) error { if !c.OAuth2Required { logger.UtilLog.Debugf("NSSFContext::AuthorizationCheck: OAuth2 not required\n") return nil } logger.UtilLog.Debugf("NSSFContext::AuthorizationCheck: token[%s] serviceName[%s]\n", token, serviceName) - return oauth.VerifyOAuth(token, serviceName, c.NrfCertPem) + return oauth.VerifyOAuth(token, string(serviceName), c.NrfCertPem) } diff --git a/internal/sbi/consumer/nf_management.go b/internal/sbi/consumer/nf_management.go index 1274e46..f6d4bdf 100644 --- a/internal/sbi/consumer/nf_management.go +++ b/internal/sbi/consumer/nf_management.go @@ -93,7 +93,7 @@ func SendDeregisterNFInstance() (*models.ProblemDetails, error) { var err error - ctx, pd, err := nssf_context.GetSelf().GetTokenCtx("nnrf-nfm", models.NfType_NRF) + ctx, pd, err := nssf_context.GetSelf().GetTokenCtx(models.ServiceName_NNRF_NFM, models.NfType_NRF) if err != nil { return pd, err } diff --git a/internal/sbi/nssaiavailability/routers.go b/internal/sbi/nssaiavailability/routers.go index ee285f7..4cccca1 100644 --- a/internal/sbi/nssaiavailability/routers.go +++ b/internal/sbi/nssaiavailability/routers.go @@ -23,8 +23,6 @@ import ( logger_util "github.com/free5gc/util/logger" ) -const serviceName string = string(models.ServiceName_NNSSF_NSSAIAVAILABILITY) - // Route is the information for every URI. type Route struct { // Name is the name of this Route. @@ -50,7 +48,7 @@ func NewRouter() *gin.Engine { func AddService(engine *gin.Engine) *gin.RouterGroup { group := engine.Group(factory.NssfNssaiavailResUriPrefix) - routerAuthorizationCheck := util.NewRouterAuthorizationCheck(serviceName) + routerAuthorizationCheck := util.NewRouterAuthorizationCheck(models.ServiceName_NNSSF_NSSAIAVAILABILITY) group.Use(func(c *gin.Context) { routerAuthorizationCheck.Check(c, nssf_context.GetSelf()) }) diff --git a/internal/sbi/nsselection/routers.go b/internal/sbi/nsselection/routers.go index c347505..f5f0b7c 100644 --- a/internal/sbi/nsselection/routers.go +++ b/internal/sbi/nsselection/routers.go @@ -23,8 +23,6 @@ import ( logger_util "github.com/free5gc/util/logger" ) -const serviceName string = string(models.ServiceName_NNSSF_NSSELECTION) - // Route is the information for every URI. type Route struct { // Name is the name of this Route. @@ -50,7 +48,7 @@ func NewRouter() *gin.Engine { func AddService(engine *gin.Engine) *gin.RouterGroup { group := engine.Group(factory.NssfNsselectResUriPrefix) - routerAuthorizationCheck := util.NewRouterAuthorizationCheck(serviceName) + routerAuthorizationCheck := util.NewRouterAuthorizationCheck(models.ServiceName_NNSSF_NSSELECTION) group.Use(func(c *gin.Context) { routerAuthorizationCheck.Check(c, nssf_context.GetSelf()) }) diff --git a/internal/util/router_auth_check.go b/internal/util/router_auth_check.go index 1943d64..ede9ad3 100644 --- a/internal/util/router_auth_check.go +++ b/internal/util/router_auth_check.go @@ -7,13 +7,14 @@ import ( nssf_context "github.com/free5gc/nssf/internal/context" "github.com/free5gc/nssf/internal/logger" + "github.com/free5gc/openapi/models" ) type RouterAuthorizationCheck struct { - serviceName string + serviceName models.ServiceName } -func NewRouterAuthorizationCheck(serviceName string) *RouterAuthorizationCheck { +func NewRouterAuthorizationCheck(serviceName models.ServiceName) *RouterAuthorizationCheck { return &RouterAuthorizationCheck{ serviceName: serviceName, } diff --git a/internal/util/router_auth_check_test.go b/internal/util/router_auth_check_test.go index bf4ecf0..0d6ec07 100644 --- a/internal/util/router_auth_check_test.go +++ b/internal/util/router_auth_check_test.go @@ -7,6 +7,8 @@ import ( "github.com/gin-gonic/gin" "github.com/pkg/errors" + + "github.com/free5gc/openapi/models" ) const ( @@ -20,7 +22,7 @@ func newMockNSSFContext() *mockNSSFContext { return &mockNSSFContext{} } -func (m *mockNSSFContext) AuthorizationCheck(token string, serviceName string) error { +func (m *mockNSSFContext) AuthorizationCheck(token string, serviceName models.ServiceName) error { if token == Valid { return nil } @@ -81,7 +83,9 @@ func TestRouterAuthorizationCheck_Check(t *testing.T) { } c.Request.Header.Set("Authorization", tt.args.token) - rac := NewRouterAuthorizationCheck("testService") + var testService models.ServiceName = "testService" + + rac := NewRouterAuthorizationCheck(testService) rac.Check(c, newMockNSSFContext()) if w.Code != tt.want.statusCode { t.Errorf("StatusCode should be %d, but got %d", tt.want.statusCode, w.Code)