예제 #1
0
        IObservable <AuthenticationResult> LoginWithApiUser(UserAndScopes userAndScopes)
        {
            return(GetAuthenticationResultForUser(userAndScopes)
                   .SelectMany(result =>
            {
                if (result.IsSuccess())
                {
                    var accountCacheItem = new AccountCacheItem(userAndScopes.User);
                    usage.IncrementLoginCount().Forget();
                    return ModelService.InsertUser(accountCacheItem).Select(_ => result);
                }

                if (result == AuthenticationResult.VerificationFailure)
                {
                    return loginCache.EraseLogin(Address).Select(_ => result);
                }
                return Observable.Return(result);
            })
                   .ObserveOn(RxApp.MainThreadScheduler)
                   .Do(result =>
            {
                if (result.IsSuccess())
                {
                    SupportsGist = userAndScopes.Scopes?.Contains("gist") ?? true;
                    IsLoggedIn = true;
                }

                log.Info("Log in from cache for login '{0}' to host '{1}' {2}",
                         userAndScopes?.User?.Login ?? "(null)",
                         hostAddress.ApiUri,
                         result.IsSuccess() ? "SUCCEEDED" : "FAILED");
            }));
        }
예제 #2
0
 public PullRequestCacheItem(PullRequest pr, IReadOnlyList <PullRequestFile> files)
 {
     Title  = pr.Title;
     Number = pr.Number;
     Base   = new GitReferenceCacheItem
     {
         Label = pr.Base.Label,
         Ref   = pr.Base.Ref,
         Sha   = pr.Base.Sha,
         RepositoryCloneUrl = pr.Base.Repository.CloneUrl,
     };
     Head = new GitReferenceCacheItem
     {
         Label = pr.Head.Label,
         Ref   = pr.Head.Ref,
         Sha   = pr.Head.Sha,
         RepositoryCloneUrl = pr.Head.Repository?.CloneUrl
     };
     CommentCount = pr.Comments + pr.ReviewComments;
     CommitCount  = pr.Commits;
     Author       = new AccountCacheItem(pr.User);
     Assignee     = pr.Assignee != null ? new AccountCacheItem(pr.Assignee) : null;
     CreatedAt    = pr.CreatedAt;
     UpdatedAt    = pr.UpdatedAt;
     Body         = pr.Body;
     ChangedFiles = files.Select(x => new PullRequestFileCacheItem(x)).ToList();
     State        = GetState(pr);
     IsOpen       = pr.State == ItemState.Open;
     Merged       = pr.Merged;
     Key          = Number.ToString(CultureInfo.InvariantCulture);
     Timestamp    = UpdatedAt;
 }
예제 #3
0
        IObservable <AuthenticationResult> LoginWithApiUser(AccountCacheItem user)
        {
            return(GetAuthenticationResultForUser(user)
                   .SelectMany(result =>
            {
                if (result.IsSuccess())
                {
                    return ModelService.InsertUser(user).Select(_ => result);
                }

                if (result == AuthenticationResult.VerificationFailure)
                {
                    return loginCache.EraseLogin(Address).Select(_ => result);
                }
                return Observable.Return(result);
            })
                   .ObserveOn(RxApp.MainThreadScheduler)
                   .Do(result =>
            {
                if (result.IsSuccess())
                {
                    IsLoggedIn = true;
                }

                log.Info("Log in from cache for login '{0}' to host '{1}' {2}",
                         user != null ? user.Login : "******",
                         hostAddress.ApiUri,
                         result.IsSuccess() ? "SUCCEEDED" : "FAILED");
            }));
        }
예제 #4
0
        public async Task <IModelService> CreateAsync(IConnection connection)
        {
            ModelService result;

            await cacheLock.WaitAsync();

            try
            {
                if (!cache.TryGetValue(connection, out result))
                {
                    result = new ModelService(
                        await apiClientFactory.Create(connection.HostAddress),
                        await hostCacheFactory.Create(connection.HostAddress),
                        avatarProvider);
                    result.InsertUser(AccountCacheItem.Create(connection.User));
                    cache.Add(connection, result);
                }
            }
            finally
            {
                cacheLock.Release();
            }

            return(result);
        }
예제 #5
0
 static IObservable <AuthenticationResult> GetAuthenticationResultForUser(AccountCacheItem account)
 {
     return(Observable.Return(account == null ? AuthenticationResult.CredentialFailure
         : account == unverifiedUser
             ? AuthenticationResult.VerificationFailure
             : AuthenticationResult.Success));
 }
예제 #6
0
        public void SetsIsEnterpriseCorrectly(string htmlUrl, bool expected)
        {
            var apiAccount    = CreateOctokitUser("foo", htmlUrl);
            var cachedAccount = new AccountCacheItem(apiAccount);

            Assert.That(expected, Is.EqualTo(cachedAccount.IsEnterprise));
        }
예제 #7
0
 public IssueCommentCacheItem(IssueComment comment)
 {
     Id        = comment.Id;
     User      = new AccountCacheItem(comment.User);
     Body      = comment.Body;
     CreatedAt = comment.CreatedAt;
 }
예제 #8
0
        public IObservable <AuthenticationResult> LogInFromCache()
        {
            Func <Task <AuthenticationResult> > f = async() =>
            {
                try
                {
                    var user = await loginManager.LoginFromCache(Address, ApiClient.GitHubClient);

                    var accountCacheItem = new AccountCacheItem(user);
                    usage.IncrementLoginCount().Forget();
                    await ModelService.InsertUser(accountCacheItem);

                    if (user != unverifiedUser.User)
                    {
                        IsLoggedIn = true;
                        return(AuthenticationResult.Success);
                    }
                    else
                    {
                        return(AuthenticationResult.VerificationFailure);
                    }
                }
                catch (AuthorizationException)
                {
                    return(AuthenticationResult.CredentialFailure);
                }
            };

            return(f().ToObservable());
        }
