-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/oauth #17
Closed
Closed
Feat/oauth #17
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
23616be
implement SendAccTokenReq() & update SendDeregisterNFInstance()
ianchen0119 fc9b532
add GetTokenCtx()
ianchen0119 d6ce425
update SendSearchNFInstances & GetTokenCtx
ianchen0119 6ff4794
add targetNF param for GetTokenCtx()
ianchen0119 937a12e
fix ci error
ianchen0119 bb671e9
move SendAccTokenReq to openapi oauth pkg
tim-ywliu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,13 +7,87 @@ | |
"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" | ||
) | ||
|
||
func GetTokenCtx(scope, targetNF string) (context.Context, *models.ProblemDetails, error) { | ||
if factory.AusfConfig.GetOAuth() { | ||
tok, pd, err := sendAccTokenReq(scope, targetNF) | ||
if err != nil { | ||
return nil, pd, err | ||
} | ||
return context.WithValue(context.Background(), | ||
openapi.ContextOAuth2, tok), pd, nil | ||
} | ||
return context.TODO(), nil, nil | ||
} | ||
|
||
func sendAccTokenReq(scope, targetNF string) (oauth2.TokenSource, *models.ProblemDetails, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can move it to |
||
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 | ||
|
@@ -75,14 +149,18 @@ | |
|
||
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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix gci linter error