Exemplo n.º 1
0
        /// <summary>
        /// Secure SMTP Client constructor for OAuth2 authorization
        /// </summary>
        /// <param name="Host">Email server host name</param>
        /// <param name="UserName">User name (host login id)</param>
        /// <param name="OAuth2">Class implementing IOAuth2 interface</param>
        public SecureSmtpClient
        (
            string Host,
            string UserName,
            IOAuth2 OAuth2
        )
        {
            // test all required values
            if (string.IsNullOrWhiteSpace(Host))
            {
                throw new ApplicationException("Host name is missing");
            }
            if (string.IsNullOrWhiteSpace(UserName))
            {
                throw new ApplicationException("User name is missing");
            }
            if (OAuth2 == null)
            {
                throw new ApplicationException("Authorization value is missing");
            }

            // save arguments
            this.Host     = Host;
            this.UserName = UserName;
            this.OAuth2   = OAuth2;
            AuthMethod    = Authentication.OAuth2;
            ConnMethod    = ConnectMethod.Secure;
            return;
        }
Exemplo n.º 2
0
        private bool OAuth2Refresh(IOAuth2 uploader, OAuthControl oauth2)
        {
            try
            {
                if (OAuth2Info.CheckOAuth(uploader.AuthInfo))
                {
                    bool result = uploader.RefreshAccessToken();
                    ConfigureOAuthStatus(oauth2, result);
                    return(result);
                }
            }
            catch (Exception ex)
            {
                ex.ShowError();
            }

            return(false);
        }
Exemplo n.º 3
0
 public GitHubController()
 {
     _oAuth2 = new GitHubOAuth(new GitHubOAuthContext
     {
         GET          = new Uri("https://github.com/login/oauth/authorize"),
         POST         = new Uri("https://github.com/login/oauth/access_token"),
         ClientId     = "c47c852759362b832923",
         ClientSecret = "19828b75a317b4d2ab768bc2372bdd987c9a93df",
         RedirectUri  = "http://localhost:2521/github/callback",
         State        = "wtf",
         Scope        = new GitHubScope
         {
             User  = true,
             Repo  = true,
             Email = true
         }
     });
 }
Exemplo n.º 4
0
 public GoogleController()
 {
     _oAuth2 = new GoogleOAuth(new GoogleOAuthContext
     {
         GET          = new Uri("https://accounts.google.com/o/oauth2/auth"),
         POST         = new Uri("https://accounts.google.com/o/oauth2/token"),
         ClientId     = "381198347835-8r1ibnrlo5t1ml6775gf0kmc49alof9d.apps.googleusercontent.com",
         ClientSecret = "QIDfoV4xL5Do6wNCZIJrQYaZ",
         RedirectUri  = "http://localhost:2521/google/callback",
         State        = "wtf",
         Scope        = new GoogleScope
         {
             Email   = true,
             Profile = true,
             OpenId  = true,
             Tasks   = true
         }
     });
 }
Exemplo n.º 5
0
        void AuthenticateWithBrowser()
        {
            var userAuthProvider = new UserAuthentication(_connection);

            _authProvider = userAuthProvider;

            userAuthProvider.TokenChanged += userAuthProvider_TokenChanged;

            OAuthToken authToken = userAuthProvider.GetToken();

            string savedToken = authToken.ToJson();

            // This can be restored into a UserAuthentication object later to reuse:
            OAuthToken         restoredToken        = OAuthToken.FromJson(savedToken);
            UserAuthentication restoredAuthProvider = new UserAuthentication(_connection, restoredToken);

            // Now the user will not be prompted when we call GetToken
            OAuthToken cachedToken = restoredAuthProvider.GetToken();
        }
