-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
104 additions
and
2 deletions.
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package consumer | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
udm_context "github.com/free5gc/udm/internal/context" | ||
|
||
"github.com/free5gc/udm/internal/logger" | ||
"github.com/free5gc/udm/pkg/factory" | ||
|
||
"github.com/free5gc/openapi" | ||
"github.com/free5gc/openapi/Nnrf_AccessToken" | ||
"github.com/free5gc/openapi/models" | ||
|
||
"github.com/antihax/optional" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
func GetTokenCtx(scope, targetNF string) (context.Context, *models.ProblemDetails, error) { | ||
if factory.UdmConfig.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) { | ||
logger.ConsumerLog.Infof("Send Access Token Request") | ||
var client *Nnrf_AccessToken.APIClient | ||
udmSelf := udm_context.Getself() | ||
// Set client and set url | ||
configuration := Nnrf_AccessToken.NewConfiguration() | ||
configuration.SetBasePath(udmSelf.NrfUri) | ||
if val, ok := udmSelf.ClientMap.Load(configuration); ok { | ||
client = val.(*Nnrf_AccessToken.APIClient) | ||
} else { | ||
client = Nnrf_AccessToken.NewAPIClient(configuration) | ||
udmSelf.ClientMap.Store(configuration, client) | ||
} | ||
|
||
var tok models.AccessTokenRsp | ||
|
||
if val, ok := udmSelf.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", | ||
udmSelf.NfId, scope, &Nnrf_AccessToken.AccessTokenRequestParamOpts{ | ||
NfType: optional.NewInterface(models.NfType_UDM), | ||
TargetNfType: optional.NewInterface(targetNF), | ||
}) | ||
if err == nil { | ||
udmSelf.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") | ||
} | ||
} |
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