Skip to content

Commit

Permalink
Fix: use models in serviceName when authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
andy89923 committed Feb 6, 2024
1 parent fbd6deb commit 6f8f9a6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 17 deletions.
10 changes: 5 additions & 5 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Init() {
}

type NFContext interface {
AuthorizationCheck(token, serviceName string) error
AuthorizationCheck(token string, serviceName models.ServiceName) error
}

var _ NFContext = &SMFContext{}
Expand Down Expand Up @@ -294,19 +294,19 @@ func GetUEDefaultPathPool(groupName string) *UEDefaultPaths {
return smfContext.UEDefaultPathPool[groupName]
}

func (c *SMFContext) GetTokenCtx(scope string, targetNF models.NfType) (
func (c *SMFContext) 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_SMF, targetNF,
c.NfInstanceID, c.NrfUri, scope)
c.NfInstanceID, c.NrfUri, string(serviceName))
}

func (c *SMFContext) AuthorizationCheck(token, serviceName string) error {
func (c *SMFContext) AuthorizationCheck(token string, serviceName models.ServiceName) error {
if !c.OAuth2Required {
return nil
}
return oauth.VerifyOAuth(token, serviceName, c.NrfCertPem)
return oauth.VerifyOAuth(token, string(serviceName), c.NrfCertPem)
}
6 changes: 2 additions & 4 deletions internal/sbi/eventexposure/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/free5gc/openapi/models"
smf_context "github.com/free5gc/smf/internal/context"
"github.com/free5gc/smf/internal/logger"
"github.com/free5gc/smf/internal/util/oauth"
util_oauth "github.com/free5gc/smf/internal/util/oauth"
"github.com/free5gc/smf/pkg/factory"
logger_util "github.com/free5gc/util/logger"
)
Expand All @@ -35,8 +35,6 @@ type Route struct {
HandlerFunc gin.HandlerFunc
}

const serviceName string = string(models.ServiceName_NSMF_EVENT_EXPOSURE)

// Routes is the list of the generated Route.
type Routes []Route

Expand All @@ -50,7 +48,7 @@ func NewRouter() *gin.Engine {
func AddService(engine *gin.Engine) *gin.RouterGroup {
group := engine.Group(factory.SmfEventExposureResUriPrefix)

routerAuthorizationCheck := util_oauth.NewRouterAuthorizationCheck(serviceName)
routerAuthorizationCheck := util_oauth.NewRouterAuthorizationCheck(models.ServiceName_NSMF_EVENT_EXPOSURE)
group.Use(func(c *gin.Context) {
routerAuthorizationCheck.Check(c, smf_context.GetSelf())
})
Expand Down
6 changes: 2 additions & 4 deletions internal/sbi/pdusession/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/free5gc/openapi/models"
smf_context "github.com/free5gc/smf/internal/context"
"github.com/free5gc/smf/internal/logger"
"github.com/free5gc/smf/internal/util/oauth"
util_oauth "github.com/free5gc/smf/internal/util/oauth"
"github.com/free5gc/smf/pkg/factory"
logger_util "github.com/free5gc/util/logger"
)
Expand All @@ -35,8 +35,6 @@ type Route struct {
HandlerFunc gin.HandlerFunc
}

const serviceName string = string(models.ServiceName_NSMF_PDUSESSION)

// Routes is the list of the generated Route.
type Routes []Route

Expand All @@ -50,7 +48,7 @@ func NewRouter() *gin.Engine {
func AddService(engine *gin.Engine) *gin.RouterGroup {
group := engine.Group(factory.SmfPdusessionResUriPrefix)

routerAuthorizationCheck := util_oauth.NewRouterAuthorizationCheck(serviceName)
routerAuthorizationCheck := util_oauth.NewRouterAuthorizationCheck(models.ServiceName_NSMF_PDUSESSION)
group.Use(func(c *gin.Context) {
routerAuthorizationCheck.Check(c, smf_context.GetSelf())
})
Expand Down
5 changes: 3 additions & 2 deletions internal/util/oauth/router_auth_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import (

"github.com/gin-gonic/gin"

"github.com/free5gc/openapi/models"
smf_context "github.com/free5gc/smf/internal/context"
"github.com/free5gc/smf/internal/logger"
)

type RouterAuthorizationCheck struct {
serviceName string
serviceName models.ServiceName
}

func NewRouterAuthorizationCheck(serviceName string) *RouterAuthorizationCheck {
func NewRouterAuthorizationCheck(serviceName models.ServiceName) *RouterAuthorizationCheck {
return &RouterAuthorizationCheck{
serviceName: serviceName,
}
Expand Down
5 changes: 3 additions & 2 deletions internal/util/oauth/router_auth_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http/httptest"
"testing"

"github.com/free5gc/openapi/models"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
Expand All @@ -20,7 +21,7 @@ func newMockSMFContext() *mockSMFContext {
return &mockSMFContext{}
}

func (m *mockSMFContext) AuthorizationCheck(token string, serviceName string) error {
func (m *mockSMFContext) AuthorizationCheck(token string, serviceName models.ServiceName) error {
if token == Valid {
return nil
}
Expand Down Expand Up @@ -81,7 +82,7 @@ func TestRouterAuthorizationCheck_Check(t *testing.T) {
}
c.Request.Header.Set("Authorization", tt.args.token)

rac := NewRouterAuthorizationCheck("testService")
rac := NewRouterAuthorizationCheck(models.ServiceName("testService"))
rac.Check(c, newMockSMFContext())
if w.Code != tt.want.statusCode {
t.Errorf("StatusCode should be %d, but got %d", tt.want.statusCode, w.Code)
Expand Down

0 comments on commit 6f8f9a6

Please sign in to comment.