Exemplo n.º 6
0
        /// <summary>
        /// Secure SMTP Client constructor for OAuth2 authorization
        /// </summary>
        /// <param name="host">Email server host name</param>
        /// <param name="userName">User name (host login id)</param>
        /// <param name="oAuth2">Class implementing IOAuth2 interface</param>
        public SecureSmtpClient(string host, string userName, IOAuth2 oAuth2)
        {
            // test all required values
            if (string.IsNullOrWhiteSpace(host))
            {
                throw new ApplicationException("Host name is missing");
            }
            if (string.IsNullOrWhiteSpace(userName))
            {
                throw new ApplicationException("User name is missing");
            }

            // save arguments
            this._host     = host;
            this._userName = userName;
            this._oAuth2   = oAuth2 ?? throw new ApplicationException("Authorization value is missing");
            _authMethod    = Authentication.OAuth2;
            _connMethod    = ConnectMethod.Secure;
            return;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Shows how to set up authentication to authenticate the user in an embedded browser form
        /// and get an OAuth2 token by prompting the user for credentials.
        /// </summary>
        private static void AuthenticateWithBrowser()
        {
            // The UserAuthentication class will handle the OAuth2 desktop
            // authentication flow using an embedded WebBrowser form,
            // cache the returned token for later API usage, and handle token refreshes.
            var userAuthProvider = new UserAuthentication(_connection);

            _authProvider = userAuthProvider;

            // optionally register an event handler to be notified if/when the auth
            // token changes
            userAuthProvider.TokenChanged += userAuthProvider_TokenChanged;

            // Retrieve a token from the server
            // Note: the RestApi class will call this as needed so it isn't required
            // to call it before accessing the API. However, manually calling GetToken first
            // is recommended so the app can more gracefully handle authentication errors
            OAuthToken authToken = userAuthProvider.GetToken();


            // OAuth2 tokens can and should be cached across application uses so users
            // don't need to grant access every time they run the application.
            // To do this, call OAuthToken.ToJSon to get a serialized version of
            // the token that can be used later.  Be sure to treat this string as a
            // user password and store it securely!
            // Note that this token will potentially be refreshed during API usage
            // using the OAuth2 token refresh protocol.  If that happens, your application
            // should overwrite the previously saved token with the new token value.
            // You can register for the TokenChanged event to be notified of any new/changed tokens
            // or you can call UserAuthentication.GetToken().ToJson() after using the API
            // to manually retrieve the most current token.
            string savedToken = authToken.ToJson();

            // This can be restored into a UserAuthentication object later to reuse:
            OAuthToken         restoredToken        = OAuthToken.FromJson(savedToken);
            UserAuthentication restoredAuthProvider = new UserAuthentication(_connection, restoredToken);

            // Now the user will not be prompted when we call GetToken
            OAuthToken cachedToken = restoredAuthProvider.GetToken();
        }
Exemplo n.º 8
0
 /// <summary>
 /// RestClient Constructor
 /// </summary>
 /// <param name="connectionInfo">the server/API connection information</param>
 /// <param name="authProvider">authentication provider to use</param>
 public RestClient(ConnectionInfo connectionInfo, IOAuth2 authProvider)
 {
     _connectionInfo = connectionInfo;
     _oauthProvider  = authProvider;
 }
Exemplo n.º 9
0
 /// <summary>
 /// Shows how to set up authentication to use a static/manually created access token.
 /// To create a manual auth token, go to the API Add-on preferences in your TSheets account
 /// and click Add Token.
 /// </summary>
 private static void AuthenticateWithManualToken()
 {
     _authProvider = new StaticAuthentication(_manualToken);
 }
Exemplo n.º 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DataServiceContext"/> class.
 /// </summary>
 /// <param name="connectionInfo">
 /// An instance of a <see cref="ConnectionInfo"/> class.
 /// </param>
 /// <param name="authProvider">
 /// An instance of a <see cref="IOAuth2"/> authentication provider class.
 /// </param>
 public DataServiceContext(ConnectionInfo connectionInfo, IOAuth2 authProvider)
 {
     ConnectionInfo = connectionInfo;
     AuthProvider   = authProvider;
 }
Exemplo n.º 11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DataServiceContext"/> class.
 /// </summary>
 /// <param name="authProvider">
 /// An instance of a <see cref="IOAuth2"/> authentication provider class.
 /// </param>
 public DataServiceContext(IOAuth2 authProvider)
     : this(new ConnectionInfo(), authProvider)
 {
 }
Exemplo n.º 12
0
        public Track(IOAuth2 oauth, T request)
        {
            _oauth = oauth;

            Request = request;
        }
Exemplo n.º 13
0
 /// <summary>
 /// Constructs a tracker
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="client"></param>
 /// <param name="request"></param>
 /// <returns></returns>
 public static ITrack <T> Tracker <T>(this IOAuth2 client, T request) where T : OAuth
 {
     return(new Track <T>(client, request));
 }
Exemplo n.º 14
0
 /// <summary>
 /// Constructs a tracker
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="client"></param>
 /// <returns></returns>
 public static ITrack <T> Tracker <T>(this IOAuth2 client) where T : OAuth, new()
 {
     return(client.Tracker(new T()));
 }