/// <summary> /// Retrieves the redirect URI that should be used for authentication. /// </summary> /// <param name="plugin">The OAuth authentication plugin details.</param> /// <returns>The redirect URI.</returns> public static string GetAppropriateRedirectUri ( this PluginInfoConfiguration plugin ) { // Sanity. if (null == plugin) { throw new ArgumentNullException(nameof(plugin)); } return(plugin.Configuration.GetValueOrNull("RedirectURIForNative") ?? plugin.Configuration.GetValueOrNull("RedirectURIForMobile") ?? plugin.Configuration.GetValueOrNull("RedirectURIForWeb") ?? plugin.Configuration.GetValueOrNull("RedirectURIForWOPI") ?? "http://localhost"); }
/// <summary> /// Generates a valid authorization URI for use when doing OAuth authentication. /// </summary> /// <param name="plugin">The OAuth authentication plugin details.</param> /// <param name="state">The state - must not be empty/null - used to passed to the authorization endpoint.</param> /// <param name="forceLogin">If true then the user will be forced to log in, even if they have already authenticated recently.</param> /// <returns>The URI that can be shown in a browser to undertake the OAuth flow.</returns> public static Uri GenerateAuthorizationUri ( this PluginInfoConfiguration plugin, string state, bool forceLogin = false ) { // Sanity. if (null == plugin) { throw new ArgumentNullException(nameof(plugin)); } if (string.IsNullOrWhiteSpace(state)) { throw new ArgumentNullException(nameof(state)); } if (false == plugin.IsOAuthPlugin()) { throw new ArgumentException("The authentication plugin does not refer to an OAuth authentication type", nameof(plugin)); } var promptType = forceLogin ? "login" : null; var redirectUri = plugin.GetAppropriateRedirectUri(); // Build up the URI with mandatory data. var uriBuilder = new UriBuilder(plugin.Configuration.GetValueOrNull("AuthorizationEndpoint")?.ToString()); uriBuilder.SetQueryParam("client_id", plugin.Configuration.GetValueOrNull("ClientID")?.ToString()); uriBuilder.SetQueryParam("redirect_uri", redirectUri); uriBuilder.SetQueryParam("response_type", "code"); // Add the optional items, if set. uriBuilder.SetQueryParamIfNotNullOrWhitespace("scope", plugin.Configuration.GetValueOrNull("Scope")?.ToString()); uriBuilder.SetQueryParamIfNotNullOrWhitespace("state", state); uriBuilder.SetQueryParamIfNotNullOrWhitespace("prompt", promptType); uriBuilder.SetQueryParamIfNotNullOrWhitespace("resource", plugin.Configuration.GetValueOrNull("Resource")?.ToString()); // Return the generated URI. return(uriBuilder.Uri); }
/// <summary> /// Retrieves the client secret. /// </summary> /// <param name="plugin">The OAuth authentication plugin details.</param> /// <returns>The redirect URI.</returns> public static string GetClientSecret ( this PluginInfoConfiguration plugin ) => plugin?.Configuration.GetValueOrNull("ClientSecret");
/// <summary> /// Retrieves the scope. /// </summary> /// <param name="plugin">The OAuth authentication plugin details.</param> /// <returns>The redirect URI.</returns> public static string GetScope ( this PluginInfoConfiguration plugin ) => plugin?.Configuration.GetValueOrNull("Scope");
/// <summary> /// Retrieves the resource. /// </summary> /// <param name="plugin">The OAuth authentication plugin details.</param> /// <returns>The redirect URI.</returns> public static string GetResource ( this PluginInfoConfiguration plugin ) => plugin?.Configuration.GetValueOrNull("Resource");
/// <summary> /// Retrieves the site realm. /// </summary> /// <param name="plugin">The OAuth authentication plugin details.</param> /// <returns>The redirect URI.</returns> public static string GetSiteRealm ( this PluginInfoConfiguration plugin ) => plugin?.Configuration.GetValueOrNull("SiteRealm");
/// <summary> /// Retrieves the token endpoint. /// </summary> /// <param name="plugin">The OAuth authentication plugin details.</param> /// <returns>The redirect URI.</returns> public static string GetTokenEndpoint ( this PluginInfoConfiguration plugin ) => plugin?.Configuration.GetValueOrNull("TokenEndpoint");
/// <summary> /// Returns whether the plugin is an OAuth plugin. /// </summary> /// <param name="plugin">The plugin details.</param> /// <returns>true if the plugin represents an OAuth configuration.</returns> public static bool IsOAuthPlugin(this PluginInfoConfiguration plugin) { return(plugin?.AssemblyName == "MFiles.AuthenticationProviders.OAuth"); }
/// <summary> /// Connects to the vault. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Connect_Click(object sender, RoutedEventArgs e) { // Hide stuff from the UI that we don't need. this.webBrowser.Visibility = Visibility.Hidden; this.vaultContents.Visibility = Visibility.Hidden; this.vaultContents.Items.Clear(); // Attempt to parse the network address. if (false == Uri.TryCreate(this.connectionDetails.NetworkAddress, UriKind.Absolute, out Uri baseUri)) { MessageBox.Show($"Cannot parse {this.connectionDetails.NetworkAddress} as a valid network address."); return; } try { // Set up the RestSharp client. // Note: the base url should be of the form "https://m-files.mycompany.com". this.client = new RestClient(baseUri); // Attempt to get the OAuth details. List <PluginInfoConfiguration> pluginInfoCollection = null; { // Get all the plugin details (there may be multiple). var response = this.client.Execute <List <PluginInfoConfiguration> >(new RestRequest("/REST/server/authenticationprotocols.aspx", Method.GET)); pluginInfoCollection = response.Data; // Save the response cookies, for MSM compatibility. this.client.CookieContainer = this.client.CookieContainer ?? new System.Net.CookieContainer(); if (null != response.Cookies) { foreach (var cookie in response.Cookies) { this.client.CookieContainer.Add(baseUri, new System.Net.Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain)); } } } if (0 == pluginInfoCollection.Count) { MessageBox.Show("No authentication plugins configured"); return; } // Try and get the OAuth-specific plugin. this.oAuthPluginInfo = pluginInfoCollection .FirstOrDefault(info => info.IsOAuthPlugin()); if (null == this.oAuthPluginInfo) { MessageBox.Show("OAuth is not configured on the vault/server."); return; } // Navigate to the authorisation screen. var state = Guid.NewGuid().ToString("B"); this.oAuthPluginInfo.Configuration["state"] = state; this.webBrowser.Navigate($"{this.oAuthPluginInfo.GenerateAuthorizationUri(state)}"); // Show the web browser. this.webBrowser.Visibility = Visibility.Visible; } catch (Exception ex) { MessageBox.Show($"Exception obtaining authentication plugin data: {ex}"); } }