Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite AuthTokenRetrieverLib #109

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

DanClowry
Copy link

This pull request adds a new class, AuthTokenRetrieverServer, that intends to replace the existing AuthTokenRetrieverLib class.

Description

The new class works similarly to the existing one. However, there are some key differences:

  • The new class accepts an Action delegate in its constructor. The delegate is invoked once the user authorised the application and the server retrieves the user's access and refresh tokens. This allows the caller to programmatically obtain the user's new tokens.
  • uhttpsharp has been removed as a dependency and has been replaced by EmbedIO. This was done due to EmbedIO's greater ease of use, community support, and to hide that pesky error message that appears when using uhttpsharp.

AuthTokenRetrieverLib Backwards Compatibility

To maintain backwards compatibility, I have kept the existing AuthTokenRetrieverLib class. It now uses the new AuthTokenRetrieverServer class under the hood but maintains the existing public interface meaning it (should) remain compatible with almost all existing consumers of the class. AuthTokenRetrieverLib has been marked as obsolete and informs developers to use the new AuthTokenRetrieverServer.

However, there are two breaking changes that could affect some edge cases:

  • The internal HttpServer property (internal HttpServer HttpServer { get; private set; }) has been removed from the class. The impact of this change should be extremely small as it can only affect consumers in the same assembly as the class.
  • The HTML of the token success page has been changed. This may affect programs that are parsing the tokens HTML page.

Example usage

// Program execution should be blocked after instantiating the token retriever server.
// Allowing the program to continue executing will most likely cause the server to be disposed of before the user authorises the app.
// This example uses an AutoResetEvent to block execution until the callback is called.
using (var waitHandle = new AutoResetEvent(false))
{
    // This action is passed into the AuthTokenRetrieverServer and is called when
    // the user authorises the application and the server gets their access tokens.
    Action<OAuthToken> tokenCallback = (OAuthToken token) =>
    {
        // Do stuff with tokens here. e.g. save to database
        Console.WriteLine($"Access token: {token.AccessToken}");
        Console.WriteLine($"Refresh token: {token.RefreshToken}");
        
        // Release the thread once we have the tokens and allow execution to continue
        waitHandle.Set();
    };

    // The AuthTokenRetrieverServer implements IDisposable and should be disposed. However, not blocking
    // execution will cause execution to continue and the server will almost immediately fall out of scope and be disposed.
    using (var tokenRetriever = new AuthTokenRetrieverServer(appId, appSecret, port, completedAuthCallback: tokenCallback))
    {
        Console.WriteLine("Please open the following URL in your browser if it doesn't automatically open:");
        Console.WriteLine(tokenRetriever.AuthorisationUrl);

        // Block the thread until waitHandle.Set(); is called in the callback 
        waitHandle.WaitOne();

        Thread.Sleep(100); // Wait a bit for the server to respond with the HTML before it is disposed ¯\_(ツ)_/¯
    }
}

This commit adds a new class, AuthTokenRetrieverServer, that intends to
replace the existing AuthTokenRetrieverLib class.

The new class works similarly to the existing one. However, there are
some key differences.

  - The new class implements IDisposable and should be wrapped in a
  using block, or otherwise disposed of, once it is no longer needed to
  release unmanaged resources.
  - The new class accepts an Action<OAuthToken> delegate in its
  constructor. The delegate is invoked once the user authorised the
  application and the server retrieves the user's access and refresh
  tokens. This allows the caller to programmatically obtain the user's
  new tokens.
  - The new class uses the EmbedIO web server instead of the previous
  uhttpsharp. This was done due to EmbedIO's greater ease of use,
  community support, and to hide that pesky error message that appears
  when using uhttpsharp ;)
BREAKING CHANGES:
  -The internal property HttpServer has been removed from
  class AuthTokenRetrieverLib. The impact of this change should be
  extremely small as it only effects consumers in the same assembly as
  the class.
  - The HTML of the token success page has been changed. This may effect
  programs that are parsing the tokens HTML page.

Changes the AuthTokenRetrieverLib class to use the new
AuthTokenRetrieverServer under the hood while still keeping the existing
external interface.

Removes uhttpsharp from AuthTokenRetriever and AuthTokenRetrieverLib as
they are no longer needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant