/// <summary> /// Binds current gateway instance. /// </summary> public void Bind(string token, string instanceUrl) { RestClient.BaseUrl = new Uri(instanceUrl); var authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(token); { RestClient.Authenticator = authenticator; } }
private Response <T> ExecuteRequest <T>(IRestRequest request, OAuthToken token) where T : new() { IAuthenticator authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(token?.AccessToken, token?.TokenType); IRestResponse <Response <T> > response = ExecuteRequest <Response <T> >(request, authenticator); ProcessResponse <T>(response); return(response.Data); }
/// <summary> /// Logs in current gateway instance. /// </summary> public LoginResult Login(string code, out string refreshToken) { RestClient client = null; { client = new RestClient(ConfigurationManager.AppSettings["SalesforceBaseUrl"]); } var request = new RestRequest(Method.POST) { Resource = ConfigurationManager.AppSettings["SalesforceTokenResource"], RequestFormat = DataFormat.Json }; IdentityServiceResponse identityResponse = null; TokenResponse response = null; request.AddParameter("grant_type", FLOW_AUTHORIZATION_CODE); request.AddParameter("redirect_uri", ConfigurationManager.AppSettings["SalesforceRedirectUrl"]); request.AddParameter("client_id", ConfigurationManager.AppSettings["SalesforceClientId"]); request.AddParameter("client_secret", ConfigurationManager.AppSettings["SalesforceClientSecret"]); request.AddParameter("code", code); if ((response = client.Execute <TokenResponse>(request).Data) == null) { throw new Exception(String.Format("Salesforce token obtain failed, authorization code: {0}", code)); } var bearer = new OAuth2AuthorizationRequestHeaderAuthenticator(response.AccessToken); { RestClient.BaseUrl = new Uri(response.InstanceUrl); RestClient.Authenticator = bearer; } client = new RestClient(response.Id); client.Authenticator = bearer; var identityRequest = new RestRequest(Method.GET) { RequestFormat = DataFormat.Json }; if ((identityResponse = client.Execute <IdentityServiceResponse>(identityRequest).Data) == null) { throw new InvalidOperationException(String.Format("Salesforce identity failed, id: {0}", response.Id)); } var result = new LoginResult(identityResponse.UserId) { Token = response.AccessToken, InstanceUrl = response.InstanceUrl }; refreshToken = response.RefreshToken; { return(result); } }
/// <summary> /// Logs in current gateway instance. /// </summary> public LoginResult Login(string username, string password, string token) { RestClient client = null; { client = new RestClient(ConfigurationManager.AppSettings["SalesforceBaseUrl"]); } var request = new RestRequest(Method.POST) { Resource = ConfigurationManager.AppSettings["SalesforceTokenResource"], RequestFormat = DataFormat.Json }; IdentityServiceResponse identityResponse = null; TokenResponse response = null; request.AddParameter("grant_type", FLOW_PASSWORD); request.AddParameter("password", String.Format("{0}{1}", password, token)); request.AddParameter("client_id", ConfigurationManager.AppSettings["SalesforceClientId"]); request.AddParameter("client_secret", ConfigurationManager.AppSettings["SalesforceClientSecret"]); request.AddParameter("username", username); if ((response = client.Execute <TokenResponse>(request).Data) == null) { throw new Exception(String.Format("Salesforce token obtain failed, username: {0}", username)); } var bearer = new OAuth2AuthorizationRequestHeaderAuthenticator(response.AccessToken); { RestClient.BaseUrl = new Uri(response.InstanceUrl); RestClient.Authenticator = bearer; } client = new RestClient(response.Id); client.Authenticator = bearer; var identityRequest = new RestRequest(Method.GET) { RequestFormat = DataFormat.Json }; if ((identityResponse = client.Execute <IdentityServiceResponse>(identityRequest).Data) == null) { throw new InvalidOperationException(String.Format("Salesforce identity failed, id: {0}", response.Id)); } var result = new LoginResult(identityResponse.UserId) { Token = response.AccessToken, InstanceUrl = response.InstanceUrl }; return(result); }
private IRestClient GetClient(bool skipAuthentication = false) { IAuthenticator authenticator = null; if (IsAuthenticated && !skipAuthentication) { var token = _resourceOwnerToken ?? _serviceCredentialStore.Credential; authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(token.AccessToken, token.TokenType); } var client = _restClientFactory.BuildRestClient(_baseUri, authenticator); client.AddHandler("application/json", new NewtonsoftJsonDeserializer()); return(client); }
/// <summary> /// Gets the node config from the cluster /// </summary> /// <returns>The new config</returns> private NodeStartUpConfiguration GetConfig() { while (true) { if ((this.CurrentApiToken == null || this.CurrentApiToken.IsExpired) && !this.GetToken()) { return(this.GetFallBackConfig()); } var authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator( this.CurrentApiToken.AccessToken, "Bearer"); var client = new RestClient(this.ConfigurationUrl) { Timeout = 5000, Authenticator = authenticator }; var request = new RestRequest { Method = Method.POST }; Console.WriteLine($"Requesting configuration for {this.ContainerType} with runtime {this.Runtime} and {PackageRepositoryExtensions.CurrentRuntime}"); request.AddJsonBody( new NewNodeTemplateRequest { ContainerType = this.ContainerType, NodeUid = this.Uid, FrameworkRuntimeType = PackageRepositoryExtensions.CurrentRuntime, Runtime = this.Runtime }); var response = client.ExecuteTaskAsync <NodeStartUpConfiguration>(request).GetAwaiter().GetResult(); if (response.ResponseStatus != ResponseStatus.Completed || response.StatusCode == HttpStatusCode.BadGateway) { return(this.GetFallBackConfig()); } if (response.StatusCode != HttpStatusCode.OK) { throw new Exception($"Unexpected response (response code: {response.StatusCode}) from service"); } return(response.Data); } }
public string ExecuteRestCall() { OAuth2AuthorizationRequestHeaderAuthenticator autorizationBearer = null; try { if (_callDescription.Header.TokenHeader) { autorizationBearer = TokenHeader(); } return(BodyCall(autorizationBearer)); } catch (Exception ex) { throw ex; } }
public override T PerformAuthenticateSync <T>(RestConnectionString connection) { // HACK: there has to be a better way to do this that allows callers (e.g. RestSharpClient) to be able to rely on T at compile time if (!string.IsNullOrWhiteSpace(connection.ClientSecret)) { var authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(connection.ClientSecret); return((T)(object)authenticator); } if (!string.IsNullOrWhiteSpace(connection.CertificateThumbprint)) { //TODO: add support for more auth types, e.g. OAuth2 JWT throw new NotSupportedException(); } // anonymous authentication return((T)(object)null); }
public string BodyCall(OAuth2AuthorizationRequestHeaderAuthenticator autorizationBearer) { try { var client = new RestClient(_callDescription.BaseURL); var request = new RestRequest(_callDescription.APIMethod); if (autorizationBearer != null) { client.Authenticator = autorizationBearer; } client.Timeout = 15000; IRestResponse restResponse = ExecuteCall(client, request, _callDescription.HttpVerbose, _callDescription.Body.BodyMediaType, _callDescription.Header.MediaTypeHeader, _callDescription.Body.BodyJsonMessage, _callDescription.Body.BodyRawMessage, _callDescription.Parameters.Parameters); return(restResponse.Content); } catch (Exception ex) { throw ex; } }