Skip to content

Commit

Permalink
Feature: NRF consumer support oauth2
Browse files Browse the repository at this point in the history
  • Loading branch information
andy89923 committed Nov 16, 2023
1 parent 492b37a commit da5256a
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 8 deletions.
4 changes: 4 additions & 0 deletions internal/context/ausf_context_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func InitAusfContext(context *AUSFContext) {
context.NfId = uuid.New().String()
context.GroupID = configuration.GroupId
context.NrfUri = configuration.NrfUri
if configuration.NrfCerPem != "" {
context.NrfCerPem = configuration.NrfCerPem
}

context.UriScheme = models.UriScheme(configuration.Sbi.Scheme) // default uri scheme
context.RegisterIPv4 = factory.AusfSbiDefaultIPv4 // default localhost
context.SBIPort = factory.AusfSbiDefaultPort // default port
Expand Down
2 changes: 2 additions & 0 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ type AUSFContext struct {
Url string
UriScheme models.UriScheme
NrfUri string
NrfCerPem string
NfService map[models.ServiceName]models.NfService
PlmnList []models.PlmnId
UdmUeauUrl string
snRegex *regexp.Regexp
EapAkaSupiImsiPrefix bool
OAuth2Required bool
}

type AusfUeContext struct {
Expand Down
25 changes: 25 additions & 0 deletions internal/sbi/consumer/nf_accesstoken.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package consumer

import (
"context"

ausf_context "github.com/free5gc/ausf/internal/context"
"github.com/free5gc/ausf/internal/logger"
"github.com/free5gc/openapi"
"github.com/free5gc/openapi/models"
"github.com/free5gc/openapi/oauth"

Check failure on line 10 in internal/sbi/consumer/nf_accesstoken.go

View workflow job for this annotation

GitHub Actions / build (1.18)

no required module provides package github.com/free5gc/openapi/oauth; to add it:
)

func GetTokenCtx(scope, targetNF string) (context.Context, *models.ProblemDetails, error) {
if ausf_context.GetSelf().OAuth2Required {
logger.ConsumerLog.Debugln("GetToekenCtx")
ausfSelf := ausf_context.GetSelf()
tok, pd, err := oauth.SendAccTokenReq(ausfSelf.NfId, models.NfType_AUSF, scope, targetNF, ausfSelf.NrfUri)
if err != nil {
return nil, pd, err
}
return context.WithValue(context.Background(),
openapi.ContextOAuth2, tok), pd, nil
}
return context.TODO(), nil, nil
}
8 changes: 6 additions & 2 deletions internal/sbi/consumer/nf_discovery.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package consumer

import (
"context"
"fmt"
"net/http"

Expand All @@ -13,11 +12,16 @@ import (
func SendSearchNFInstances(nrfUri string, targetNfType, requestNfType models.NfType,
param Nnrf_NFDiscovery.SearchNFInstancesParamOpts,
) (*models.SearchResult, error) {
ctx, _, err := GetTokenCtx("nnrf-disc", "NRF")
if err != nil {
return nil, err
}

configuration := Nnrf_NFDiscovery.NewConfiguration()
configuration.SetBasePath(nrfUri)
client := Nnrf_NFDiscovery.NewAPIClient(configuration)

result, rsp, rspErr := client.NFInstancesStoreApi.SearchNFInstances(context.TODO(),
result, rsp, rspErr := client.NFInstancesStoreApi.SearchNFInstances(ctx,
targetNfType, requestNfType, &param)
if rspErr != nil {
return nil, fmt.Errorf("NFInstancesStoreApi Response error: %+w", rspErr)
Expand Down
22 changes: 18 additions & 4 deletions internal/sbi/consumer/nf_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@ func BuildNFInstance(ausfContext *ausf_context.AUSFContext) (profile models.NfPr
}

// func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfile) (resouceNrfUri string,
// retrieveNfInstanceID string, err error) {
//
// retrieveNfInstanceID string, err error) {
func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfile) (string, string, error) {
configuration := Nnrf_NFManagement.NewConfiguration()
configuration.SetBasePath(nrfUri)
client := Nnrf_NFManagement.NewAPIClient(configuration)

var res *http.Response
for {
if _, resTmp, err := client.NFInstanceIDDocumentApi.RegisterNFInstance(context.TODO(), nfInstanceId,
profile); err != nil || resTmp == nil {
nf, resTmp, err := client.NFInstanceIDDocumentApi.RegisterNFInstance(context.TODO(), nfInstanceId, profile)
if err != nil || resTmp == nil {
logger.ConsumerLog.Errorf("AUSF register to NRF Error[%v]", err)
time.Sleep(2 * time.Second)
continue
Expand All @@ -64,6 +65,14 @@ func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfil
resourceUri := res.Header.Get("Location")
resourceNrfUri := resourceUri[:strings.Index(resourceUri, "/nnrf-nfm/")]
retrieveNfInstanceID := resourceUri[strings.LastIndex(resourceUri, "/")+1:]

oauth2 := nf.CustomInfo["oauth2"].(bool)
ausf_context.GetSelf().OAuth2Required = oauth2
logger.MainLog.Infoln("OAuth2 setting receive from NRF:", oauth2)
if oauth2 && ausf_context.GetSelf().NrfCerPem == "" {
logger.CfgLog.Error("OAuth2 enable but no nrfCerPem provided in config.")
}

return resourceNrfUri, retrieveNfInstanceID, nil
} else {
fmt.Println(fmt.Errorf("handler returned wrong status code %d", status))
Expand All @@ -76,13 +85,18 @@ func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfil
func SendDeregisterNFInstance() (*models.ProblemDetails, error) {
logger.ConsumerLog.Infof("Send Deregister NFInstance")

ctx, pd, err := GetTokenCtx("nnrf-nfm", "NRF")
if err != nil {
return pd, err
}

ausfSelf := ausf_context.GetSelf()
// Set client and set url
configuration := Nnrf_NFManagement.NewConfiguration()
configuration.SetBasePath(ausfSelf.NrfUri)
client := Nnrf_NFManagement.NewAPIClient(configuration)

res, err := client.NFInstanceIDDocumentApi.DeregisterNFInstance(context.Background(), ausfSelf.NfId)
res, err := client.NFInstanceIDDocumentApi.DeregisterNFInstance(ctx, ausfSelf.NfId)
if err == nil {
return nil, err
} else if res != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/sbi/producer/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ func EapEncodeAttribute(attributeType string, data string) (string, error) {
}

// func eapAkaPrimePrf(ikPrime string, ckPrime string, identity string) (K_encr string, K_aut string, K_re string,
// MSK string, EMSK string) {
//
// MSK string, EMSK string) {
func eapAkaPrimePrf(ikPrime string, ckPrime string, identity string) ([]byte, []byte, []byte, []byte, []byte) {
keyAp := ikPrime + ckPrime

Expand Down
3 changes: 2 additions & 1 deletion internal/sbi/producer/ue_authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ func HandleUeAuthPostRequest(request *httpwrapper.Request) *httpwrapper.Response
}

// func UeAuthPostRequestProcedure(updateAuthenticationInfo models.AuthenticationInfo) (
// response *models.UeAuthenticationCtx, locationURI string, problemDetails *models.ProblemDetails) {
//
// response *models.UeAuthenticationCtx, locationURI string, problemDetails *models.ProblemDetails) {
func UeAuthPostRequestProcedure(updateAuthenticationInfo models.AuthenticationInfo) (*models.UeAuthenticationCtx,
string, *models.ProblemDetails,
) {
Expand Down
1 change: 1 addition & 0 deletions pkg/factory/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Configuration struct {
Sbi *Sbi `yaml:"sbi,omitempty" valid:"required"`
ServiceNameList []string `yaml:"serviceNameList,omitempty" valid:"required"`
NrfUri string `yaml:"nrfUri,omitempty" valid:"url,required"`
NrfCerPem string `yaml:"nrfCerPem,omitempty" valid:"type(string),minstringlength(1),optional"`
PlmnSupportList []models.PlmnId `yaml:"plmnSupportList,omitempty" valid:"required"`
GroupId string `yaml:"groupId,omitempty" valid:"type(string),minstringlength(1)"`
EapAkaSupiImsiPrefix bool `yaml:"eapAkaSupiImsiPrefix,omitempty" valid:"type(bool),optional"`
Expand Down

0 comments on commit da5256a

Please sign in to comment.