Skip to content

Commit

Permalink
Merge pull request #19 from andy89923/fix/oauth
Browse files Browse the repository at this point in the history
Fix: use models in serviceName
  • Loading branch information
ianchen0119 authored Feb 7, 2024
2 parents 206d1c1 + 0ca0a1a commit f1900ad
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 16 deletions.
10 changes: 5 additions & 5 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Init() {
}

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

var _ NFContext = &NSSFContext{}
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion internal/sbi/consumer/nf_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions internal/sbi/nssaiavailability/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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())
})
Expand Down
4 changes: 1 addition & 3 deletions internal/sbi/nsselection/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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())
})
Expand Down
5 changes: 3 additions & 2 deletions internal/util/router_auth_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
8 changes: 6 additions & 2 deletions internal/util/router_auth_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/gin-gonic/gin"
"github.com/pkg/errors"

"github.com/free5gc/openapi/models"
)

const (
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f1900ad

Please sign in to comment.