Exemple #1
0
        public static async Task <OAuthAuthorization> TryLoadExistingAuthorizationAsync(
            IAuthAdapter oauthAdapter,
            CancellationToken token)
        {
            var existingTokenResponse = await oauthAdapter
                                        .GetStoredRefreshTokenAsync(token)
                                        .ConfigureAwait(false);

            if (oauthAdapter.IsRefreshTokenValid(existingTokenResponse))
            {
                TraceSources.Common.TraceVerbose("Found existing credentials");

                var scopesOfExistingTokenResponse = existingTokenResponse.Scope.Split(' ');
                if (!scopesOfExistingTokenResponse.ContainsAll(oauthAdapter.Scopes))
                {
                    TraceSources.Common.TraceVerbose(
                        "Dropping existing credential as it lacks one or more scopes");

                    // The existing auth might be fine, but it lacks a scope.
                    // Delete it so that it does not cause harm later.
                    await oauthAdapter.DeleteStoredRefreshToken().ConfigureAwait(false);

                    return(null);
                }
                else
                {
                    var credential = oauthAdapter.AuthorizeUsingRefreshToken(existingTokenResponse);

                    var userInfo = await oauthAdapter.QueryUserInfoAsync(
                        credential,
                        token).ConfigureAwait(false);

                    return(new OAuthAuthorization(
                               oauthAdapter,
                               credential,
                               userInfo));
                }
            }
            else
            {
                return(null);
            }
        }