Skip to content

Commit 04c86d6

Browse files
authored
Merge pull request #20 from KunLee76/feature/oauth
2 parents a464e64 + 0cb7a81 commit 04c86d6

File tree

18 files changed

+319
-98
lines changed

18 files changed

+319
-98
lines changed

go.mod

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
module github.com/free5gc/pcf
22

3-
go 1.17
3+
go 1.18
44

55
require (
66
github.com/antihax/optional v1.0.0
77
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
88
github.com/cydev/zero v0.0.0-20160322155811-4a4535dd56e7
9-
github.com/free5gc/openapi v1.0.7-0.20231216094313-e15a4ff046f6
9+
github.com/free5gc/openapi v1.0.7-0.20240207073137-2f335d104547
1010
github.com/free5gc/util v1.0.5-0.20231001095115-433858e5be94
1111
github.com/gin-contrib/cors v1.3.1
1212
github.com/gin-gonic/gin v1.9.1
13-
github.com/google/uuid v1.3.0
13+
github.com/google/uuid v1.4.0
1414
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
15-
github.com/sirupsen/logrus v1.8.1
15+
github.com/pkg/errors v0.9.1
16+
github.com/sirupsen/logrus v1.9.3
1617
github.com/urfave/cli v1.22.5
1718
go.mongodb.org/mongo-driver v1.8.4
1819
gopkg.in/yaml.v2 v2.4.0
@@ -43,7 +44,6 @@ require (
4344
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4445
github.com/modern-go/reflect2 v1.0.2 // indirect
4546
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
46-
github.com/pkg/errors v0.9.1 // indirect
4747
github.com/russross/blackfriday/v2 v2.0.1 // indirect
4848
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
4949
github.com/tim-ywliu/nested-logrus-formatter v1.3.2 // indirect
@@ -54,12 +54,12 @@ require (
5454
github.com/xdg-go/stringprep v1.0.2 // indirect
5555
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
5656
golang.org/x/arch v0.3.0 // indirect
57-
golang.org/x/crypto v0.17.0 // indirect
57+
golang.org/x/crypto v0.14.0 // indirect
5858
golang.org/x/net v0.17.0 // indirect
5959
golang.org/x/oauth2 v0.0.0-20210810183815-faf39c7919d5 // indirect
6060
golang.org/x/sync v0.1.0 // indirect
61-
golang.org/x/sys v0.15.0 // indirect
62-
golang.org/x/text v0.14.0 // indirect
61+
golang.org/x/sys v0.13.0 // indirect
62+
golang.org/x/text v0.13.0 // indirect
6363
google.golang.org/appengine v1.6.6 // indirect
6464
google.golang.org/protobuf v1.30.0 // indirect
6565
gopkg.in/h2non/gock.v1 v1.1.2 // indirect

go.sum

Lines changed: 10 additions & 54 deletions
Large diffs are not rendered by default.

internal/context/context.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ type AppSessionData struct {
7171
SmPolicyData *UeSmPolicyData
7272
}
7373

74-
var pcfContext PCFContext
74+
var pcfContext = PCFContext{}
75+
76+
type NFContext interface {
77+
AuthorizationCheck(token string, serviceName models.ServiceName) error
78+
}
79+
80+
var _ NFContext = &PCFContext{}
7581

7682
func InitpcfContext(context *PCFContext) {
7783
config := factory.PcfConfig
@@ -433,12 +439,27 @@ func (c *PCFContext) NewAmfStatusSubscription(subscriptionID string, subscriptio
433439
c.AMFStatusSubsData.Store(subscriptionID, subscriptionData)
434440
}
435441

436-
func (c *PCFContext) GetTokenCtx(scope, targetNF string) (
442+
func (c *PCFContext) GetTokenCtx(serviceName models.ServiceName, targetNF models.NfType) (
437443
context.Context, *models.ProblemDetails, error,
438444
) {
439445
if !c.OAuth2Required {
440446
return context.TODO(), nil, nil
441447
}
442-
return oauth.GetTokenCtx(models.NfType_PCF,
443-
c.NfId, c.NrfUri, scope, targetNF)
448+
return oauth.GetTokenCtx(models.NfType_PCF, targetNF,
449+
c.NfId, c.NrfUri, string(serviceName))
450+
}
451+
452+
func (c *PCFContext) AuthorizationCheck(token string, serviceName models.ServiceName) error {
453+
if !c.OAuth2Required {
454+
logger.UtilLog.Debugf("PCFContext::AuthorizationCheck: OAuth2 not required\n")
455+
return nil
456+
}
457+
// TODO: free5gc webconsole uses npcf-oam but it can't get token since it's not an NF.
458+
if serviceName == models.ServiceName_NPCF_OAM {
459+
logger.UtilLog.Warnf("OAuth2 is enable but namf-oam didn't check token now.")
460+
return nil
461+
}
462+
463+
logger.UtilLog.Debugf("PCFContext::AuthorizationCheck: token[%s] serviceName[%s]\n", token, serviceName)
464+
return oauth.VerifyOAuth(token, string(serviceName), c.NrfCertPem)
444465
}

internal/sbi/ampolicy/routers.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import (
1515

1616
"github.com/gin-gonic/gin"
1717

18+
"github.com/free5gc/openapi/models"
19+
pcf_context "github.com/free5gc/pcf/internal/context"
1820
"github.com/free5gc/pcf/internal/logger"
21+
"github.com/free5gc/pcf/internal/util"
1922
"github.com/free5gc/pcf/pkg/factory"
2023
logger_util "github.com/free5gc/util/logger"
2124
)
@@ -45,6 +48,11 @@ func NewRouter() *gin.Engine {
4548
func AddService(engine *gin.Engine) *gin.RouterGroup {
4649
group := engine.Group(factory.PcfAMpolicyCtlResUriPrefix)
4750

51+
routerAuthorizationCheck := util.NewRouterAuthorizationCheck(models.ServiceName_NPCF_AM_POLICY_CONTROL)
52+
group.Use(func(c *gin.Context) {
53+
routerAuthorizationCheck.Check(c, pcf_context.GetSelf())
54+
})
55+
4856
for _, route := range routes {
4957
switch route.Method {
5058
case "GET":

internal/sbi/bdtpolicy/routers.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import (
1818

1919
"github.com/gin-gonic/gin"
2020

21+
"github.com/free5gc/openapi/models"
22+
pcf_context "github.com/free5gc/pcf/internal/context"
2123
"github.com/free5gc/pcf/internal/logger"
24+
"github.com/free5gc/pcf/internal/util"
2225
"github.com/free5gc/pcf/pkg/factory"
2326
logger_util "github.com/free5gc/util/logger"
2427
)
@@ -48,6 +51,11 @@ func NewRouter() *gin.Engine {
4851
func AddService(engine *gin.Engine) *gin.RouterGroup {
4952
group := engine.Group(factory.PcfBdtPolicyCtlResUriPrefix)
5053

54+
routerAuthorizationCheck := util.NewRouterAuthorizationCheck(models.ServiceName_NPCF_BDTPOLICYCONTROL)
55+
group.Use(func(c *gin.Context) {
56+
routerAuthorizationCheck.Check(c, pcf_context.GetSelf())
57+
})
58+
5159
for _, route := range routes {
5260
switch route.Method {
5361
case "GET":

internal/sbi/consumer/communication.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package consumer
22

33
import (
4-
"context"
54
"fmt"
65
"strings"
76

@@ -24,9 +23,12 @@ func AmfStatusChangeSubscribe(amfUri string, guamiList []models.Guami) (
2423
AmfStatusUri: fmt.Sprintf("%s"+factory.PcfCallbackResUriPrefix+"/amfstatus", pcfSelf.GetIPv4Uri()),
2524
GuamiList: guamiList,
2625
}
27-
26+
ctx, pd, err := pcf_context.GetSelf().GetTokenCtx(models.ServiceName_NAMF_COMM, models.NfType_AMF)
27+
if err != nil {
28+
return pd, err
29+
}
2830
res, httpResp, localErr := client.SubscriptionsCollectionDocumentApi.AMFStatusChangeSubscribe(
29-
context.Background(), subscriptionData)
31+
ctx, subscriptionData)
3032
defer func() {
3133
if rspCloseErr := httpResp.Body.Close(); rspCloseErr != nil {
3234
logger.ConsumerLog.Errorf("AMFStatusChangeSubscribe response body cannot close: %+v",

internal/sbi/consumer/influenceDataSubscription.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package consumer
22

33
import (
4-
"context"
54
"strconv"
65
"strings"
76

@@ -20,10 +19,14 @@ func CreateInfluenceDataSubscription(ue *pcf_context.UeContext, request models.S
2019
logger.ConsumerLog.Warnf("Can't find corresponding UDR with UE[%s]", ue.Supi)
2120
return "", &problemDetail, nil
2221
}
22+
ctx, pd, err := pcf_context.GetSelf().GetTokenCtx(models.ServiceName_NUDR_DR, models.NfType_UDR)
23+
if err != nil {
24+
return "", pd, err
25+
}
2326
udrClient := util.GetNudrClient(ue.UdrUri)
2427
trafficInfluSub := buildTrafficInfluSub(request)
2528
_, httpResp, localErr := udrClient.InfluenceDataSubscriptionsCollectionApi.
26-
ApplicationDataInfluenceDataSubsToNotifyPost(context.Background(), trafficInfluSub)
29+
ApplicationDataInfluenceDataSubsToNotifyPost(ctx, trafficInfluSub)
2730
if localErr == nil {
2831
locationHeader := httpResp.Header.Get("Location")
2932
subscriptionID = locationHeader[strings.LastIndex(locationHeader, "/")+1:]
@@ -70,9 +73,13 @@ func RemoveInfluenceDataSubscription(ue *pcf_context.UeContext, subscriptionID s
7073
logger.ConsumerLog.Warnf("Can't find corresponding UDR with UE[%s]", ue.Supi)
7174
return &problemDetail, nil
7275
}
76+
ctx, pd, err := pcf_context.GetSelf().GetTokenCtx(models.ServiceName_NUDR_DR, models.NfType_UDR)
77+
if err != nil {
78+
return pd, err
79+
}
7380
udrClient := util.GetNudrClient(ue.UdrUri)
7481
httpResp, localErr := udrClient.IndividualInfluenceDataSubscriptionDocumentApi.
75-
ApplicationDataInfluenceDataSubsToNotifySubscriptionIdDelete(context.Background(), subscriptionID)
82+
ApplicationDataInfluenceDataSubsToNotifySubscriptionIdDelete(ctx, subscriptionID)
7683
if localErr == nil {
7784
logger.ConsumerLog.Debugf("Nudr_DataRepository Remove Influence Data Subscription Status %s",
7885
httpResp.Status)

internal/sbi/consumer/nf_discovery.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func SendSearchNFInstances(
2222
configuration.SetBasePath(nrfUri)
2323
client := Nnrf_NFDiscovery.NewAPIClient(configuration)
2424

25-
ctx, _, err := pcf_context.GetSelf().GetTokenCtx("nnrf-disc", "NRF")
25+
ctx, _, err := pcf_context.GetSelf().GetTokenCtx(models.ServiceName_NNRF_DISC, models.NfType_NRF)
2626
if err != nil {
2727
return nil, err
2828
}

internal/sbi/consumer/nf_management.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package consumer
22

33
import (
4-
"context"
54
"fmt"
65
"net/http"
76
"strings"
87
"time"
98

9+
"github.com/pkg/errors"
10+
1011
"github.com/free5gc/openapi"
1112
"github.com/free5gc/openapi/Nnrf_NFManagement"
1213
"github.com/free5gc/openapi/models"
@@ -51,12 +52,18 @@ func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfil
5152
// Set client and set url
5253
configuration := Nnrf_NFManagement.NewConfiguration()
5354
configuration.SetBasePath(nrfUri)
54-
client := Nnrf_NFManagement.NewAPIClient(configuration)
55+
apiClient := Nnrf_NFManagement.NewAPIClient(configuration)
56+
57+
ctx, _, err := pcf_context.GetSelf().GetTokenCtx(models.ServiceName_NNRF_NFM, models.NfType_NRF)
58+
if err != nil {
59+
return "", "",
60+
errors.Errorf("SendRegisterNFInstance error: %+v", err)
61+
}
5562

5663
var res *http.Response
5764
var nf models.NfProfile
5865
for {
59-
nf, res, err = client.NFInstanceIDDocumentApi.RegisterNFInstance(context.TODO(), nfInstanceId, profile)
66+
nf, res, err = apiClient.NFInstanceIDDocumentApi.RegisterNFInstance(ctx, nfInstanceId, profile)
6067
if err != nil || res == nil {
6168
// TODO : add log
6269
fmt.Println(fmt.Errorf("PCF register to NRF Error[%v]", err.Error()))
@@ -102,7 +109,7 @@ func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfil
102109
func SendDeregisterNFInstance() (problemDetails *models.ProblemDetails, err error) {
103110
logger.ConsumerLog.Infof("Send Deregister NFInstance")
104111

105-
ctx, pd, err := pcf_context.GetSelf().GetTokenCtx("nnrf-nfm", "NRF")
112+
ctx, pd, err := pcf_context.GetSelf().GetTokenCtx(models.ServiceName_NNRF_NFM, models.NfType_NRF)
106113
if err != nil {
107114
return pd, err
108115
}

internal/sbi/httpcallback/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func NewRouter() *gin.Engine {
3535

3636
func AddService(engine *gin.Engine) *gin.RouterGroup {
3737
group := engine.Group(factory.PcfCallbackResUriPrefix)
38-
// https://localhost:29507/{factory.PcfCallbackResUriPrefix}/route
38+
3939
for _, route := range routes {
4040
switch route.Method {
4141
case "POST":

0 commit comments

Comments
 (0)