From 23616be2c51ef8d9e914a0863c45c4706b30cec1 Mon Sep 17 00:00:00 2001 From: ianchen0119 Date: Thu, 26 Oct 2023 09:57:12 +0000 Subject: [PATCH 1/6] implement SendAccTokenReq() & update SendDeregisterNFInstance() --- internal/context/context.go | 2 + internal/sbi/consumer/nf_management.go | 69 +++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/internal/context/context.go b/internal/context/context.go index 07a21be..581d96a 100644 --- a/internal/context/context.go +++ b/internal/context/context.go @@ -11,6 +11,8 @@ import ( type AUSFContext struct { suciSupiMap sync.Map UePool sync.Map + ClientMap sync.Map + TokenMap sync.Map NfId string GroupID string SBIPort int diff --git a/internal/sbi/consumer/nf_management.go b/internal/sbi/consumer/nf_management.go index 4c4cc8e..fa656b3 100644 --- a/internal/sbi/consumer/nf_management.go +++ b/internal/sbi/consumer/nf_management.go @@ -7,13 +7,74 @@ import ( "strings" "time" + "github.com/antihax/optional" ausf_context "github.com/free5gc/ausf/internal/context" "github.com/free5gc/ausf/internal/logger" "github.com/free5gc/openapi" + "github.com/free5gc/openapi/Nnrf_AccessToken" "github.com/free5gc/openapi/Nnrf_NFManagement" "github.com/free5gc/openapi/models" + "golang.org/x/oauth2" ) +func SendAccTokenReq(scope string) (oauth2.TokenSource, *models.ProblemDetails, error) { + logger.ConsumerLog.Infof("Send Access Token Request") + var client *Nnrf_AccessToken.APIClient + ausfSelf := ausf_context.GetSelf() + // Set client and set url + configuration := Nnrf_AccessToken.NewConfiguration() + configuration.SetBasePath(ausfSelf.NrfUri) + if val, ok := ausfSelf.ClientMap.Load(configuration); ok { + client = val.(*Nnrf_AccessToken.APIClient) + } else { + client = Nnrf_AccessToken.NewAPIClient(configuration) + ausfSelf.ClientMap.Store(configuration, client) + } + + var tok models.AccessTokenRsp + + if val, ok := ausfSelf.TokenMap.Load(scope); ok { + tok = val.(models.AccessTokenRsp) + if int32(time.Now().Unix()) < tok.ExpiresIn { + logger.ConsumerLog.Infof("Token is not expired") + token := &oauth2.Token{ + AccessToken: tok.AccessToken, + TokenType: tok.TokenType, + Expiry: time.Unix(int64(tok.ExpiresIn), 0), + } + return oauth2.StaticTokenSource(token), nil, nil + } + } + + tok, res, err := client.AccessTokenRequestApi.AccessTokenRequest(context.Background(), "client_credentials", + ausfSelf.NfId, scope, &Nnrf_AccessToken.AccessTokenRequestParamOpts{ + NfType: optional.NewInterface(models.NfType_AUSF), + TargetNfType: optional.NewInterface(models.NfType_NRF), + }) + if err == nil { + ausfSelf.TokenMap.Store(scope, tok) + token := &oauth2.Token{ + AccessToken: tok.AccessToken, + TokenType: tok.TokenType, + Expiry: time.Unix(int64(tok.ExpiresIn), 0), + } + return oauth2.StaticTokenSource(token), nil, err + } else if res != nil { + defer func() { + if resCloseErr := res.Body.Close(); resCloseErr != nil { + logger.ConsumerLog.Errorf("AccessTokenRequestApi response body cannot close: %+v", resCloseErr) + } + }() + if res.Status != err.Error() { + return nil, nil, err + } + problem := err.(openapi.GenericOpenAPIError).Model().(models.ProblemDetails) + return nil, &problem, err + } else { + return nil, nil, openapi.ReportError("server no response") + } +} + func BuildNFInstance(ausfContext *ausf_context.AUSFContext) (profile models.NfProfile, err error) { profile.NfInstanceId = ausfContext.NfId profile.NfType = models.NfType_AUSF @@ -75,6 +136,10 @@ func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfil func SendDeregisterNFInstance() (*models.ProblemDetails, error) { logger.ConsumerLog.Infof("Send Deregister NFInstance") + tok, pd, err := SendAccTokenReq("nnrf-nfm") + if err != nil { + return pd, err + } ausfSelf := ausf_context.GetSelf() // Set client and set url @@ -82,7 +147,9 @@ func SendDeregisterNFInstance() (*models.ProblemDetails, error) { configuration.SetBasePath(ausfSelf.NrfUri) client := Nnrf_NFManagement.NewAPIClient(configuration) - res, err := client.NFInstanceIDDocumentApi.DeregisterNFInstance(context.Background(), ausfSelf.NfId) + ctx := context.WithValue(context.Background(), + openapi.ContextOAuth2, tok) + res, err := client.NFInstanceIDDocumentApi.DeregisterNFInstance(ctx, ausfSelf.NfId) if err == nil { return nil, err } else if res != nil { From fc9b532b2b5dce38e5a45620e31788dbbaab5569 Mon Sep 17 00:00:00 2001 From: ianchen0119 Date: Thu, 26 Oct 2023 10:01:25 +0000 Subject: [PATCH 2/6] add GetTokenCtx() --- internal/sbi/consumer/nf_management.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/internal/sbi/consumer/nf_management.go b/internal/sbi/consumer/nf_management.go index fa656b3..e856911 100644 --- a/internal/sbi/consumer/nf_management.go +++ b/internal/sbi/consumer/nf_management.go @@ -17,7 +17,16 @@ import ( "golang.org/x/oauth2" ) -func SendAccTokenReq(scope string) (oauth2.TokenSource, *models.ProblemDetails, error) { +func GetTokenCtx(scope string) (context.Context, *models.ProblemDetails, error) { + tok, pd, err := sendAccTokenReq(scope) + if err != nil { + return nil, pd, err + } + return context.WithValue(context.Background(), + openapi.ContextOAuth2, tok), pd, nil +} + +func sendAccTokenReq(scope string) (oauth2.TokenSource, *models.ProblemDetails, error) { logger.ConsumerLog.Infof("Send Access Token Request") var client *Nnrf_AccessToken.APIClient ausfSelf := ausf_context.GetSelf() @@ -136,7 +145,7 @@ func SendRegisterNFInstance(nrfUri, nfInstanceId string, profile models.NfProfil func SendDeregisterNFInstance() (*models.ProblemDetails, error) { logger.ConsumerLog.Infof("Send Deregister NFInstance") - tok, pd, err := SendAccTokenReq("nnrf-nfm") + ctx, pd, err := GetTokenCtx("nnrf-nfm") if err != nil { return pd, err } @@ -147,8 +156,6 @@ func SendDeregisterNFInstance() (*models.ProblemDetails, error) { configuration.SetBasePath(ausfSelf.NrfUri) client := Nnrf_NFManagement.NewAPIClient(configuration) - ctx := context.WithValue(context.Background(), - openapi.ContextOAuth2, tok) res, err := client.NFInstanceIDDocumentApi.DeregisterNFInstance(ctx, ausfSelf.NfId) if err == nil { return nil, err From d6ce4255cfe5252ee544cd552f6c3df7df88a883 Mon Sep 17 00:00:00 2001 From: ianchen0119 Date: Thu, 26 Oct 2023 10:08:16 +0000 Subject: [PATCH 3/6] update SendSearchNFInstances & GetTokenCtx --- internal/sbi/consumer/nf_discovery.go | 7 +++++-- internal/sbi/consumer/nf_management.go | 14 +++++++++----- pkg/factory/config.go | 7 +++++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/internal/sbi/consumer/nf_discovery.go b/internal/sbi/consumer/nf_discovery.go index 59035ec..041bb1b 100644 --- a/internal/sbi/consumer/nf_discovery.go +++ b/internal/sbi/consumer/nf_discovery.go @@ -1,7 +1,6 @@ package consumer import ( - "context" "fmt" "net/http" @@ -16,8 +15,12 @@ func SendSearchNFInstances(nrfUri string, targetNfType, requestNfType models.NfT configuration := Nnrf_NFDiscovery.NewConfiguration() configuration.SetBasePath(nrfUri) client := Nnrf_NFDiscovery.NewAPIClient(configuration) + ctx, _, err := GetTokenCtx("nnrf-disc") + if err != nil { + return nil, err + } - result, rsp, rspErr := client.NFInstancesStoreApi.SearchNFInstances(context.TODO(), + result, rsp, rspErr := client.NFInstancesStoreApi.SearchNFInstances(ctx, targetNfType, requestNfType, ¶m) if rspErr != nil { return nil, fmt.Errorf("NFInstancesStoreApi Response error: %+w", rspErr) diff --git a/internal/sbi/consumer/nf_management.go b/internal/sbi/consumer/nf_management.go index e856911..0a02ca9 100644 --- a/internal/sbi/consumer/nf_management.go +++ b/internal/sbi/consumer/nf_management.go @@ -10,6 +10,7 @@ import ( "github.com/antihax/optional" ausf_context "github.com/free5gc/ausf/internal/context" "github.com/free5gc/ausf/internal/logger" + "github.com/free5gc/ausf/pkg/factory" "github.com/free5gc/openapi" "github.com/free5gc/openapi/Nnrf_AccessToken" "github.com/free5gc/openapi/Nnrf_NFManagement" @@ -18,12 +19,15 @@ import ( ) func GetTokenCtx(scope string) (context.Context, *models.ProblemDetails, error) { - tok, pd, err := sendAccTokenReq(scope) - if err != nil { - return nil, pd, err + if factory.AusfConfig.GetOAuth() { + tok, pd, err := sendAccTokenReq(scope) + if err != nil { + return nil, pd, err + } + return context.WithValue(context.Background(), + openapi.ContextOAuth2, tok), pd, nil } - return context.WithValue(context.Background(), - openapi.ContextOAuth2, tok), pd, nil + return context.TODO(), nil, nil } func sendAccTokenReq(scope string) (oauth2.TokenSource, *models.ProblemDetails, error) { diff --git a/pkg/factory/config.go b/pkg/factory/config.go index a24ef32..be0ac0c 100644 --- a/pkg/factory/config.go +++ b/pkg/factory/config.go @@ -109,6 +109,13 @@ type Sbi struct { BindingIPv4 string `yaml:"bindingIPv4,omitempty" valid:"host,required"` // IP used to run the server in the node. Port int `yaml:"port,omitempty" valid:"port,required"` Tls *Tls `yaml:"tls,omitempty" valid:"optional"` + OAuth bool `yaml:"oauth,omitempty" valid:"optional"` +} + +func (c *Config) GetOAuth() bool { + c.RLock() + defer c.RUnlock() + return c.Configuration.Sbi.OAuth } func (s *Sbi) validate() (bool, error) { From 6ff4794624726b3ee7a4e059c694dae30abec7ea Mon Sep 17 00:00:00 2001 From: ianchen0119 Date: Fri, 27 Oct 2023 02:36:09 +0000 Subject: [PATCH 4/6] add targetNF param for GetTokenCtx() --- internal/sbi/consumer/nf_discovery.go | 2 +- internal/sbi/consumer/nf_management.go | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/sbi/consumer/nf_discovery.go b/internal/sbi/consumer/nf_discovery.go index 041bb1b..16939c5 100644 --- a/internal/sbi/consumer/nf_discovery.go +++ b/internal/sbi/consumer/nf_discovery.go @@ -15,7 +15,7 @@ func SendSearchNFInstances(nrfUri string, targetNfType, requestNfType models.NfT configuration := Nnrf_NFDiscovery.NewConfiguration() configuration.SetBasePath(nrfUri) client := Nnrf_NFDiscovery.NewAPIClient(configuration) - ctx, _, err := GetTokenCtx("nnrf-disc") + ctx, _, err := GetTokenCtx("nnrf-disc", "NRF") if err != nil { return nil, err } diff --git a/internal/sbi/consumer/nf_management.go b/internal/sbi/consumer/nf_management.go index 0a02ca9..ae90ef4 100644 --- a/internal/sbi/consumer/nf_management.go +++ b/internal/sbi/consumer/nf_management.go @@ -18,9 +18,9 @@ import ( "golang.org/x/oauth2" ) -func GetTokenCtx(scope string) (context.Context, *models.ProblemDetails, error) { +func GetTokenCtx(scope, targetNF string) (context.Context, *models.ProblemDetails, error) { if factory.AusfConfig.GetOAuth() { - tok, pd, err := sendAccTokenReq(scope) + tok, pd, err := sendAccTokenReq(scope, targetNF) if err != nil { return nil, pd, err } @@ -30,7 +30,7 @@ func GetTokenCtx(scope string) (context.Context, *models.ProblemDetails, error) return context.TODO(), nil, nil } -func sendAccTokenReq(scope string) (oauth2.TokenSource, *models.ProblemDetails, error) { +func sendAccTokenReq(scope, targetNF string) (oauth2.TokenSource, *models.ProblemDetails, error) { logger.ConsumerLog.Infof("Send Access Token Request") var client *Nnrf_AccessToken.APIClient ausfSelf := ausf_context.GetSelf() @@ -62,7 +62,7 @@ func sendAccTokenReq(scope string) (oauth2.TokenSource, *models.ProblemDetails, tok, res, err := client.AccessTokenRequestApi.AccessTokenRequest(context.Background(), "client_credentials", ausfSelf.NfId, scope, &Nnrf_AccessToken.AccessTokenRequestParamOpts{ NfType: optional.NewInterface(models.NfType_AUSF), - TargetNfType: optional.NewInterface(models.NfType_NRF), + TargetNfType: optional.NewInterface(targetNF), }) if err == nil { ausfSelf.TokenMap.Store(scope, tok) @@ -149,7 +149,7 @@ 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") + ctx, pd, err := GetTokenCtx("nnrf-nfm", "NRF") if err != nil { return pd, err } From 937a12e6ce824773bff5476bf5fe8595d2c4cd62 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Fri, 3 Nov 2023 17:57:18 +0800 Subject: [PATCH 5/6] fix ci error --- internal/sbi/consumer/nf_management.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/sbi/consumer/nf_management.go b/internal/sbi/consumer/nf_management.go index ae90ef4..8b905b5 100644 --- a/internal/sbi/consumer/nf_management.go +++ b/internal/sbi/consumer/nf_management.go @@ -8,6 +8,7 @@ import ( "time" "github.com/antihax/optional" + ausf_context "github.com/free5gc/ausf/internal/context" "github.com/free5gc/ausf/internal/logger" "github.com/free5gc/ausf/pkg/factory" From bb671e92241b57bd962dbfef60b64a30d5ec72b3 Mon Sep 17 00:00:00 2001 From: Tim Liu Date: Sun, 12 Nov 2023 10:11:50 +0000 Subject: [PATCH 6/6] move SendAccTokenReq to openapi oauth pkg --- go.mod | 2 +- go.sum | 6 +-- internal/context/context.go | 2 - internal/sbi/consumer/nf_discovery.go | 4 +- internal/sbi/consumer/nf_management.go | 71 +++----------------------- 5 files changed, 13 insertions(+), 72 deletions(-) diff --git a/go.mod b/go.mod index 386d587..4de5c01 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/antihax/optional v1.0.0 github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d github.com/bronze1man/radius v0.0.0-20190516032554-afd8baec892d - github.com/free5gc/openapi v1.0.7-0.20230802173229-2b3ded4db293 + github.com/free5gc/openapi v1.0.7-0.20231112094355-a96c3450377e github.com/free5gc/util v1.0.5-0.20231001095115-433858e5be94 github.com/gin-gonic/gin v1.9.1 github.com/google/gopacket v1.1.19 diff --git a/go.sum b/go.sum index ecef5d3..4976549 100644 --- a/go.sum +++ b/go.sum @@ -61,8 +61,8 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/evanphx/json-patch v0.5.2/go.mod h1:ZWS5hhDbVDyob71nXKNL0+PWn6ToqBHMikGIFbs31qQ= -github.com/free5gc/openapi v1.0.7-0.20230802173229-2b3ded4db293 h1:BSIvKCYu7646sE8J9R1L8v2R435otUik3wOFN33csfs= -github.com/free5gc/openapi v1.0.7-0.20230802173229-2b3ded4db293/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/util v1.0.5-0.20231001095115-433858e5be94 h1:tNylIqH/m5Kq+3KuC+jjXGl06Y6EmM8yq61ZUgNrPBY= github.com/free5gc/util v1.0.5-0.20231001095115-433858e5be94/go.mod h1:aMszJZbCkcg5xaGgzya+55jz+OPMsJqPLq5Z3fWDFPE= github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= @@ -308,7 +308,6 @@ golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81R golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= @@ -525,6 +524,7 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/internal/context/context.go b/internal/context/context.go index 581d96a..07a21be 100644 --- a/internal/context/context.go +++ b/internal/context/context.go @@ -11,8 +11,6 @@ import ( type AUSFContext struct { suciSupiMap sync.Map UePool sync.Map - ClientMap sync.Map - TokenMap sync.Map NfId string GroupID string SBIPort int diff --git a/internal/sbi/consumer/nf_discovery.go b/internal/sbi/consumer/nf_discovery.go index 16939c5..ae508fe 100644 --- a/internal/sbi/consumer/nf_discovery.go +++ b/internal/sbi/consumer/nf_discovery.go @@ -20,8 +20,8 @@ func SendSearchNFInstances(nrfUri string, targetNfType, requestNfType models.NfT return nil, err } - result, rsp, rspErr := client.NFInstancesStoreApi.SearchNFInstances(ctx, - targetNfType, requestNfType, ¶m) + result, rsp, rspErr := client.NFInstancesStoreApi.SearchNFInstances( + ctx, targetNfType, requestNfType, ¶m) if rspErr != nil { return nil, fmt.Errorf("NFInstancesStoreApi Response error: %+w", rspErr) } diff --git a/internal/sbi/consumer/nf_management.go b/internal/sbi/consumer/nf_management.go index 8b905b5..7b280c9 100644 --- a/internal/sbi/consumer/nf_management.go +++ b/internal/sbi/consumer/nf_management.go @@ -7,21 +7,22 @@ import ( "strings" "time" - "github.com/antihax/optional" - ausf_context "github.com/free5gc/ausf/internal/context" "github.com/free5gc/ausf/internal/logger" "github.com/free5gc/ausf/pkg/factory" "github.com/free5gc/openapi" - "github.com/free5gc/openapi/Nnrf_AccessToken" "github.com/free5gc/openapi/Nnrf_NFManagement" "github.com/free5gc/openapi/models" - "golang.org/x/oauth2" + "github.com/free5gc/openapi/oauth" ) func GetTokenCtx(scope, targetNF string) (context.Context, *models.ProblemDetails, error) { - if factory.AusfConfig.GetOAuth() { - tok, pd, err := sendAccTokenReq(scope, targetNF) + ausfSelf := ausf_context.GetSelf() + cfg := factory.AusfConfig + if cfg.GetOAuth() { + tok, pd, err := oauth.SendAccTokenReq( + ausfSelf.NfId, models.NfType_AUSF, + scope, targetNF, ausfSelf.NrfUri) if err != nil { return nil, pd, err } @@ -31,64 +32,6 @@ func GetTokenCtx(scope, targetNF string) (context.Context, *models.ProblemDetail return context.TODO(), nil, nil } -func sendAccTokenReq(scope, targetNF string) (oauth2.TokenSource, *models.ProblemDetails, error) { - logger.ConsumerLog.Infof("Send Access Token Request") - var client *Nnrf_AccessToken.APIClient - ausfSelf := ausf_context.GetSelf() - // Set client and set url - configuration := Nnrf_AccessToken.NewConfiguration() - configuration.SetBasePath(ausfSelf.NrfUri) - if val, ok := ausfSelf.ClientMap.Load(configuration); ok { - client = val.(*Nnrf_AccessToken.APIClient) - } else { - client = Nnrf_AccessToken.NewAPIClient(configuration) - ausfSelf.ClientMap.Store(configuration, client) - } - - var tok models.AccessTokenRsp - - if val, ok := ausfSelf.TokenMap.Load(scope); ok { - tok = val.(models.AccessTokenRsp) - if int32(time.Now().Unix()) < tok.ExpiresIn { - logger.ConsumerLog.Infof("Token is not expired") - token := &oauth2.Token{ - AccessToken: tok.AccessToken, - TokenType: tok.TokenType, - Expiry: time.Unix(int64(tok.ExpiresIn), 0), - } - return oauth2.StaticTokenSource(token), nil, nil - } - } - - tok, res, err := client.AccessTokenRequestApi.AccessTokenRequest(context.Background(), "client_credentials", - ausfSelf.NfId, scope, &Nnrf_AccessToken.AccessTokenRequestParamOpts{ - NfType: optional.NewInterface(models.NfType_AUSF), - TargetNfType: optional.NewInterface(targetNF), - }) - if err == nil { - ausfSelf.TokenMap.Store(scope, tok) - token := &oauth2.Token{ - AccessToken: tok.AccessToken, - TokenType: tok.TokenType, - Expiry: time.Unix(int64(tok.ExpiresIn), 0), - } - return oauth2.StaticTokenSource(token), nil, err - } else if res != nil { - defer func() { - if resCloseErr := res.Body.Close(); resCloseErr != nil { - logger.ConsumerLog.Errorf("AccessTokenRequestApi response body cannot close: %+v", resCloseErr) - } - }() - if res.Status != err.Error() { - return nil, nil, err - } - problem := err.(openapi.GenericOpenAPIError).Model().(models.ProblemDetails) - return nil, &problem, err - } else { - return nil, nil, openapi.ReportError("server no response") - } -} - func BuildNFInstance(ausfContext *ausf_context.AUSFContext) (profile models.NfProfile, err error) { profile.NfInstanceId = ausfContext.NfId profile.NfType = models.NfType_AUSF