public static AuthenticationBuilder AddGitHub(
            this AuthenticationBuilder builder,
            GitHubAuthContext authContext)
        {
            builder.AddGitHub("GitHub", options =>
            {
                options.ClientId     = authContext.OAuth.ClientId;
                options.ClientSecret = authContext.OAuth.Secret;
                options.Scope.Add("repo");
                options.Scope.Add("read:user");
                options.Events = new OAuthEvents
                {
                    OnTicketReceived = async(ctx) =>
                    {
                        ctx.HandleResponse();
                        await ctx.Response.WriteAsync("Service authorized!");
                    },
                    OnCreatingTicket = async(ctx) =>
                    {
                        IOAuthTicketHandler?handler = ctx.HttpContext.RequestServices
                                                      .GetRequiredService <IOAuthTicketHandler>();

                        await handler.ProcessTicketAsync(ctx);
                    },
                };
            });

            return(builder);
        }
Example #2
0
        public async Task StartAsync(
            int port,
            Guid id,
            Action <IServiceCollection> configure)
        {
            var url = $"http://localhost:{port}";
            GitHubAuthContext authContext = await GetAuthContext(id);

            IHost _host = Host.CreateDefaultBuilder()
                          .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseUrls(url);
                webBuilder.UseStartup <Startup>();
            }).ConfigureLogging(cf =>
            {
                if (!Debugger.IsAttached)
                {
                    cf.SetMinimumLevel(LogLevel.Warning);
                }
            }).ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.AddInMemoryCollection(
                    new Dictionary <string, string>
                {
                    ["Boost:ServiceId"]      = authContext.Id.ToString("N"),
                    ["Boost:OAuth:ClientId"] = authContext.OAuth.ClientId,
                    ["Boost:OAuth:Secret"]   = authContext.OAuth.Secret,
                });
            }).ConfigureServices((ctx, services) =>
            {
                JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
                configure(services);
                services.AddControllersWithViews();
                services.AddHttpContextAccessor();
                services.AddSameSiteOptions();
                services.AddSingleton <IOAuthTicketHandler, OAuthTicketHandler>();

                GitHubAuthContext authContext = ctx.Configuration.GetSection("Boost")
                                                .Get <GitHubAuthContext>();

                services.AddSingleton(authContext);

                services.AddAuthentication(options =>
                {
                    options.DefaultChallengeScheme = "GitHub";
                }).AddGitHub(authContext);

                services.AddHttpClient();
            })

                          .Build();

            Console.WriteLine($"GitHub auth server started on {url}");

            await _host.RunAsync();
        }
Example #3
0
        public async Task ProcessTicketAsync(OAuthCreatingTicketContext ctx)
        {
            IConnectedServiceManager csm = ctx.HttpContext.RequestServices
                                           .GetRequiredService <IConnectedServiceManager>();

            GitHubAuthContext autContext = ctx.HttpContext.RequestServices
                                           .GetRequiredService <GitHubAuthContext>();

            ConnectedService?service = await csm.GetServiceAsync(
                autContext.Id,
                ctx.HttpContext.RequestAborted);

            ConnectedServiceProperty?accessToken = service.Properties
                                                   .FirstOrDefault(x => x.Name == "AccessToken");

            if (accessToken is { })