Пример #1
0
        public async Task SetYoutubeApiKeyAsync([Remainder] string key)
        {
            var config = Manager.Database.Load <YoutubeConfig>(YoutubeConfig.DocumentName());

            if (config == null)
            {
                config        = new YoutubeConfig();
                config.ApiKey = key;
            }

            Manager.Database.Store(config, YoutubeConfig.DocumentName());
            await ReplyAsync("Key set.");
        }
Пример #2
0
        public void ConfigureServices(HostBuilderContext hostContext, IServiceCollection services)
        {
            var ircConfig     = new IrcConfiguration();
            var youtubeConfig = new YoutubeConfig();

            hostContext.Configuration.GetSection("Youtube").Bind(youtubeConfig);
            hostContext.Configuration.GetSection("Connections:Irc").Bind(ircConfig);
            services.AddSingleton(youtubeConfig);

            services.AddHostedService <Worker>();
            services.AddUnitStrapper();
            services.AddCommandModule();
            services.AddUserModule();
            services.AddIrcConnection(ircConfig, true);
            services.AddIrcBallistic();
        }
Пример #3
0
        public async Task <SubscriptionStatus> IsSubscribedTo(string user, string subbedTo)
        {
            var config = Database.Load <YoutubeConfig>(YoutubeConfig.DocumentName());

            if (config == null)
            {
                return(SubscriptionStatus.Unknown);
            }

            var parameters = $"?part=snippet%2CcontentDetails&forChannelId={subbedTo}&channelId={user}&key={config.ApiKey}";
            var request    = new HttpRequestMessage(HttpMethod.Get, $"https://www.googleapis.com/youtube/v3/subscriptions{parameters}");

            var response = await HttpClient.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                return(SubscriptionStatus.Error);
            }

            var content = await response.Content.ReadAsStringAsync();

            var token      = JToken.Parse(content);
            var firstMatch = token.Value <JToken>("items").FirstOrDefault();

            if (firstMatch == null)
            {
                return(SubscriptionStatus.NotSubscribed);
            }

            if (firstMatch.Value <JToken>("snippet").Value <JToken>("resourceId").Value <JToken>("channelId").ToString().Equals(subbedTo))
            {
                return(SubscriptionStatus.Subscribed);
            }

            return(SubscriptionStatus.Unknown);
        }
Пример #4
0
 public YoutubeCommand(YoutubeConfig youtubeConfig)
 {
     _youtubeConfig = youtubeConfig;
 }
Пример #5
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options => {
                options.AddPolicy(DevCorsPolicy,
                                  builder => {
                    builder
                    .AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader();
                });
            });

            services.AddControllers();

            var builder = new SqlConnectionStringBuilder(
                Configuration.GetConnectionString("DB"));

            services.AddDbContextPool <BangerShareContext>(options => options
                                                           .UseSqlServer(builder.ConnectionString));

            services.AddScoped <UserService>();
            services.AddScoped <PlaylistService>();
            services.AddScoped <SongService>();
            services.AddScoped <FriendService>();
            services.AddScoped <SpotifyAPIService>();
            services.AddScoped <YoutubeAPIService>();

            services.AddScoped <IRepository <User>, UserRepository>();
            services.AddScoped <IRepository <UserPlaylist>, UserPlaylistRepository>();
            services.AddScoped <IRepository <Playlist>, PlaylistRepository>();
            services.AddScoped <IRepository <UserLike>, UserLikeRepository>();
            services.AddScoped <FriendRepository>();
            services.AddScoped <SongRepository>();

            services.AddScoped <IUnitOfWork, UnitOfWork <BangerShareContext> >();

            services.AddSingleton <IPasswordHasher, PasswordHasher>();
            services.AddSingleton <ITokenHandler, Security.Tokens.TokenHandler>();
            services.AddScoped <IAuthenticationService, AuthenticationService>();

            var spoitfyConfig = new SpotifyConfig();

            Configuration.Bind("Spotify", spoitfyConfig);
            services.AddSingleton(spoitfyConfig);

            var youtubeConfig = new YoutubeConfig();

            Configuration.Bind("Youtube", youtubeConfig);
            services.AddSingleton(youtubeConfig);

            services.Configure <TokenOptions>(Configuration.GetSection("TokenOptions"));
            var tokenOptions = Configuration.GetSection("TokenOptions").Get <TokenOptions>();

            var signingConfigurations = new SigningConfigurations();

            services.AddSingleton(signingConfigurations);

            // middleware used to validate access token in header of requests
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(jwtBearerOptions =>
            {
                jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateAudience         = true,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer      = tokenOptions.Issuer,
                    ValidAudience    = tokenOptions.Audience,
                    IssuerSigningKey = signingConfigurations.Key,
                    ClockSkew        = TimeSpan.Zero
                };
            });

            services.AddAutoMapper(typeof(Startup));

            // swagger
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "BangerShare API", Version = "v1"
                });

                // allows for authorization in swagger
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = "JWT Authorization header using the Bearer scheme. " + Environment.NewLine +
                                  "Enter 'Bearer' [space] and then your token in the text input below. " + Environment.NewLine +
                                  "Example: 'Bearer 12345abcdef'",
                    Name   = "Authorization",
                    In     = ParameterLocation.Header,
                    Type   = SecuritySchemeType.ApiKey,
                    Scheme = "Bearer"
                });

                c.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id   = "Bearer"
                            },
                            Scheme = "oauth2",
                            Name   = "Bearer",
                            In     = ParameterLocation.Header,
                        },
                        new List <string>()
                    }
                });
            });
        }