Skip to content

Commit

Permalink
Merge pull request #30 from andy89923/refactor/auth-logic
Browse files Browse the repository at this point in the history
Refactor: Move AuthorizationCheck() to context.go
  • Loading branch information
ianchen0119 authored Dec 26, 2023
2 parents 80caa1c + 8c5d435 commit 26fb5a1
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 28 deletions.
11 changes: 11 additions & 0 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,14 @@ func SignNFCert(nfType, nfId string) error {
func GetSelf() *NRFContext {
return &nrfContext
}

func (context *NRFContext) AuthorizationCheck(token, serviceName string) error {
if !factory.NrfConfig.GetOAuth() {
return nil
}
err := oauth.VerifyOAuth(token, serviceName, factory.NrfConfig.GetNrfCertPemPath())
if err != nil {
return err
}
return nil
}
1 change: 1 addition & 0 deletions internal/sbi/discovery/api_nf_instances_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
func HTTPSearchNFInstances(c *gin.Context) {
auth_err := authorizationCheck(c)
if auth_err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()})
return
}

Expand Down
5 changes: 3 additions & 2 deletions internal/sbi/discovery/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (

"github.com/gin-gonic/gin"

nrf_context "github.com/free5gc/nrf/internal/context"
"github.com/free5gc/nrf/internal/logger"
"github.com/free5gc/nrf/internal/util"
"github.com/free5gc/nrf/pkg/factory"
logger_util "github.com/free5gc/util/logger"
)
Expand Down Expand Up @@ -44,7 +44,8 @@ func NewRouter() *gin.Engine {
}

func authorizationCheck(c *gin.Context) error {
return util.AuthorizationCheck(c, "nnrf-disc")
token := c.Request.Header.Get("Authorization")
return nrf_context.GetSelf().AuthorizationCheck(token, "nnrf-disc")
}

func AddService(engine *gin.Engine) *gin.RouterGroup {
Expand Down
14 changes: 14 additions & 0 deletions internal/sbi/management/api_nf_instance_id_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
func HTTPDeregisterNFInstance(c *gin.Context) {
auth_err := authorizationCheck(c)
if auth_err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()})
return
}

Expand All @@ -49,6 +50,12 @@ func HTTPDeregisterNFInstance(c *gin.Context) {

// GetNFInstance - Read the profile of a given NF Instance
func HTTPGetNFInstance(c *gin.Context) {
auth_err := authorizationCheck(c)
if auth_err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()})
return
}

req := httpwrapper.NewRequest(c.Request, nil)
req.Params["nfInstanceID"] = c.Params.ByName("nfInstanceID")

Expand All @@ -72,6 +79,7 @@ func HTTPGetNFInstance(c *gin.Context) {
func HTTPRegisterNFInstance(c *gin.Context) {
// auth_err := authorizationCheck(c)
// if auth_err != nil {
// c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()})
// return
// }

Expand Down Expand Up @@ -131,6 +139,12 @@ func HTTPRegisterNFInstance(c *gin.Context) {

// UpdateNFInstance - Update NF Instance profile
func HTTPUpdateNFInstance(c *gin.Context) {
auth_err := authorizationCheck(c)
if auth_err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()})
return
}

// step 1: retrieve http request body
requestBody, err := c.GetRawData()
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions internal/sbi/management/api_nf_instances_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ import (

// GetNFInstances - Retrieves a collection of NF Instances
func HTTPGetNFInstances(c *gin.Context) {
auth_err := authorizationCheck(c)
if auth_err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()})
return
}

req := httpwrapper.NewRequest(c.Request, nil)
req.Query = c.Request.URL.Query()

Expand Down
12 changes: 12 additions & 0 deletions internal/sbi/management/api_subscription_id_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ import (

// RemoveSubscription - Deletes a subscription
func HTTPRemoveSubscription(c *gin.Context) {
auth_err := authorizationCheck(c)
if auth_err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()})
return
}

req := httpwrapper.NewRequest(c.Request, nil)
req.Params["subscriptionID"] = c.Params.ByName("subscriptionID")

Expand All @@ -44,6 +50,12 @@ func HTTPRemoveSubscription(c *gin.Context) {

// UpdateSubscription - Updates a subscription
func HTTPUpdateSubscription(c *gin.Context) {
auth_err := authorizationCheck(c)
if auth_err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()})
return
}

requestBody, err := c.GetRawData()
if err != nil {
problemDetail := models.ProblemDetails{
Expand Down
6 changes: 6 additions & 0 deletions internal/sbi/management/api_subscriptions_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ import (

// CreateSubscription - Create a new subscription
func HTTPCreateSubscription(c *gin.Context) {
auth_err := authorizationCheck(c)
if auth_err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": auth_err.Error()})
return
}

var subscription models.NrfSubscriptionData

// step 1: retrieve http request body
Expand Down
5 changes: 3 additions & 2 deletions internal/sbi/management/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (

"github.com/gin-gonic/gin"

nrf_context "github.com/free5gc/nrf/internal/context"
"github.com/free5gc/nrf/internal/logger"
"github.com/free5gc/nrf/internal/util"
"github.com/free5gc/nrf/pkg/factory"
logger_util "github.com/free5gc/util/logger"
)
Expand Down Expand Up @@ -44,7 +44,8 @@ func NewRouter() *gin.Engine {
}

func authorizationCheck(c *gin.Context) error {
return util.AuthorizationCheck(c, "nnrf-nfm")
token := c.Request.Header.Get("Authorization")
return nrf_context.GetSelf().AuthorizationCheck(token, "nnrf-nfm")
}

func AddService(engine *gin.Engine) *gin.RouterGroup {
Expand Down
24 changes: 0 additions & 24 deletions internal/util/nf_authorization.go

This file was deleted.

0 comments on commit 26fb5a1

Please sign in to comment.