diff --git a/go.mod b/go.mod index ad85df4a..4c0c7dda 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/free5gc/aper v1.0.4 github.com/free5gc/nas v1.1.0 github.com/free5gc/ngap v1.0.6 - github.com/free5gc/openapi v1.0.7-0.20231112094355-a96c3450377e + github.com/free5gc/openapi v1.0.7-0.20231216094313-e15a4ff046f6 github.com/free5gc/pfcp v1.0.6 github.com/free5gc/util v1.0.5-0.20231001095115-433858e5be94 github.com/gin-gonic/gin v1.9.1 diff --git a/go.sum b/go.sum index 980590dd..26d9f328 100644 --- a/go.sum +++ b/go.sum @@ -70,8 +70,8 @@ github.com/free5gc/ngap v1.0.6 h1:f9sKqHMNrFZVo9Kp8hAyrCXSoI8l746N5O+DFn7vKHA= github.com/free5gc/ngap v1.0.6/go.mod h1:TG1kwwU/EyIlJ3bxY591rdxpD5ZeYnLZTzoWjcfvrBM= github.com/free5gc/openapi v1.0.4/go.mod h1:KRCnnp0GeK0Bl4gnrX79cQAidKXNENf8VRdG0y9R0Fc= github.com/free5gc/openapi v1.0.6/go.mod h1:iw/N0E+FlX44EEx24IBi2EdZW8v+bkj3ETWPGnlK9DI= -github.com/free5gc/openapi v1.0.7-0.20231112094355-a96c3450377e h1:mXnoioq+fxpChliDl5Uy+m6+Hm7iWrJPZo9mi6BijHE= -github.com/free5gc/openapi v1.0.7-0.20231112094355-a96c3450377e/go.mod h1:qv9KqEucoZSeENPRFGxfTe+33ZWYyiYFx1Rj+H0DoWA= +github.com/free5gc/openapi v1.0.7-0.20231216094313-e15a4ff046f6 h1:8P/wOkTAQMgZJe9pUUNSTE5PWeAdlMrsU9kLsI+VAVE= +github.com/free5gc/openapi v1.0.7-0.20231216094313-e15a4ff046f6/go.mod h1:qv9KqEucoZSeENPRFGxfTe+33ZWYyiYFx1Rj+H0DoWA= github.com/free5gc/pfcp v1.0.6 h1:dKEVyZWozF1G+yk1JXw/1ggtIRI0v362say/Q6VDZTE= github.com/free5gc/pfcp v1.0.6/go.mod h1:WzpW7Zxhx5WONMumNKRWbPn7pl/iTYp2FqRLNiOWUjs= github.com/free5gc/tlv v1.0.2-0.20230131124215-8b6ebd69bf93 h1:QPSSI5zw4goiIfxem9doVyMqTO8iKLQ536pzpET5Y+Q= diff --git a/internal/context/context.go b/internal/context/context.go index 9f3554b9..0282cfb8 100644 --- a/internal/context/context.go +++ b/internal/context/context.go @@ -14,6 +14,7 @@ import ( "github.com/free5gc/openapi/Nnrf_NFManagement" "github.com/free5gc/openapi/Nudm_SubscriberDataManagement" "github.com/free5gc/openapi/models" + "github.com/free5gc/openapi/oauth" "github.com/free5gc/pfcp/pfcpType" "github.com/free5gc/smf/internal/logger" "github.com/free5gc/smf/pkg/factory" @@ -154,9 +155,7 @@ func InitSmfContext(config *factory.Config) { logger.CtxLog.Warn("NRF Uri is empty! Using localhost as NRF IPv4 address.") smfContext.NrfUri = fmt.Sprintf("%s://%s:%d", smfContext.URIScheme, "127.0.0.1", 29510) } - if configuration.NrfCertPem != "" { - smfContext.NrfCertPem = configuration.NrfCertPem - } + smfContext.NrfCertPem = configuration.NrfCertPem if pfcp := configuration.PFCP; pfcp != nil { smfContext.ListenAddr = pfcp.ListenAddr @@ -288,3 +287,13 @@ func GetUserPlaneInformation() *UserPlaneInformation { func GetUEDefaultPathPool(groupName string) *UEDefaultPaths { return smfContext.UEDefaultPathPool[groupName] } + +func (c *SMFContext) GetTokenCtx(scope, targetNF string) ( + context.Context, *models.ProblemDetails, error, +) { + if !c.OAuth2Required { + return context.TODO(), nil, nil + } + return oauth.GetTokenCtx(models.NfType_SMF, + c.NfInstanceID, c.NrfUri, scope, targetNF) +} diff --git a/internal/context/nf_accesstoken.go b/internal/context/nf_accesstoken.go deleted file mode 100644 index c9b5fd7d..00000000 --- a/internal/context/nf_accesstoken.go +++ /dev/null @@ -1,24 +0,0 @@ -package context - -import ( - "context" - - "github.com/free5gc/openapi" - "github.com/free5gc/openapi/models" - "github.com/free5gc/openapi/oauth" - "github.com/free5gc/smf/internal/logger" -) - -func GetTokenCtx(scope, targetNF string) (context.Context, *models.ProblemDetails, error) { - if GetSelf().OAuth2Required { - logger.ConsumerLog.Debugln("GetToekenCtx") - smfSelf := GetSelf() - tok, pd, err := oauth.SendAccTokenReq(smfSelf.NfInstanceID, models.NfType_SMF, scope, targetNF, smfSelf.NrfUri) - if err != nil { - return nil, pd, err - } - return context.WithValue(context.Background(), - openapi.ContextOAuth2, tok), pd, nil - } - return context.TODO(), nil, nil -} diff --git a/internal/context/sm_context.go b/internal/context/sm_context.go index 7b7b6115..308337d0 100644 --- a/internal/context/sm_context.go +++ b/internal/context/sm_context.go @@ -409,7 +409,7 @@ func (smContext *SMContext) PDUAddressToNAS() ([12]byte, uint8) { // PCFSelection will select PCF for this SM Context func (smContext *SMContext) PCFSelection() error { - ctx, _, err := GetTokenCtx("nnrf-disc", "NRF") + ctx, _, err := GetSelf().GetTokenCtx("nnrf-disc", "NRF") if err != nil { return err } diff --git a/internal/sbi/consumer/nf_accesstoken.go b/internal/sbi/consumer/nf_accesstoken.go deleted file mode 100644 index 8a420a85..00000000 --- a/internal/sbi/consumer/nf_accesstoken.go +++ /dev/null @@ -1,25 +0,0 @@ -package consumer - -import ( - "context" - - "github.com/free5gc/openapi" - "github.com/free5gc/openapi/models" - "github.com/free5gc/openapi/oauth" - smf_context "github.com/free5gc/smf/internal/context" - "github.com/free5gc/smf/internal/logger" -) - -func GetTokenCtx(scope, targetNF string) (context.Context, *models.ProblemDetails, error) { - if smf_context.GetSelf().OAuth2Required { - logger.ConsumerLog.Debugln("GetToekenCtx") - smfSelf := smf_context.GetSelf() - tok, pd, err := oauth.SendAccTokenReq(smfSelf.NfInstanceID, models.NfType_SMF, scope, targetNF, smfSelf.NrfUri) - if err != nil { - return nil, pd, err - } - return context.WithValue(context.Background(), - openapi.ContextOAuth2, tok), pd, nil - } - return context.TODO(), nil, nil -} diff --git a/internal/sbi/consumer/nf_discovery.go b/internal/sbi/consumer/nf_discovery.go index b62d141f..c671d534 100644 --- a/internal/sbi/consumer/nf_discovery.go +++ b/internal/sbi/consumer/nf_discovery.go @@ -15,7 +15,7 @@ import ( ) func SendNFDiscoveryUDM() (*models.ProblemDetails, error) { - ctx, pd, err := GetTokenCtx("nnrf-disc", "NRF") + ctx, pd, err := smf_context.GetSelf().GetTokenCtx("nnrf-disc", "NRF") if err != nil { return pd, err } @@ -61,7 +61,7 @@ func SendNFDiscoveryUDM() (*models.ProblemDetails, error) { } func SendNFDiscoveryPCF() (problemDetails *models.ProblemDetails, err error) { - ctx, pd, err := GetTokenCtx("nnrf-disc", "NRF") + ctx, pd, err := smf_context.GetSelf().GetTokenCtx("nnrf-disc", "NRF") if err != nil { return pd, err } @@ -101,7 +101,7 @@ func SendNFDiscoveryPCF() (problemDetails *models.ProblemDetails, err error) { } func SendNFDiscoveryServingAMF(smContext *smf_context.SMContext) (*models.ProblemDetails, error) { - ctx, pd, err := GetTokenCtx("nnrf-disc", "NRF") + ctx, pd, err := smf_context.GetSelf().GetTokenCtx("nnrf-disc", "NRF") if err != nil { return pd, err } diff --git a/internal/sbi/consumer/nf_management.go b/internal/sbi/consumer/nf_management.go index fbf7b4d1..7f85b37a 100644 --- a/internal/sbi/consumer/nf_management.go +++ b/internal/sbi/consumer/nf_management.go @@ -106,7 +106,7 @@ func RetrySendNFRegistration(MaxRetry int) error { func SendNFDeregistration() error { // Check data (Use RESTful DELETE) - ctx, _, err := GetTokenCtx("nnrf-nfm", "NRF") + ctx, _, err := smf_context.GetSelf().GetTokenCtx("nnrf-nfm", "NRF") if err != nil { return err } @@ -136,7 +136,7 @@ func SendNFDeregistration() error { func SendDeregisterNFInstance() (*models.ProblemDetails, error) { logger.ConsumerLog.Infof("Send Deregister NFInstance") - ctx, pd, err := GetTokenCtx("nnrf-nfm", "NRF") + ctx, pd, err := smf_context.GetSelf().GetTokenCtx("nnrf-nfm", "NRF") if err != nil { return pd, err }