public static ClaimsIdentity CreateAthleteClaims(
            SegmentChallengeConfiguration configuration,
            Athlete athlete)
        {
            var claimsIdentity = new ClaimsIdentity();

            claimsIdentity.AddClaim(new Claim("sub", athlete.Id.ToString()));
            claimsIdentity.AddClaim(new Claim("name", athlete.GetDisplayName()));

            claimsIdentity.AddClaim(new Claim("user_data", JsonConvert.SerializeObject(new {
                profile_picture = athlete.ProfilePicture,
                birth_date      = athlete.BirthDate?.ToString("yyyy-MM-dd"),
                gender          = athlete.Gender,
                email           = athlete.Email,
                is_admin        = configuration.Administrators.Contains(athlete.Id)
            })));

            return(claimsIdentity);
        }
        public static String CreateAthleteJwt(
            SegmentChallengeConfiguration configuration,
            Athlete athlete)
        {
            var tokenHandler = new JwtSecurityTokenHandler();
            var claims       = CreateAthleteClaims(configuration, athlete);

            // Create JWToken
            var token = tokenHandler.CreateJwtSecurityToken(
                issuer: configuration.BaseUrl,
                audience: configuration.BaseUrl,
                subject: claims,
                notBefore: DateTime.UtcNow,
                expires: DateTime.UtcNow.AddDays(configuration.TokenExpiration),
                signingCredentials:
                new SigningCredentials(
                    new SymmetricSecurityKey(
                        Encoding.UTF8.GetBytes(configuration.SecretKey)),
                    SecurityAlgorithms.HmacSha256Signature)
                );

            return(tokenHandler.WriteToken(token));
        }
Esempio n. 3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var siteConfigSection = Configuration.GetSection("SegmentChallenge");

            services.Configure <SegmentChallengeConfiguration>(siteConfigSection);
            services.Configure <StravaConfiguration>(Configuration.GetSection("Strava"));
            services.Configure <MySqlConfiguration>(Configuration.GetSection("MySql"));

            services.AddLogging();
            services.AddMvc();

            // In production, the React files will be served from this directory
            services.AddSpaStaticFiles(configuration => {
                configuration.RootPath = "ClientApp/build";
            });

            services.AddHsts(options => { options.MaxAge = TimeSpan.FromHours(1); });

            var siteConfiguration = new SegmentChallengeConfiguration();

            siteConfigSection.Bind(siteConfiguration);

            services.AddAuthentication("JwtCookie")
            .AddScheme <JwtCookieOptions, JwtCookieHandler>(
                "JwtCookie",
                options => {
                options.SecretKey    = siteConfiguration.SecretKey;
                options.ClaimsIssuer = siteConfiguration.BaseUrl;
            });

            services.AddSingleton <Func <DbConnection> >(provider => {
                var configuration =
                    provider.GetRequiredService <IOptions <MySqlConfiguration> >().Value;

                return(() => {
                    var builder = new MySqlConnectionStringBuilder {
                        Port = configuration.Port,
                        Server = configuration.Host,
                        Database = configuration.Database,
                        UserID = configuration.User,
                        Password = configuration.Password,
                        CharacterSet = "utf8mb4",
                        SslMode = MySqlSslMode.None,
                        IgnoreCommandTransaction = true
                    };

                    return new MySqlConnection(builder.ToString());
                });
            });

            services.AddSingleton <StravaApiHelper>();

            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddSingleton <IActionContextAccessor, ActionContextAccessor>();

            services.AddSingleton <BackgroundTaskService>();
            services.AddHostedService <BackgroundTaskService>();

            services.AddScoped <EffortRefresher>();

            services.AddSingleton <AutoRefreshService>();
            services.AddHostedService <AutoRefreshService>();
        }