Example #1
0
        public static IObservable <ApplicationAuthorization> CreateAndDeleteExistingApplicationAuthorization(
            this IObservableAuthorizationsClient authorizationsClient,
            string clientId,
            string clientSecret,
            NewAuthorization newAuthorization,
            Func <TwoFactorAuthorizationException, IObservable <TwoFactorChallengeResult> > twoFactorChallengeHandler,
            string twoFactorAuthenticationCode,
            bool retryInvalidTwoFactorCode)
        {
            Ensure.ArgumentNotNull(authorizationsClient, "authorizationsClient");
            Ensure.ArgumentNotNullOrEmptyString(clientId, "clientId");
            Ensure.ArgumentNotNullOrEmptyString(clientSecret, "clientSecret");
            Ensure.ArgumentNotNull(newAuthorization, "newAuthorization");

            // If retryInvalidTwoFactorCode is false, then we only show the TwoFactorDialog when we catch
            // a TwoFactorRequiredException. If it's true, we show it for TwoFactorRequiredException and
            // TwoFactorChallengeFailedException
            Func <TwoFactorAuthorizationException, IObservable <TwoFactorChallengeResult> > twoFactorHandler = ex =>
                                                                                                               retryInvalidTwoFactorCode || ex is TwoFactorRequiredException
                    ? twoFactorChallengeHandler(ex)
                    : Observable.Throw <TwoFactorChallengeResult>(ex);

            return(authorizationsClient.CreateAuthorizationAndDeleteExisting(
                       clientId,
                       clientSecret,
                       newAuthorization,
                       twoFactorAuthenticationCode)
                   .Catch <ApplicationAuthorization, TwoFactorAuthorizationException>(
                       exception => twoFactorHandler(exception)
                       .SelectMany(result =>
                                   result.ResendCodeRequested
                            ? authorizationsClient.CreateAndDeleteExistingApplicationAuthorization(
                                       clientId,
                                       clientSecret,
                                       newAuthorization,
                                       twoFactorHandler,
                                       null, // twoFactorAuthenticationCode
                                       retryInvalidTwoFactorCode)
                            : authorizationsClient.CreateAndDeleteExistingApplicationAuthorization(
                                       clientId,
                                       clientSecret,
                                       newAuthorization,
                                       twoFactorHandler,
                                       result.AuthenticationCode,
                                       retryInvalidTwoFactorCode))));
        }
Example #2
0
 /// <summary>
 /// This method will create a new authorization for the specified OAuth application. If an authorization
 /// for that application already exists for the user and fingerprint, it'll delete the existing one and
 /// recreate it.
 /// </summary>
 /// <remarks>
 /// <para>
 /// This method is typically used to initiate an application authentication flow.
 /// This method allows the caller to provide a callback which is used to retrieve the two-factor code from
 /// the user. Typically the callback is used to show some user interface to the user.
 /// </para>
 /// <para>
 /// See <a href="http://developer.github.com/v3/oauth/#list-your-authorizations">API documentation</a>
 /// for more details.
 /// </para>
 /// </remarks>
 /// <param name="authorizationsClient">The <see cref="IAuthorizationsClient" /> this method extends</param>
 /// <param name="clientId">Client ID for the OAuth application that is requesting the token</param>
 /// <param name="clientSecret">The client secret</param>
 /// <param name="newAuthorization">Defines the scopes and metadata for the token</param>
 /// <param name="twoFactorChallengeHandler">Callback used to retrieve the two-factor authentication code
 /// from the user</param>
 /// <param name="retryInvalidTwoFactorCode">If true, instead of completing when the two factor code supplied
 /// is invalid, we go through the whole cycle again and prompt the two factor dialog.</param>
 /// <returns></returns>
 public static IObservable <ApplicationAuthorization> CreateAndDeleteExistingApplicationAuthorization(
     this IObservableAuthorizationsClient authorizationsClient,
     string clientId,
     string clientSecret,
     NewAuthorization newAuthorization,
     Func <TwoFactorAuthorizationException, IObservable <TwoFactorChallengeResult> > twoFactorChallengeHandler,
     bool retryInvalidTwoFactorCode)
 {
     return(authorizationsClient.CreateAndDeleteExistingApplicationAuthorization(
                clientId,
                clientSecret,
                newAuthorization,
                twoFactorChallengeHandler,
                null,
                retryInvalidTwoFactorCode));
 }