private static AccessTokenService CreateTarget(
            IOptions <AuthOptions> optionsAccessor         = null,
            IAccessTokenRepository tokenRepository         = null,
            IGitHubUserModelService gitHubUserModelService = null,
            IHttpContextAccessor contextAccessor           = null)
        {
            if (optionsAccessor == null)
            {
                var options = new AuthOptions {
                    Secret = Faker.Random.AlphaNumeric(32)
                };
                optionsAccessor = Substitute.For <IOptions <AuthOptions> >();
                optionsAccessor.Value.Returns(options);
            }

            tokenRepository = tokenRepository ?? Substitute.For <IAccessTokenRepository>();

            contextAccessor        = contextAccessor ?? Substitute.For <IHttpContextAccessor>();
            gitHubUserModelService = gitHubUserModelService ?? Substitute.For <IGitHubUserModelService>();

            var accessTokenService = new AccessTokenService(optionsAccessor, tokenRepository, gitHubUserModelService,
                                                            contextAccessor);

            return(accessTokenService);
        }
예제 #2
0
        public async Task <IActionResult> CreateToken([FromServices] IGitHubUserModelService gitHubUserModelService, [FromQuery] long gitHubRepositoryId)
        {
            var jsonWebToken = await _accessTokenService.CreateTokenAsync(gitHubRepositoryId);

            var listRepositoriesViewModel = await BuildListRepositoriesViewModel(gitHubUserModelService, gitHubRepositoryId, jsonWebToken);

            return(View("ListRepositories", listRepositoriesViewModel));
        }
 public AccessTokenService(IOptions <AuthOptions> optionsAccessor,
                           IAccessTokenRepository tokenRepository,
                           IGitHubUserModelService gitHubUserModelService,
                           IHttpContextAccessor contextAccessor)
 {
     _optionsAccessor        = optionsAccessor;
     _tokenRepository        = tokenRepository;
     _gitHubUserModelService = gitHubUserModelService;
     _contextAccessor        = contextAccessor;
 }
예제 #4
0
        private async Task <ListRepositoriesViewModel> BuildListRepositoriesViewModel(IGitHubUserModelService gitHubUserModelService, long gitHubRepositoryId, string jsonWebToken)
        {
            var buildListRepositoriesViewModel = await BuildListRepositoriesViewModel(gitHubUserModelService);

            buildListRepositoriesViewModel.CreatedToken       = jsonWebToken;
            buildListRepositoriesViewModel.CreatedTokenRepoId = gitHubRepositoryId;
            return(buildListRepositoriesViewModel);
        }
예제 #5
0
        private async Task <ListRepositoriesViewModel> BuildListRepositoriesViewModel(IGitHubUserModelService gitHubUserModelService)
        {
            var installations = await gitHubUserModelService.GetInstallationsAsync();

            var repositoriesByOwner = installations
                                      .SelectMany(installation => installation.Repositories)
                                      .GroupBy(repository => repository.Owner)
                                      .OrderBy(grouping => grouping.Key)
                                      .ToArray();

            var issuedAccessTokens = await _accessTokenService.GetTokensForUserRepositoriesAsync();

            var tokenLookup = issuedAccessTokens.ToLookup(t => t.GitHubRepositoryId, r => r);

            return(new ListRepositoriesViewModel
            {
                TokenLookup = tokenLookup,
                RepositoriesByOwner = repositoriesByOwner
            });
        }
예제 #6
0
        public async Task <IActionResult> ListRepositories([FromServices] IGitHubUserModelService gitHubUserModelService)
        {
            var listRepositoriesViewModel = await BuildListRepositoriesViewModel(gitHubUserModelService);

            return(View(listRepositoriesViewModel));
        }