示例#1
0
        /// <summary>
        /// Gets a temporary bearer token for an application. Refreshes the token as required.
        /// </summary>
        public string GetApplicationBearerToken()
        {
            const int    TOKEN_EXPIRY_GRACE_SECONDS = 60;
            const string grantType = "client_credentials";

            lock (_lock)
            {
                if (string.IsNullOrEmpty(_applicationBearerToken) ||
                    _tPaasTokenExpiryUtc < DateTime.UtcNow)
                {
                    var customHeaders = new HeaderDictionary
                    {
                        { HeaderConstants.ACCEPT, ContentTypeConstants.ApplicationJson },
                        { HeaderConstants.CONTENT_TYPE, ContentTypeConstants.ApplicationFormUrlEncoded },
                        { HeaderConstants.AUTHORIZATION, string.Format($"Basic {_configuration.GetValueString("TPAAS_APP_TOKENKEYS")}") }
                    };
                    TPaasOauthResult tPaasOauthResult;

                    try
                    {
                        //Revoke expired or expiring token
                        if (!string.IsNullOrEmpty(_applicationBearerToken))
                        {
                            var revokeResult = _tpaas.RevokeApplicationBearerToken(_applicationBearerToken, customHeaders).WaitAndUnwrapException();
                            if (revokeResult.Code != 0)
                            {
                                _log.LogInformation($"GetApplicationBearerToken failed to revoke token: {revokeResult.Message}");
                                throw new ServiceException(HttpStatusCode.InternalServerError,
                                                           new ContractExecutionResult(ContractExecutionStatesEnum.InternalProcessingError,
                                                                                       $"Failed to revoke application bearer token: {revokeResult.Message}"));
                            }

                            _applicationBearerToken = null;
                            _tPaasTokenExpiryUtc    = DateTime.MinValue;
                        }
                        //Authenticate to get a token
                        tPaasOauthResult = _tpaas.GetApplicationBearerToken(grantType, customHeaders).WaitAndUnwrapException();

                        var tPaasUrl = _configuration.GetValueString("TPAAS_OAUTH_URL") ?? "null";
                        _log.LogInformation(
                            $"GetApplicationBearerToken() Got new bearer token: TPAAS_OAUTH_URL: {tPaasUrl} grantType: {grantType} customHeaders: {customHeaders.LogHeaders(_logMaxChar)}");
                    }
                    catch (Exception e)
                    {
                        var message =
                            string.Format($"GetApplicationBearerToken call to endpoint failed with exception {e.Message}");

                        throw new ServiceException(HttpStatusCode.InternalServerError,
                                                   new ContractExecutionResult(ContractExecutionStatesEnum.InternalProcessingError,
                                                                               message));
                    }

                    if (tPaasOauthResult.Code != 0)
                    {
                        var message =
                            string.Format($"GetApplicationBearerToken call failed with exception {tPaasOauthResult.Message}");
                        throw new ServiceException(HttpStatusCode.InternalServerError,
                                                   new ContractExecutionResult(ContractExecutionStatesEnum.InternalProcessingError,
                                                                               message));
                    }

                    _applicationBearerToken = tPaasOauthResult.tPaasOauthRawResult.access_token;
                    _tPaasTokenExpiryUtc    = DateTime.UtcNow.AddSeconds(tPaasOauthResult.tPaasOauthRawResult.expires_in - TOKEN_EXPIRY_GRACE_SECONDS);
                }

                _log.LogInformation(
                    $"GetApplicationBearerToken()  Using bearer token: {_applicationBearerToken}");
                return(_applicationBearerToken);
            }
        }