авторизация
This commit is contained in:
@ -1,11 +1,12 @@
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Mapster;
|
||||
using static Consts;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc.Authorization;
|
||||
using Microsoft.OpenApi.Models;
|
||||
|
||||
|
||||
TypeAdapterConfig<PutMeetingDto, Meeting>.NewConfig().Map(d => d.SpeackerImage, s => s.SpeackerImage.JoinFileNames());
|
||||
@ -31,44 +32,51 @@ builder.Services.AddDbContext<ApplicationContext>(options =>
|
||||
builder.Services.AddIdentity<User, IdentityRole>()
|
||||
.AddEntityFrameworkStores<ApplicationContext>();
|
||||
|
||||
builder.Services.AddAuthorization();
|
||||
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
// указывает, будет ли валидироваться издатель при валидации токена
|
||||
ValidateIssuer = true,
|
||||
// строка, представляющая издателя
|
||||
ValidIssuer = AuthOptions.ISSUER,
|
||||
// будет ли валидироваться потребитель токена
|
||||
ValidateAudience = true,
|
||||
// установка потребителя токена
|
||||
ValidAudience = AuthOptions.AUDIENCE,
|
||||
// будет ли валидироваться время существования
|
||||
ValidateLifetime = true,
|
||||
// установка ключа безопасности
|
||||
IssuerSigningKey = AuthOptions.GetSymmetricSecurityKey(),
|
||||
// валидация ключа безопасности
|
||||
ValidateIssuerSigningKey = true,
|
||||
};
|
||||
});
|
||||
// .AddGoogle(googleOptions =>
|
||||
// {
|
||||
// googleOptions.ClientId = configuration["Authentication:Google:ClientId"] ?? throw new NullReferenceException("");
|
||||
// googleOptions.ClientSecret = configuration["Authentication:Google:ClientSecret"] ?? throw new NullReferenceException("");
|
||||
// });
|
||||
builder.Services.AddAuthentication(opt => {
|
||||
opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
})
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
var bearerOptions = new BearerAccessTokenOptions();
|
||||
options.RequireHttpsMetadata = bearerOptions.RequiredHttpsMetadata;
|
||||
options.TokenValidationParameters = bearerOptions.TokenValidationParameters;
|
||||
});
|
||||
|
||||
// builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
|
||||
// .AddEntityFrameworkStores<ApplicationContext>();
|
||||
// builder.Services.AddRazorPages();
|
||||
|
||||
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
{
|
||||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
|
||||
|
||||
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
||||
{
|
||||
Description = "JWT Authorization header using the Bearer scheme. Enter 'Bearer' [space] and then your token in the text input below.",
|
||||
Name = "Authorization",
|
||||
In = ParameterLocation.Header,
|
||||
Type = SecuritySchemeType.ApiKey,
|
||||
Scheme = "Bearer"
|
||||
});
|
||||
|
||||
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
||||
{
|
||||
{
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
Reference = new OpenApiReference
|
||||
{
|
||||
Type = ReferenceType.SecurityScheme,
|
||||
Id = "Bearer"
|
||||
}
|
||||
},
|
||||
new string[] {}
|
||||
}
|
||||
});
|
||||
});
|
||||
builder.Services.AddCors();
|
||||
|
||||
var app = builder.Build();
|
||||
@ -97,100 +105,3 @@ app.MapControllers();
|
||||
//app.MapRazorPages();
|
||||
|
||||
app.Run();
|
||||
|
||||
public class AuthOptions
|
||||
{
|
||||
public const string ISSUER = "MyAuthServer"; // издатель токена
|
||||
public const string AUDIENCE = "MyAuthClient"; // потребитель токена
|
||||
const string KEY = "mysupersecret_secretkey!123"; // ключ для шифрации
|
||||
public static SymmetricSecurityKey GetSymmetricSecurityKey() =>
|
||||
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(KEY));
|
||||
}
|
||||
|
||||
public static class Consts
|
||||
{
|
||||
public const char TOKENS_SEPORATOR = ';';
|
||||
}
|
||||
public static class DataHelpers
|
||||
{
|
||||
public static string JoinFileNames(this IEnumerable<IFormFile> files) => files.Select(s => s.FileName).JoinStrings();
|
||||
|
||||
public static string JoinStrings(this IEnumerable<string> files) => String.Join(TOKENS_SEPORATOR, files.Select(s => s));
|
||||
|
||||
public static TimeSpan ParseDuration(this string duration)
|
||||
{
|
||||
var durArr = duration.Split(TOKENS_SEPORATOR, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
var hours = int.Parse(durArr.First());
|
||||
var minutes = int.Parse(durArr[1]);
|
||||
|
||||
return new TimeSpan(hours, minutes, 0);
|
||||
|
||||
}
|
||||
|
||||
public static async Task WriteFileToDirectory(this IFormFile file)
|
||||
{
|
||||
var readStream = file.OpenReadStream();
|
||||
var memstream = new MemoryStream();
|
||||
await readStream.CopyToAsync(memstream);
|
||||
await File.WriteAllBytesAsync(Path.Combine("cyber-boom-files", file.FileName), memstream.ToArray());
|
||||
}
|
||||
|
||||
public static async Task WriteFilesToDirectory(this IEnumerable<IFormFile> files)
|
||||
{
|
||||
foreach(var file in files)
|
||||
{
|
||||
await file.WriteFileToDirectory();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static async Task<StatsData> GetStatistic(this ApplicationContext applicationContext, string id)
|
||||
{
|
||||
var specialities = await applicationContext.UserWriteToMetings.Where(c => c.UserId == id)
|
||||
.Join(applicationContext.Meetings,
|
||||
m => m.MeetingId,
|
||||
m => m.Id,
|
||||
(c,m) => new {
|
||||
m.Tags,
|
||||
m.Id,
|
||||
m.Duration,
|
||||
m.Time
|
||||
}
|
||||
).Where(t => DateTime.UtcNow > t.Time).ToArrayAsync();
|
||||
|
||||
var selectedSpecialities = specialities.Select(s => new {
|
||||
s.Id,
|
||||
Tags = s.Tags.Split(TOKENS_SEPORATOR, StringSplitOptions.RemoveEmptyEntries),
|
||||
Duration = s.Duration.ParseDuration().TotalHours
|
||||
});
|
||||
|
||||
var allTags = selectedSpecialities.SelectMany(s => s.Tags).Distinct();
|
||||
var count = selectedSpecialities.Count();
|
||||
|
||||
StatsData stats = new StatsData{
|
||||
Count = count,
|
||||
Hours = selectedSpecialities.Sum(m => m.Duration) * count
|
||||
|
||||
};
|
||||
foreach(var tag in allTags)
|
||||
{
|
||||
//StatsData.TagStats
|
||||
var specByTag = selectedSpecialities.Where(f => f.Tags.Contains(tag));
|
||||
var countByTag = specByTag.Count();
|
||||
var hours = selectedSpecialities.Sum(s => s.Duration) * countByTag;
|
||||
|
||||
var stat = new StatsData.TagStats
|
||||
{
|
||||
Count = countByTag,
|
||||
Tag = tag,
|
||||
Hours = hours
|
||||
};
|
||||
stats.StatsByTag.Add(stat);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return stats;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user