public ImgurOAuth2Tests(ITestOutputHelper testOutputHelper) { LogSettings.RegisterDefaultLogger <XUnitLogger>(LogLevels.Verbose, testOutputHelper); var oAuth2Settings = new OAuth2Settings { AuthorizationUri = ApiUri.AppendSegments("oauth2", "authorize"). ExtendQuery(new Dictionary <string, string> { { "response_type", "code" }, { "client_id", "{ClientId}" }, { "redirect_uri", "{RedirectUrl}" }, // TODO: Add version? { "state", "{State}" } }), TokenUrl = ApiUri.AppendSegments("oauth2", "token"), CloudServiceName = "Imgur", ClientId = ClientId, ClientSecret = ClientSecret, RedirectUrl = "https://getgreenshot.org/oauth/imgur", AuthorizeMode = AuthorizeModes.OutOfBoundAuto }; var behavior = new HttpBehaviour { JsonSerializer = new JsonNetJsonSerializer(), OnHttpClientCreated = httpClient => { httpClient.SetAuthorization("Client-ID", ClientId); httpClient.DefaultRequestHeaders.ExpectContinue = false; } }; _oAuthHttpBehaviour = OAuth2HttpBehaviourFactory.Create(oAuth2Settings, behavior); }
public DropboxDestination(IDropboxConfiguration dropboxPluginConfiguration, IDropboxLanguage dropboxLanguage) { _dropboxPluginConfiguration = dropboxPluginConfiguration; _dropboxLanguage = dropboxLanguage; _oAuth2Settings = new OAuth2Settings { AuthorizationUri = DropboxApiUri. AppendSegments("1", "oauth2", "authorize"). ExtendQuery(new Dictionary <string, string> { { "response_type", "code" }, { "client_id", "{ClientId}" }, { "redirect_uri", "{RedirectUrl}" }, { "state", "{State}" } }), TokenUrl = DropboxApiUri.AppendSegments("1", "oauth2", "token"), CloudServiceName = "Dropbox", ClientId = dropboxPluginConfiguration.ClientId, ClientSecret = dropboxPluginConfiguration.ClientSecret, AuthorizeMode = AuthorizeModes.LocalhostServer, RedirectUrl = "http://localhost:47336", Token = dropboxPluginConfiguration }; _oAuthHttpBehaviour = OAuth2HttpBehaviourFactory.Create(_oAuth2Settings); }
/// <summary> /// Create the JiraApi, using OAuth 1 for the communication, here the HttpClient is configured /// </summary> /// <param name="baseUri">Base URL, e.g. https://yourjiraserver</param> /// <param name="jiraOAuthSettings">JiraOAuthSettings</param> /// <param name="httpSettings">IHttpSettings or null for default</param> public JiraApi(Uri baseUri, JiraOAuthSettings jiraOAuthSettings, IHttpSettings httpSettings = null) : this(baseUri) { var jiraOAuthUri = JiraBaseUri.AppendSegments("plugins", "servlet", "oauth"); var oAuthSettings = new OAuth1Settings { TokenUrl = jiraOAuthUri.AppendSegments("request-token"), TokenMethod = HttpMethod.Post, AccessTokenUrl = jiraOAuthUri.AppendSegments("access-token"), AccessTokenMethod = HttpMethod.Post, CheckVerifier = false, SignatureType = OAuth1SignatureTypes.RsaSha1, Token = jiraOAuthSettings.Token, ClientId = jiraOAuthSettings.ConsumerKey, CloudServiceName = jiraOAuthSettings.CloudServiceName, RsaSha1Provider = jiraOAuthSettings.RsaSha1Provider, AuthorizeMode = jiraOAuthSettings.AuthorizeMode, AuthorizationUri = jiraOAuthUri.AppendSegments("authorize") .ExtendQuery(new Dictionary <string, string> { { OAuth1Parameters.Token.EnumValueOf(), "{RequestToken}" }, { OAuth1Parameters.Callback.EnumValueOf(), "{RedirectUrl}" } }) }; // Configure the OAuth1Settings _behaviour = ConfigureBehaviour(OAuth1HttpBehaviourFactory.Create(oAuthSettings), httpSettings); }
/// <summary> /// Helper method to create content /// </summary> /// <param name="httpBehaviour">IHttpBehaviour</param> /// <param name="inputType">Type</param> /// <param name="content">object</param> /// <param name="contentType">if a specific content-Type is needed, specify it</param> /// <returns>HttpContent</returns> private static HttpContent Create(IHttpBehaviour httpBehaviour, Type inputType, object content, string contentType = null) { HttpContent resultHttpContent; if (typeof(HttpContent).IsAssignableFrom(inputType)) { resultHttpContent = content as HttpContent; } else { var httpContentConverter = httpBehaviour.HttpContentConverters.OrderBy(x => x.Order).FirstOrDefault(x => x.CanConvertToHttpContent(inputType, content)); if (httpContentConverter == null) { return(null); } resultHttpContent = httpContentConverter.ConvertToHttpContent(inputType, content); } if (contentType != null) { resultHttpContent?.SetContentType(contentType); } // Make sure the OnHttpContentCreated function is called if (httpBehaviour.OnHttpContentCreated != null) { return(httpBehaviour.OnHttpContentCreated.Invoke(resultHttpContent)); } return(resultHttpContent); }
/// <summary> /// Create the JenkinsApi object, here the HttpClient is configured /// </summary> /// <param name="baseUri">Base URL, e.g. https://yourjenkinsserver</param> /// <param name="httpSettings">IHttpSettings or null for default</param> private JenkinsClient(Uri baseUri, IHttpSettings httpSettings = null) { JenkinsBaseUri = baseUri ?? throw new ArgumentNullException(nameof(baseUri)); Settings = httpSettings ?? HttpExtensionsGlobals.HttpSettings.ShallowClone(); Settings.PreAuthenticate = true; Behaviour = new HttpBehaviour { HttpSettings = Settings, JsonSerializer = new SimpleJsonSerializer(), OnHttpRequestMessageCreated = httpMessage => { if (_crumbIssuer != null) { httpMessage?.Headers.TryAddWithoutValidation(_crumbIssuer.CrumbRequestField, _crumbIssuer.Crumb); } if (!string.IsNullOrEmpty(_user) && _apiToken != null) { httpMessage?.SetBasicAuthorization(_user, _apiToken); } return(httpMessage); } }; }
/// <summary> /// Create a specify OAuth IHttpBehaviour /// </summary> /// <param name="oAuthSettings">OAuthSettings</param> /// <param name="fromHttpBehaviour">IHttpBehaviour or null</param> /// <returns>IHttpBehaviour</returns> public static OAuth1HttpBehaviour Create(OAuth1Settings oAuthSettings, IHttpBehaviour fromHttpBehaviour = null) { // Get a clone of a IHttpBehaviour (passed or current) var oauthHttpBehaviour = new OAuth1HttpBehaviour(fromHttpBehaviour); // Add a wrapper (delegate handler) which wraps all new HttpMessageHandlers oauthHttpBehaviour.ChainOnHttpMessageHandlerCreated(httpMessageHandler => new OAuth1HttpMessageHandler(oAuthSettings, oauthHttpBehaviour, httpMessageHandler)); return oauthHttpBehaviour; }
/// <summary> /// Create a specify OAuth IHttpBehaviour /// </summary> /// <param name="oAuthSettings">OAuthSettings</param> /// <param name="fromHttpBehaviour">IHttpBehaviour or null</param> /// <returns>IHttpBehaviour</returns> public static OAuth1HttpBehaviour Create(OAuth1Settings oAuthSettings, IHttpBehaviour fromHttpBehaviour = null) { // Get a clone of a IHttpBehaviour (passed or current) var oauthHttpBehaviour = new OAuth1HttpBehaviour(fromHttpBehaviour); // Add a wrapper (delegate handler) which wraps all new HttpMessageHandlers oauthHttpBehaviour.ChainOnHttpMessageHandlerCreated(httpMessageHandler => new OAuth1HttpMessageHandler(oAuthSettings, oauthHttpBehaviour, httpMessageHandler)); return(oauthHttpBehaviour); }
/// <summary> /// Set the specified configuration, if it already exists it is overwritten /// </summary> /// <param name="httpBehaviour">IHttpBehaviour</param> /// <param name="configuration">THttpReqestConfiguration</param> /// <returns>IHttpBehaviour</returns> public static IHttpBehaviour SetConfig(this IHttpBehaviour httpBehaviour, IHttpRequestConfiguration configuration) { // Although this probably doesn't happen... if null is passed HttpBehaviour.Current is used if (httpBehaviour == null) { httpBehaviour = HttpBehaviour.Current; } httpBehaviour.RequestConfigurations[configuration.Name] = configuration; return(httpBehaviour); }
/// <summary> /// Create the JiraApi object, here the HttpClient is configured /// </summary> /// <param name="baseUri">Base URL, e.g. https://yourjiraserver</param> /// <param name="httpSettings">IHttpSettings or null for default</param> private JiraClient(Uri baseUri, IHttpSettings httpSettings = null) { Behaviour = ConfigureBehaviour(new HttpBehaviour(), httpSettings); JiraBaseUri = baseUri; JiraRestUri = baseUri.AppendSegments("rest", "api", "2"); JiraAuthUri = baseUri.AppendSegments("rest", "auth", "1"); JiraAgileRestUri = baseUri.AppendSegments("rest", "agile", "1.0"); JiraGreenhopperRestUri = baseUri.AppendSegments("rest", "greenhopper", "1.0"); }
public OAuthTests(ITestOutputHelper testOutputHelper) { LogSettings.RegisterDefaultLogger<XUnitLogger>(LogLevels.Verbose, testOutputHelper); var oAuthSettings = new OAuth1Settings { ClientId = "key", ClientSecret = "secret", AuthorizeMode = AuthorizeModes.TestPassThrough, TokenUrl = OAuthTestServerUri.AppendSegments("request_token.php"), TokenMethod = HttpMethod.Post, AccessTokenUrl = OAuthTestServerUri.AppendSegments("access_token.php"), AccessTokenMethod = HttpMethod.Post, CheckVerifier = false }; _oAuthHttpBehaviour = OAuth1HttpBehaviourFactory.Create(oAuthSettings); }
public OAuthTests(ITestOutputHelper testOutputHelper) { LogSettings.RegisterDefaultLogger <XUnitLogger>(LogLevels.Verbose, testOutputHelper); var oAuthSettings = new OAuth1Settings { ClientId = "key", ClientSecret = "secret", AuthorizeMode = AuthorizeModes.TestPassThrough, TokenUrl = OAuthTestServerUri.AppendSegments("request_token.php"), TokenMethod = HttpMethod.Post, AccessTokenUrl = OAuthTestServerUri.AppendSegments("access_token.php"), AccessTokenMethod = HttpMethod.Post, CheckVerifier = false }; _oAuthHttpBehaviour = OAuth1HttpBehaviourFactory.Create(oAuthSettings); }
public OAuth2Tests(ITestOutputHelper testOutputHelper) { LogSettings.RegisterDefaultLogger<XUnitLogger>(LogLevels.Verbose, testOutputHelper); var oAuth2Settings = new OAuth2Settings { ClientId = "<client id from google developer console>", ClientSecret = "<client secret from google developer console>", CloudServiceName = "Google", AuthorizeMode = AuthorizeModes.LocalhostServer, TokenUrl = GoogleApiUri.AppendSegments("oauth2", "v4", "token"), AuthorizationUri = new Uri("https://accounts.google.com").AppendSegments("o", "oauth2", "v2", "auth").ExtendQuery(new Dictionary<string, string> { {"response_type", "code"}, {"client_id", "{ClientId}"}, {"redirect_uri", "{RedirectUrl}"}, {"state", "{State}"}, {"scope", GoogleApiUri.AppendSegments("auth", "calendar").AbsoluteUri} }) }; _oAuthHttpBehaviour = OAuth2HttpBehaviourFactory.Create(oAuth2Settings); }
public DropboxDestination( IDropboxConfiguration dropboxPluginConfiguration, IDropboxLanguage dropboxLanguage, INetworkConfiguration networkConfiguration, IResourceProvider resourceProvider, ICoreConfiguration coreConfiguration, IGreenshotLanguage greenshotLanguage, Func <CancellationTokenSource, Owned <PleaseWaitForm> > pleaseWaitFormFactory ) : base(coreConfiguration, greenshotLanguage) { _dropboxPluginConfiguration = dropboxPluginConfiguration; _dropboxLanguage = dropboxLanguage; _resourceProvider = resourceProvider; _pleaseWaitFormFactory = pleaseWaitFormFactory; _oAuth2Settings = new OAuth2Settings { AuthorizationUri = DropboxApiUri. AppendSegments("1", "oauth2", "authorize"). ExtendQuery(new Dictionary <string, string> { { "response_type", "code" }, { "client_id", "{ClientId}" }, { "redirect_uri", "{RedirectUrl}" }, { "state", "{State}" } }), TokenUrl = DropboxApiUri.AppendSegments("1", "oauth2", "token"), CloudServiceName = "Dropbox", ClientId = dropboxPluginConfiguration.ClientId, ClientSecret = dropboxPluginConfiguration.ClientSecret, AuthorizeMode = AuthorizeModes.LocalhostServer, RedirectUrl = "http://localhost:47336", Token = dropboxPluginConfiguration }; var httpBehaviour = OAuth2HttpBehaviourFactory.Create(_oAuth2Settings); _oAuthHttpBehaviour = httpBehaviour; // Use the default network settings httpBehaviour.HttpSettings = networkConfiguration; }
/// <summary> /// Create a HttpMessageHandler which handles the OAuth 2 communication for you /// </summary> /// <param name="oAuth2Settings">OAuth2Settings</param> /// <param name="httpBehaviour">IHttpBehaviour</param> /// <param name="innerHandler">HttpMessageHandler</param> public OAuth2HttpMessageHandler(OAuth2Settings oAuth2Settings, IHttpBehaviour httpBehaviour, HttpMessageHandler innerHandler) : base(innerHandler) { if (oAuth2Settings.ClientId == null) { throw new ArgumentNullException(nameof(oAuth2Settings.ClientId)); } if (oAuth2Settings.ClientSecret == null) { throw new ArgumentNullException(nameof(oAuth2Settings.ClientSecret)); } if (oAuth2Settings.TokenUrl == null) { throw new ArgumentNullException(nameof(oAuth2Settings.TokenUrl)); } _oAuth2Settings = oAuth2Settings; var newHttpBehaviour = httpBehaviour.ShallowClone(); // Remove the OnHttpMessageHandlerCreated newHttpBehaviour.OnHttpMessageHandlerCreated = null; // Use it for internal communication _httpBehaviour = newHttpBehaviour; }
/// <summary> /// Create a HttpMessageHandler which handles the OAuth 2 communication for you /// </summary> /// <param name="oAuth2Settings">OAuth2Settings</param> /// <param name="httpBehaviour">IHttpBehaviour</param> /// <param name="innerHandler">HttpMessageHandler</param> public OAuth2HttpMessageHandler(OAuth2Settings oAuth2Settings, IHttpBehaviour httpBehaviour, HttpMessageHandler innerHandler) : base(innerHandler) { if (oAuth2Settings.ClientId is null) { throw new ArgumentNullException(nameof(oAuth2Settings.ClientId)); } if (oAuth2Settings.ClientSecret is null) { throw new ArgumentNullException(nameof(oAuth2Settings.ClientSecret)); } if (oAuth2Settings.TokenUrl is null) { throw new ArgumentNullException(nameof(oAuth2Settings.TokenUrl)); } _oAuth2Settings = oAuth2Settings; var newHttpBehaviour = httpBehaviour.ShallowClone(); // Remove the OnHttpMessageHandlerCreated newHttpBehaviour.OnHttpMessageHandlerCreated = null; // Use it for internal communication _httpBehaviour = newHttpBehaviour; }
public OAuth2Tests(ITestOutputHelper testOutputHelper) { LogSettings.RegisterDefaultLogger <XUnitLogger>(LogLevels.Verbose, testOutputHelper); var oAuth2Settings = new OAuth2Settings { ClientId = "<client id from google developer console>", ClientSecret = "<client secret from google developer console>", CloudServiceName = "Google", AuthorizeMode = AuthorizeModes.LocalhostServer, TokenUrl = GoogleApiUri.AppendSegments("oauth2", "v4", "token"), AuthorizationUri = new Uri("https://accounts.google.com").AppendSegments("o", "oauth2", "v2", "auth") .ExtendQuery(new Dictionary <string, string> { { "response_type", "code" }, { "client_id", "{ClientId}" }, { "redirect_uri", "{RedirectUrl}" }, { "state", "{State}" }, { "scope", GoogleApiUri.AppendSegments("auth", "calendar").AbsoluteUri } }) }; _oAuthHttpBehaviour = OAuth2HttpBehaviourFactory.Create(oAuth2Settings); }
/// <summary> /// Get the IHttpRequestConfiguration from the IHttpBehaviour /// </summary> /// <typeparam name="THttpRequestConfiguration">Type which implements IHttpRequestConfiguration</typeparam> /// <param name="httpBehaviour">IHttpBehaviour instance, if null HttpBehaviour.Current is used</param> /// <returns>THttpReqestConfiguration</returns> public static THttpRequestConfiguration GetConfig <THttpRequestConfiguration>(this IHttpBehaviour httpBehaviour) where THttpRequestConfiguration : class, IHttpRequestConfiguration, new() { // Although this probably doesn't happen... if null is passed HttpBehaviour.Current is used if (httpBehaviour == null) { httpBehaviour = HttpBehaviour.Current; } IHttpRequestConfiguration configurationUntyped; httpBehaviour.RequestConfigurations.TryGetValue(typeof(THttpRequestConfiguration).Name, out configurationUntyped); return(configurationUntyped as THttpRequestConfiguration ?? new THttpRequestConfiguration()); }
/// <summary> /// Create a OAuthHttpBehaviour /// </summary> /// <param name="httpBehaviour">IHttpBehaviour to wrap</param> public OAuth1HttpBehaviour(IHttpBehaviour httpBehaviour = null) { _wrapped = (httpBehaviour ?? HttpBehaviour.Current).ShallowClone(); }
/// <summary> /// Create the JiraApi object, here the HttpClient is configured /// </summary> /// <param name="baseUri">Base URL, e.g. https://yourjiraserver</param> /// <param name="httpSettings">IHttpSettings or null for default</param> private JiraClient(Uri baseUri, IHttpSettings httpSettings = null) : this(baseUri) { Behaviour = ConfigureBehaviour(new HttpBehaviour(), httpSettings); }
/// <summary> /// Helper method to create content /// </summary> /// <param name="httpBehaviour">IHttpBehaviour</param> /// <param name="contentItem"></param> /// <returns>HttpContent</returns> private static HttpContent Create(IHttpBehaviour httpBehaviour, ContentItem contentItem) { return(Create(httpBehaviour, contentItem.Content.GetType(), contentItem.Content, contentItem.ContentType)); }
/// <summary> /// Create the JiraApi object, here the HttpClient is configured /// </summary> /// <param name="baseUri">Base URL, e.g. https://yourjiraserver</param> /// <param name="httpSettings">IHttpSettings or null for default</param> public JiraApi(Uri baseUri, IHttpSettings httpSettings = null) : this(baseUri) { _behaviour = ConfigureBehaviour(new HttpBehaviour(), httpSettings); }