예제 #9
0
 public RepositoryCacheItem(Repository apiRepository)
 {
     Name     = apiRepository.Name;
     Owner    = AccountCacheItem.Create(apiRepository.Owner);
     CloneUrl = apiRepository.CloneUrl;
     Private  = apiRepository.Private;
     Fork     = apiRepository.Fork;
 }
예제 #10
0
 IAccount Create(AccountCacheItem accountCacheItem)
 {
     return(new Models.Account(
                accountCacheItem.Login,
                accountCacheItem.IsUser,
                accountCacheItem.IsEnterprise,
                accountCacheItem.OwnedPrivateRepositoriesCount,
                accountCacheItem.PrivateRepositoriesInPlanCount,
                avatarProvider.GetAvatar(accountCacheItem)));
 }
예제 #11
0
            public PullRequestCacheItem(PullRequest pr)
            {
                Title        = pr.Title;
                Number       = pr.Number;
                CommentCount = pr.Comments;
                Author       = new AccountCacheItem(pr.User);
                CreatedAt    = pr.CreatedAt;
                UpdatedAt    = pr.UpdatedAt;

                Key       = Number.ToString(CultureInfo.InvariantCulture);
                Timestamp = UpdatedAt;
            }
예제 #12
0
 public PullRequestCacheItem(PullRequest pr)
 {
     Title        = pr.Title;
     Number       = pr.Number;
     CommentCount = pr.Comments + pr.ReviewComments;
     Author       = new AccountCacheItem(pr.User);
     Assignee     = pr.Assignee != null ? new AccountCacheItem(pr.Assignee) : null;
     CreatedAt    = pr.CreatedAt;
     UpdatedAt    = pr.UpdatedAt;
     IsOpen       = pr.State == ItemState.Open;
     Key          = Number.ToString(CultureInfo.InvariantCulture);
     Timestamp    = UpdatedAt;
 }
예제 #13
0
 public RepositoryCacheItem(Repository apiRepository)
 {
     Id        = apiRepository.Id;
     Name      = apiRepository.Name;
     Owner     = AccountCacheItem.Create(apiRepository.Owner);
     CloneUrl  = apiRepository.CloneUrl;
     Private   = apiRepository.Private;
     Fork      = apiRepository.Fork;
     Key       = string.Format(CultureInfo.InvariantCulture, "{0}/{1}", Owner.Login, Name);
     CreatedAt = apiRepository.CreatedAt;
     UpdatedAt = apiRepository.UpdatedAt;
     Timestamp = apiRepository.UpdatedAt;
 }
예제 #14
0
 public PullRequestReviewCacheItem(IPullRequestReviewModel review)
 {
     Id     = review.Id;
     NodeId = review.NodeId;
     User   = new AccountCacheItem
     {
         Login     = review.User.Login,
         AvatarUrl = review.User.AvatarUrl,
     };
     Body        = review.Body;
     State       = review.State;
     SubmittedAt = review.SubmittedAt;
 }
예제 #15
0
 public PullRequestReviewCommentCacheItem(PullRequestReviewComment comment)
 {
     Id               = comment.Id;
     Path             = comment.Path;
     Position         = comment.Position;
     OriginalPosition = comment.OriginalPosition;
     CommitId         = comment.CommitId;
     OriginalCommitId = comment.OriginalCommitId;
     DiffHunk         = comment.DiffHunk;
     User             = new AccountCacheItem(comment.User);
     Body             = comment.Body;
     CreatedAt        = comment.CreatedAt;
 }
예제 #16
0
 public PullRequestReviewCommentCacheItem(IPullRequestReviewCommentModel comment)
 {
     Id     = comment.Id;
     NodeId = comment.NodeId;
     PullRequestReviewId = comment.PullRequestReviewId;
     Path             = comment.Path;
     Position         = comment.Position;
     OriginalPosition = comment.OriginalPosition;
     CommitId         = comment.CommitId;
     OriginalCommitId = comment.OriginalCommitId;
     DiffHunk         = comment.DiffHunk;
     User             = new AccountCacheItem
     {
         Login     = comment.User.Login,
         AvatarUrl = comment.User.AvatarUrl,
     };
     Body      = comment.Body;
     CreatedAt = comment.CreatedAt;
     IsPending = comment.IsPending;
 }
예제 #17
0
        public IObservable <AuthenticationResult> LogIn(string usernameOrEmail, string password)
        {
            Guard.ArgumentNotEmptyString(usernameOrEmail, nameof(usernameOrEmail));
            Guard.ArgumentNotEmptyString(password, nameof(password));

            return(Observable.Defer(async() =>
            {
                var user = await loginManager.Login(Address, ApiClient.GitHubClient, usernameOrEmail, password);
                var accountCacheItem = new AccountCacheItem(user);
                usage.IncrementLoginCount().Forget();
                await ModelService.InsertUser(accountCacheItem);

                if (user != unverifiedUser.User)
                {
                    IsLoggedIn = true;
                    return Observable.Return(AuthenticationResult.Success);
                }
                else
                {
                    return Observable.Return(AuthenticationResult.VerificationFailure);
                }
            }));
        }
예제 #18
0
 public IObservable <Unit> InsertUser(AccountCacheItem user)
 {
     return(hostCache.InsertObject("user", user));
 }