mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 01:31:19 +00:00
Update Asp.Net Core 2.1 to 3.1
This commit is contained in:
parent
c896f5fbed
commit
e0fe54d98f
37
MyCore.Framework/Business/ProfileLogic.cs
Normal file
37
MyCore.Framework/Business/ProfileLogic.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using MyCore.Framework.Models;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Framework.Business
|
||||
{
|
||||
public class ProfileLogic
|
||||
{
|
||||
private readonly ILogger<ProfileLogic> _logger;
|
||||
|
||||
public ProfileLogic(ILogger<ProfileLogic> logger)
|
||||
: base()
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public bool Authenticate(string email, string password)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(email))
|
||||
{
|
||||
_logger.LogError($"Authenticate error: No e-mail provided");
|
||||
throw new RequestException(StatusCodes.Status401Unauthorized, "Authentication error");
|
||||
}
|
||||
if (string.IsNullOrEmpty(password))
|
||||
{
|
||||
_logger.LogError($"Authenticate error: No password provided");
|
||||
throw new RequestException(StatusCodes.Status401Unauthorized, "Authentication error");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
26
MyCore.Framework/Models/Exceptions/RequestException.cs
Normal file
26
MyCore.Framework/Models/Exceptions/RequestException.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace MyCore.Framework.Models
|
||||
{
|
||||
[System.Serializable]
|
||||
public class RequestException : System.Exception
|
||||
{
|
||||
public int StatusCode { get; set; }
|
||||
|
||||
public object Payload {get;set;}
|
||||
|
||||
protected RequestException() { }
|
||||
|
||||
public RequestException(int statusCode) : this() { StatusCode = statusCode; }
|
||||
public RequestException(int statusCode, string message) : base(message) { StatusCode = statusCode; }
|
||||
public RequestException(int statusCode, string message, System.Exception inner) : base(message, inner) { StatusCode = statusCode; }
|
||||
protected RequestException(
|
||||
System.Runtime.Serialization.SerializationInfo info,
|
||||
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
|
||||
|
||||
public string GetJson()
|
||||
{
|
||||
return JsonSerializer.Serialize(new { StatusCode, Message, Payload });
|
||||
}
|
||||
}
|
||||
}
|
||||
12
MyCore.Framework/MyCore.Framework.csproj
Normal file
12
MyCore.Framework/MyCore.Framework.csproj
Normal file
@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="LinqKit.Microsoft.EntityFrameworkCore" Version="1.1.21" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.DTO.Common
|
||||
namespace MyCore.Interfaces.DTO
|
||||
{
|
||||
public enum ConnectionStatus
|
||||
{
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.DTO.Common
|
||||
namespace MyCore.Interfaces.DTO
|
||||
{
|
||||
public enum DeviceType // TO BE Continued
|
||||
{
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.DTO.Common
|
||||
namespace MyCore.Interfaces.DTO
|
||||
{
|
||||
public enum MeansOfCommunication
|
||||
{
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.DTO
|
||||
namespace MyCore.Interfaces.DTO
|
||||
{
|
||||
public class RequestParam
|
||||
{
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.DTO.Energy
|
||||
namespace MyCore.Interfaces.DTO
|
||||
{
|
||||
public enum ViewBy
|
||||
{
|
||||
8
MyCore.Interfaces/DTO/LoginDTO.cs
Normal file
8
MyCore.Interfaces/DTO/LoginDTO.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace MyCore.Interfaces.DTO
|
||||
{
|
||||
public class LoginDTO
|
||||
{
|
||||
public string Email { get; set; }
|
||||
public string Password { get; set; }
|
||||
}
|
||||
}
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.DTO.MyControlPanel
|
||||
namespace MyCore.Interfaces.DTO
|
||||
{
|
||||
public class AutomationDTO
|
||||
{
|
||||
@ -1,11 +1,9 @@
|
||||
using MyCore.DTO.Common;
|
||||
using MyCore.Models.MyControlPanel;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.DTO.MyControlPanel
|
||||
namespace MyCore.Interfaces.DTO
|
||||
{
|
||||
public class DeviceSummaryDTO
|
||||
{
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.DTO.MyControlPanel
|
||||
namespace MyCore.Interfaces.DTO
|
||||
{
|
||||
public class GroupDTO
|
||||
{
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.DTO.MyControlPanel
|
||||
namespace MyCore.Interfaces.DTO
|
||||
{
|
||||
public class LocationDTO
|
||||
{
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.DTO.MyControlPanel
|
||||
namespace MyCore.Interfaces.DTO
|
||||
{
|
||||
public class ProviderDTO
|
||||
{
|
||||
45
MyCore.Interfaces/DTO/Odd.cs
Normal file
45
MyCore.Interfaces/DTO/Odd.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Interfaces
|
||||
{
|
||||
public class Odd
|
||||
{
|
||||
public string Sport_nice { get; set; }
|
||||
public List<string> Teams { get; set; }
|
||||
public int Commence_time { get; set; }
|
||||
public string Home_team { get; set; }
|
||||
public List<OddSite> Sites { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public class OddSite
|
||||
{
|
||||
public string Site_key { get; set; }
|
||||
public string Site_nice { get; set; }
|
||||
public int Last_update { get; set; }
|
||||
public OddMatch Odds { get; set; }
|
||||
}
|
||||
|
||||
public class OddMatch
|
||||
{
|
||||
public IEnumerable<double> H2h { get; set; }
|
||||
}
|
||||
|
||||
public class OddH2H
|
||||
{
|
||||
public double HomeOdd { get; set; }
|
||||
public double DrawOdd { get; set; }
|
||||
public double VisitOdd { get; set; }
|
||||
}
|
||||
|
||||
public class OddNice
|
||||
{
|
||||
public List<string> Teams { get; set; }
|
||||
public int Commence_time { get; set; }
|
||||
public string Home_team { get; set; }
|
||||
public OddH2H Odds { get; set; }
|
||||
}
|
||||
}
|
||||
14
MyCore.Interfaces/DTO/SwaggerTokenRequest.cs
Normal file
14
MyCore.Interfaces/DTO/SwaggerTokenRequest.cs
Normal file
@ -0,0 +1,14 @@
|
||||
namespace MyCore.Interfaces.DTO
|
||||
{
|
||||
/// <summary>
|
||||
/// Swagger test client authentication data
|
||||
/// </summary>
|
||||
public class SwaggerTokenRequest
|
||||
{
|
||||
public string grant_type { get; set; }
|
||||
public string username { get; set; }
|
||||
public string password { get; set; }
|
||||
public string client_id { get; set; }
|
||||
public string client_secret { get; set; }
|
||||
}
|
||||
}
|
||||
14
MyCore.Interfaces/DTO/TokenDTO.cs
Normal file
14
MyCore.Interfaces/DTO/TokenDTO.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace MyCore.Interfaces.DTO
|
||||
{
|
||||
public class TokenDTO
|
||||
{
|
||||
public string access_token { get; set; }
|
||||
public string refresh_token { get; set; }
|
||||
public string scope { get; set; }
|
||||
public string token_type { get; set; }
|
||||
public int expires_in { get; set; }
|
||||
public DateTimeOffset expiration { get; set; }
|
||||
}
|
||||
}
|
||||
@ -5,7 +5,7 @@ using System.Threading.Tasks;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
|
||||
namespace MyCore.Models
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class Book
|
||||
{
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public enum LightState
|
||||
{
|
||||
@ -5,7 +5,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models.Common
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class LogDatabase // TODO
|
||||
{
|
||||
@ -1,11 +1,10 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MyCore.DTO.Common;
|
||||
using MyCore.DTO.MyControlPanel;
|
||||
using MyCore.Interfaces.DTO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MyCore.Models.MyControlPanel.Database
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Automation
|
||||
@ -1,14 +1,12 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MyCore.DTO.Common;
|
||||
using MyCore.DTO.MyControlPanel;
|
||||
using MyCore.Models.MyControlPanel.Database;
|
||||
using MyCore.Services.MyControlPanel;
|
||||
using MyCore.Interfaces.DTO;
|
||||
using MyCore.Interfaces.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace MyCore.Models.MyControlPanel.Database
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Group of devices
|
||||
@ -1,10 +1,9 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MyCore.DTO.Common;
|
||||
using MyCore.DTO.MyControlPanel;
|
||||
using MyCore.Interfaces.DTO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
namespace MyCore.Models.MyControlPanel.Database
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Group of devices
|
||||
@ -1,8 +1,8 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MyCore.DTO.MyControlPanel;
|
||||
using MyCore.Interfaces.DTO;
|
||||
|
||||
namespace MyCore.Models.MyControlPanel.Database
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Location of a device (Room name, garden, ..)
|
||||
@ -1,11 +1,10 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MyCore.DTO.Common;
|
||||
using MyCore.DTO.MyControlPanel;
|
||||
using MyCore.Interfaces.DTO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MyCore.Models.MyControlPanel.Database
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Provider of a device (provider of informations) - e.g. : Meross, Arlo, IoThomas, ...
|
||||
@ -2,17 +2,17 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using AspNetCore.Security.Jwt;
|
||||
//using AspNetCore.Security.Jwt;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MyCore.Models.MyControlPanel;
|
||||
using MyCore.Interfaces.Models;
|
||||
|
||||
namespace MyCore.Models.MyControlPanel.Database
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// User Information
|
||||
/// </summary>
|
||||
public class UserInfo : IAuthenticationUser
|
||||
public class UserInfo //: IAuthenticationUser // TODO !
|
||||
{
|
||||
[BsonId]
|
||||
[BsonRepresentation(BsonType.ObjectId)]
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models.Layout
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class PanelSection
|
||||
{
|
||||
8
MyCore.Interfaces/Models/Policy.cs
Normal file
8
MyCore.Interfaces/Models/Policy.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class Policy
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string[] Claims { get; set; }
|
||||
}
|
||||
}
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models.Arlo
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class ArloDevice
|
||||
{
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models.Arlo
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class UserLocation
|
||||
{
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models.Arlo
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class UserMedia
|
||||
{
|
||||
@ -5,7 +5,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models.Energy
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class ElectricityProduction
|
||||
{
|
||||
@ -5,7 +5,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class SmartGardenMessage
|
||||
{
|
||||
@ -5,7 +5,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class SmartPrinterMessage
|
||||
{
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models.Meross
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class DeviceAbilities
|
||||
{
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models.Meross
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class MerossDevice
|
||||
{
|
||||
@ -1,12 +1,12 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MyCore.Models.Providers.Zigbee.Aqara;
|
||||
using MyCore.Interfaces.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models.Aqara
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class AqaraCube : AqaraDevice
|
||||
{
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models.Providers.Zigbee.Aqara
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class AqaraDevice
|
||||
{
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models.Providers.Zigbee.Aqara
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class AqaraSwitch : AqaraDevice
|
||||
{
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models.Ikea
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class LightBulb
|
||||
{
|
||||
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models.Providers.Zigbee.Zigbee2Mqtt
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class Zigbee2MqttDevice
|
||||
{
|
||||
@ -5,7 +5,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class ScreenConfiguration
|
||||
{
|
||||
@ -5,7 +5,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class ScreenDevice
|
||||
{
|
||||
@ -6,7 +6,7 @@ using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace MyCore.Models
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class Widget
|
||||
{
|
||||
@ -4,7 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class WidgetAgenda : Widget
|
||||
{
|
||||
@ -4,7 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class WidgetHourAndDate : Widget
|
||||
{
|
||||
@ -4,7 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class WidgetMessage : Widget
|
||||
{
|
||||
@ -4,7 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class WidgetNews : Widget
|
||||
{
|
||||
@ -4,7 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class WidgetRadio : Widget
|
||||
{
|
||||
@ -4,7 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class WidgetTraffic : Widget
|
||||
{
|
||||
@ -5,7 +5,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Models
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class WidgetWeather : Widget
|
||||
{
|
||||
18
MyCore.Interfaces/Models/TokensSettings.cs
Normal file
18
MyCore.Interfaces/Models/TokensSettings.cs
Normal file
@ -0,0 +1,18 @@
|
||||
namespace MyCore.Interfaces.Models
|
||||
{
|
||||
public class TokensSettings
|
||||
{
|
||||
/// <summary>
|
||||
/// Application secret for tokens generation
|
||||
/// </summary>
|
||||
public string Secret { get; set; }
|
||||
/// <summary>
|
||||
/// Access token expiration in minutes
|
||||
/// </summary>
|
||||
public int AccessTokenExpiration { get; set; } = 30;
|
||||
/// <summary>
|
||||
/// Refresh token expiration in minutes
|
||||
/// </summary>
|
||||
public int RefreshTokenExpiration { get; set; } = 4 * 60;
|
||||
}
|
||||
}
|
||||
11
MyCore.Interfaces/MyCore.Interfaces.csproj
Normal file
11
MyCore.Interfaces/MyCore.Interfaces.csproj
Normal file
@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.8.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
16
MyCore.sln
16
MyCore.sln
@ -5,7 +5,9 @@ VisualStudioVersion = 16.0.29709.97
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MyCore", "MyCore\MyCore.csproj", "{017065F0-FC61-4566-A432-F414FC217AAA}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyCore.Devices", "MyCore.Devices\MyCore.Devices.csproj", "{7B9FE719-FBF7-42D5-BCF5-BDA31B05D66D}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MyCore.Interfaces", "MyCore.Interfaces\MyCore.Interfaces.csproj", "{6FD8EE93-D8BD-43AD-A126-A8ECBF4451E4}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MyCore.Framework", "MyCore.Framework\MyCore.Framework.csproj", "{AA56B2F3-A871-4C99-89FB-038F957399F2}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@ -17,10 +19,14 @@ Global
|
||||
{017065F0-FC61-4566-A432-F414FC217AAA}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{017065F0-FC61-4566-A432-F414FC217AAA}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{017065F0-FC61-4566-A432-F414FC217AAA}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{7B9FE719-FBF7-42D5-BCF5-BDA31B05D66D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{7B9FE719-FBF7-42D5-BCF5-BDA31B05D66D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{7B9FE719-FBF7-42D5-BCF5-BDA31B05D66D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{7B9FE719-FBF7-42D5-BCF5-BDA31B05D66D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{6FD8EE93-D8BD-43AD-A126-A8ECBF4451E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{6FD8EE93-D8BD-43AD-A126-A8ECBF4451E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{6FD8EE93-D8BD-43AD-A126-A8ECBF4451E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{6FD8EE93-D8BD-43AD-A126-A8ECBF4451E4}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{AA56B2F3-A871-4C99-89FB-038F957399F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{AA56B2F3-A871-4C99-89FB-038F957399F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{AA56B2F3-A871-4C99-89FB-038F957399F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{AA56B2F3-A871-4C99-89FB-038F957399F2}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
91
MyCore/Controllers/AuthenticationController.cs
Normal file
91
MyCore/Controllers/AuthenticationController.cs
Normal file
@ -0,0 +1,91 @@
|
||||
using MyCore.Interfaces.DTO;
|
||||
using MyCore.Service.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
using NSwag.Annotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Service.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// Authentication controller
|
||||
/// </summary>
|
||||
[ApiController, Route("api/[controller]")]
|
||||
[Authorize]
|
||||
[OpenApiTag("Authentication", Description = "Authentication management")]
|
||||
public class AuthenticationController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<AuthenticationController> _logger;
|
||||
private readonly TokensService _tokensService;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="logger">Logger</param>
|
||||
/// <param name="tokensService">Tokens service</param>
|
||||
public AuthenticationController(ILogger<AuthenticationController> logger, TokensService tokensService)
|
||||
{
|
||||
_logger = logger;
|
||||
_tokensService = tokensService;
|
||||
}
|
||||
|
||||
private ActionResult<LoginDTO> Authenticate(string email, string password)
|
||||
{
|
||||
try
|
||||
{
|
||||
var token = _tokensService.Authenticate(email.ToLower(), password);
|
||||
|
||||
return Ok(token);
|
||||
}
|
||||
/*catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
_logger?.LogError(ex, $"Authentication error for user '{email}': unauthorized access");
|
||||
return Unauthorized(ex);
|
||||
}*/
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.LogError(ex, $"Authenticate error for user '{email}'");
|
||||
return Problem($"Authenticate error for user '{email}': {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Authenticate with form parameters (used by Swagger test client)
|
||||
/// </summary>
|
||||
/// <param name="tokenRequest">Swagger token request</param>
|
||||
/// <returns>Token descriptor</returns>
|
||||
[AllowAnonymous]
|
||||
[HttpPost("Token")]
|
||||
[Consumes("application/x-www-form-urlencoded")]
|
||||
[SwaggerResponse(HttpStatusCode.OK, typeof(LoginDTO), Description = "Success")]
|
||||
[SwaggerResponse(HttpStatusCode.Unauthorized, typeof(string), Description = "Invalid credentials")]
|
||||
[SwaggerResponse(HttpStatusCode.InternalServerError, typeof(string), Description = "Error")]
|
||||
public ActionResult<LoginDTO> AuthenticateWithForm([FromForm] SwaggerTokenRequest tokenRequest)
|
||||
{
|
||||
return Authenticate(tokenRequest.username, tokenRequest.password);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Authenticate with Json parameters (used by most clients)
|
||||
/// </summary>
|
||||
/// <param name="login">Login DTO</param>
|
||||
/// <returns>Token descriptor</returns>
|
||||
[AllowAnonymous]
|
||||
[HttpPost("Authenticate")]
|
||||
[Consumes("application/json")]
|
||||
[SwaggerResponse(HttpStatusCode.OK, typeof(LoginDTO), Description = "Success")]
|
||||
[SwaggerResponse(HttpStatusCode.Unauthorized, typeof(string), Description = "Invalid credentials")]
|
||||
[SwaggerResponse(HttpStatusCode.InternalServerError, typeof(string), Description = "Error")]
|
||||
public ActionResult<LoginDTO> AuthenticateWithJson([FromBody] LoginDTO login)
|
||||
{
|
||||
return Authenticate(login.Email.ToLower(), login.Password);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,12 +1,12 @@
|
||||
using System.Collections.Generic;
|
||||
using MyCore.Models;
|
||||
using MyCore.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using MyCore.Interfaces.Models;
|
||||
|
||||
namespace MyCore.Controllers
|
||||
{
|
||||
[Authorize(Roles = "Admin")]
|
||||
[Authorize]
|
||||
[Route("api/books")]
|
||||
[ApiController]
|
||||
public class BooksController : ControllerBase
|
||||
|
||||
@ -8,18 +8,15 @@ using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MongoDB.Bson;
|
||||
using MyCore.DTO.Common;
|
||||
using MyCore.DTO.MyControlPanel;
|
||||
using MyCore.Models;
|
||||
using MyCore.Models.MyControlPanel;
|
||||
using MyCore.Models.MyControlPanel.Database;
|
||||
using MyCore.Interfaces.DTO;
|
||||
using MyCore.Interfaces.Models;
|
||||
using MyCore.Services;
|
||||
using MyCore.Services.Devices;
|
||||
using MyCore.Services.MyControlPanel;
|
||||
|
||||
namespace MyCore.Controllers.Devices
|
||||
{
|
||||
[Authorize(Roles = "Admin")]
|
||||
[Authorize] // TODO Add ROLES (Roles = "Admin")
|
||||
[Route("api/device")]
|
||||
[ApiController]
|
||||
public class DeviceController : ControllerBase
|
||||
|
||||
@ -7,15 +7,14 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using MQTTnet;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Server;
|
||||
using MyCore.DTO.Energy;
|
||||
using MyCore.Models;
|
||||
using MyCore.Models.Energy;
|
||||
using MyCore.Interfaces.DTO;
|
||||
using MyCore.Interfaces.Models;
|
||||
using MyCore.Services;
|
||||
using static MyCore.Services.OddService;
|
||||
|
||||
namespace MyCore.Controllers
|
||||
{
|
||||
[Authorize(Roles = "Admin")]
|
||||
[Authorize] // TODO Add ROLES (Roles = "Admin")
|
||||
[Route("api/energy")]
|
||||
[ApiController]
|
||||
public class EnergyController : ControllerBase
|
||||
|
||||
@ -6,12 +6,12 @@ using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MongoDB.Bson;
|
||||
using MyCore.Models;
|
||||
using MyCore.Interfaces.Models;
|
||||
using MyCore.Services;
|
||||
|
||||
namespace MyCore.Controllers
|
||||
{
|
||||
[Authorize(Roles = "Admin")]
|
||||
[Authorize] // TODO Add ROLES (Roles = "Admin")
|
||||
[Route("api/iot")]
|
||||
[ApiController]
|
||||
public class IOTController : ControllerBase
|
||||
|
||||
@ -4,23 +4,33 @@ using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MQTTnet;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Server;
|
||||
using MyCore.DTO;
|
||||
using MyCore.Models;
|
||||
using MyCore.Interfaces;
|
||||
using MyCore.Interfaces.Models;
|
||||
using MyCore.Services;
|
||||
using static MyCore.Services.OddService;
|
||||
|
||||
|
||||
namespace MyCore.Controllers
|
||||
{
|
||||
[Authorize(Roles = "Admin")]
|
||||
[Authorize] // TODO Add ROLES Authorize(Roles = "Admin")
|
||||
[Route("api/odd")]
|
||||
[ApiController]
|
||||
public class OddController : ControllerBase
|
||||
{
|
||||
private OddService oddService = new OddService(OddService.RegionOdd.UK);
|
||||
|
||||
private readonly ILogger<OddController> _logger;
|
||||
|
||||
public OddController(ILogger<OddController> logger) : base()
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get odds for one country and one odd value maximum
|
||||
/// </summary>
|
||||
@ -46,7 +56,7 @@ namespace MyCore.Controllers
|
||||
|
||||
var result = await GetOddsForCountry(id, oddRequest);
|
||||
|
||||
return new OkObjectResult(result);
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@ -60,7 +70,6 @@ namespace MyCore.Controllers
|
||||
/// Get odds for one country and one odd value maximum
|
||||
/// </summary>
|
||||
/// <param name="oddRequest">Odd Maximum value</param>
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType(typeof(List<OddNice>), 200)]
|
||||
[ProducesResponseType(typeof(string), 404)]
|
||||
[ProducesResponseType(typeof(string), 500)]
|
||||
@ -80,7 +89,7 @@ namespace MyCore.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
return new OkObjectResult(oddToSend);
|
||||
return Ok(oddToSend);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
@ -6,11 +6,8 @@ using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MongoDB.Bson;
|
||||
using MyCore.DTO.Common;
|
||||
using MyCore.DTO.MyControlPanel;
|
||||
using MyCore.Models;
|
||||
using MyCore.Models.MyControlPanel;
|
||||
using MyCore.Models.MyControlPanel.Database;
|
||||
using MyCore.Interfaces.DTO;
|
||||
using MyCore.Interfaces.Models;
|
||||
using MyCore.Services;
|
||||
using MyCore.Services.Devices;
|
||||
using MyCore.Services.MyControlPanel;
|
||||
|
||||
@ -6,12 +6,12 @@ using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MongoDB.Bson;
|
||||
using MyCore.Models;
|
||||
using MyCore.Interfaces.Models;
|
||||
using MyCore.Services;
|
||||
|
||||
namespace MyCore.Controllers
|
||||
{
|
||||
[Authorize(Roles = "Admin")]
|
||||
[Authorize] // TODO Add ROLES (Roles = "Admin")
|
||||
[Route("api/device/screen")]
|
||||
[ApiController]
|
||||
public class ScreenDeviceController : ControllerBase
|
||||
|
||||
@ -16,7 +16,7 @@ namespace MyCore.Controllers
|
||||
[ApiController]
|
||||
public class MQTTController : ControllerBase
|
||||
{
|
||||
private string _mqttServer = "192.168.0.8";
|
||||
private string _mqttServer = "192.168.31.140";
|
||||
/// <summary>
|
||||
/// It's a mqtt publish test ! :)
|
||||
/// </summary>
|
||||
|
||||
@ -7,14 +7,13 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using MQTTnet;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Server;
|
||||
using MyCore.Models;
|
||||
using MyCore.Models.Layout;
|
||||
using MyCore.Interfaces.Models;
|
||||
using MyCore.Services;
|
||||
using static MyCore.Services.OddService;
|
||||
|
||||
namespace MyCore.Controllers
|
||||
{
|
||||
[Authorize(Roles = "User")]
|
||||
[Authorize] // TODO Add ROLES (Roles = "User")
|
||||
[Route("api/layout")]
|
||||
[ApiController]
|
||||
public class LayoutController : ControllerBase
|
||||
|
||||
@ -10,9 +10,9 @@ using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using MyCore.DTO;
|
||||
using MyCore.Models;
|
||||
using MyCore.Models.MyControlPanel.Database;
|
||||
using MyCore.Interfaces.DTO;
|
||||
using MyCore.Interfaces.Models;
|
||||
using MyCore.Service.Services;
|
||||
using MyCore.Services;
|
||||
|
||||
namespace MyCore.Controllers
|
||||
@ -22,10 +22,10 @@ namespace MyCore.Controllers
|
||||
[ApiController]
|
||||
public class TokenController : ControllerBase
|
||||
{
|
||||
private TokenService _tokenService;
|
||||
private TokensService _tokenService;
|
||||
private UserDatabaseService _userService;
|
||||
|
||||
public TokenController(TokenService tokenService, UserDatabaseService userService)
|
||||
public TokenController(TokensService tokenService, UserDatabaseService userService)
|
||||
{
|
||||
_tokenService = tokenService;
|
||||
_userService = userService;
|
||||
@ -33,14 +33,14 @@ namespace MyCore.Controllers
|
||||
|
||||
[AllowAnonymous]
|
||||
[HttpPost]
|
||||
public ActionResult<UserInfo> ConnectUser([FromBody] TokenDTO tokenDTO)
|
||||
public ActionResult<UserInfo> ConnectUser([FromBody] LoginDTO loginDTO)
|
||||
{
|
||||
//string test = _TokenService.GenerateSHA256String(password);
|
||||
|
||||
if (IsValidUserAndPasswordCombination(tokenDTO.Email, tokenDTO.Password))
|
||||
if (IsValidUserAndPasswordCombination(loginDTO.Email, loginDTO.Password))
|
||||
{
|
||||
UserInfo user = _userService.GetByEmail(tokenDTO.Email);
|
||||
user.Token = _tokenService.GenerateToken(tokenDTO.Email).ToString();
|
||||
UserInfo user = _userService.GetByEmail(loginDTO.Email);
|
||||
user.Token = _tokenService.GenerateToken(loginDTO.Email).ToString();
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
@ -7,21 +7,21 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using MQTTnet;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Server;
|
||||
using MyCore.Models;
|
||||
using MyCore.Models.MyControlPanel.Database;
|
||||
using MyCore.Interfaces.Models;
|
||||
using MyCore.Service.Services;
|
||||
using MyCore.Services;
|
||||
|
||||
namespace MyCore.Controllers
|
||||
{
|
||||
[Authorize(Roles = "Admin")]
|
||||
[Authorize] // TODO Add ROLES (Roles = "Admin")
|
||||
[Route("api/user")]
|
||||
[ApiController]
|
||||
public class UserController : ControllerBase
|
||||
{
|
||||
private UserDatabaseService _userService;
|
||||
private TokenService _tokenService;
|
||||
private TokensService _tokenService;
|
||||
|
||||
public UserController(UserDatabaseService userService, TokenService tokenService)
|
||||
public UserController(UserDatabaseService userService, TokensService tokenService)
|
||||
{
|
||||
_userService = userService;
|
||||
_tokenService = tokenService;
|
||||
|
||||
@ -7,13 +7,13 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using MQTTnet;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Server;
|
||||
using MyCore.Models;
|
||||
using MyCore.Interfaces.Models;
|
||||
using MyCore.Services;
|
||||
using static MyCore.Services.OddService;
|
||||
|
||||
namespace MyCore.Controllers
|
||||
{
|
||||
[Authorize(Roles = "Admin")]
|
||||
[Authorize] // TODO Add ROLES (Roles = "Admin")
|
||||
[Route("api/test")]
|
||||
[ApiController]
|
||||
public class ValuesController : ControllerBase
|
||||
|
||||
@ -1,13 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.DTO
|
||||
{
|
||||
public class TokenDTO
|
||||
{
|
||||
public string Email { get; set; }
|
||||
public string Password { get; set; }
|
||||
}
|
||||
}
|
||||
@ -1,45 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.DTO
|
||||
{
|
||||
public class Odd
|
||||
{
|
||||
public string Sport_nice;
|
||||
public List<string> Teams;
|
||||
public int Commence_time;
|
||||
public string Home_team;
|
||||
public List<OddSite> Sites;
|
||||
|
||||
}
|
||||
|
||||
public class OddSite
|
||||
{
|
||||
public string Site_key;
|
||||
public string Site_nice;
|
||||
public int Last_update;
|
||||
public OddMatch Odds;
|
||||
}
|
||||
|
||||
public class OddMatch
|
||||
{
|
||||
public IEnumerable<double> H2h;
|
||||
}
|
||||
|
||||
public class OddH2H
|
||||
{
|
||||
public double HomeOdd;
|
||||
public double DrawOdd;
|
||||
public double VisitOdd;
|
||||
}
|
||||
|
||||
public class OddNice
|
||||
{
|
||||
public List<string> Teams;
|
||||
public int Commence_time;
|
||||
public string Home_team;
|
||||
public OddH2H Odds;
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
<UserSecretsId>6d53b0c4-74d6-41aa-8816-2ec3cf42767a</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
@ -9,6 +9,8 @@
|
||||
<PropertyGroup>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<NoWarn>$(NoWarn);1591</NoWarn>
|
||||
<AssemblyName>MyCore.Service</AssemblyName>
|
||||
<RootNamespace>MyCore.Service</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@ -25,9 +27,15 @@
|
||||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.8.0" />
|
||||
<PackageReference Include="MQTTnet" Version="3.0.8" />
|
||||
<PackageReference Include="NSwag.AspNetCore" Version="13.9.2" />
|
||||
<PackageReference Include="ServiceStack.Client" Version="5.8.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
|
||||
<PackageReference Include="YeelightAPI" Version="1.7.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MyCore.Framework\MyCore.Framework.csproj" />
|
||||
<ProjectReference Include="..\MyCore.Interfaces\MyCore.Interfaces.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -6,6 +6,7 @@ using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MyCore
|
||||
@ -14,11 +15,14 @@ namespace MyCore
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
CreateWebHostBuilder(args).Build().Run();
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
}
|
||||
|
||||
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
|
||||
WebHost.CreateDefaultBuilder(args)
|
||||
.UseStartup<Startup>();
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
{
|
||||
webBuilder.UseStartup<Startup>();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:25049",
|
||||
"sslPort": 0
|
||||
"sslPort": 44341
|
||||
}
|
||||
},
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
|
||||
60
MyCore/Security.cs
Normal file
60
MyCore/Security.cs
Normal file
@ -0,0 +1,60 @@
|
||||
using MyCore.Interfaces.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Service
|
||||
{
|
||||
internal static class Security
|
||||
{
|
||||
public const string Scope = "MyCore-api";
|
||||
|
||||
/// <summary>
|
||||
/// Permissions
|
||||
/// </summary>
|
||||
private class Permissions
|
||||
{
|
||||
/// <summary>
|
||||
/// Dpos admin access
|
||||
/// </summary>
|
||||
public const string Admin = "MyCore.admin";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Custom claims types
|
||||
/// </summary>
|
||||
public class ClaimTypes
|
||||
{
|
||||
public const string Permission = "Permission";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Permissions for each type of profile
|
||||
/// </summary>
|
||||
public static readonly Dictionary<Type, string[]> ProfilesConfiguration = new Dictionary<Type, string[]>()
|
||||
{
|
||||
// An admin has access to everything
|
||||
//{ typeof(AdminProfile), new[] { Permissions.Admin} },
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Policies names
|
||||
/// </summary>
|
||||
public class Policies
|
||||
{
|
||||
/// <summary>
|
||||
/// Administration
|
||||
/// </summary>
|
||||
public const string Admin = "MyCore.Administration";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Policies
|
||||
/// </summary>
|
||||
public static readonly Policy[] PoliciesConfiguration = new[]
|
||||
{
|
||||
new Policy() { Name = Policies.Admin, Claims = new[] { Permissions.Admin} }
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -2,7 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using MyCore.Models;
|
||||
using MyCore.Interfaces.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MongoDB.Driver;
|
||||
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MyCore.DTO.Common;
|
||||
using MyCore.DTO.MyControlPanel;
|
||||
using MyCore.Models.MyControlPanel;
|
||||
using MyCore.Models.MyControlPanel.Database;
|
||||
using MyCore.Interfaces.DTO;
|
||||
using MyCore.Interfaces.Models;
|
||||
using MyCore.Services.MyControlPanel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -86,11 +84,11 @@ namespace MyCore.Services.Devices
|
||||
switch (provider.Name)
|
||||
{
|
||||
case "Arlo":
|
||||
List<Models.Arlo.ArloDevice> arloDevices = new ArloService(provider.Username, provider.Password).GetAllDevices();
|
||||
List<ArloDevice> arloDevices = new ArloService(provider.Username, provider.Password).GetAllDevices();
|
||||
createdDevice = CreateArloDevices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, arloDevices, provider);
|
||||
break;
|
||||
case "Meross":
|
||||
List<Models.Meross.MerossDevice> merossDevices = new MerossService(provider.Username, provider.Password).GetMerossDevices();
|
||||
List<MerossDevice> merossDevices = new MerossService(provider.Username, provider.Password).GetMerossDevices();
|
||||
createdDevice = CreateMerossDevices(_DeviceDatabaseService, _ProviderDatabaseService, _LocationDatabaseService, userId, merossDevices, provider);
|
||||
break;
|
||||
case "Yeelight":
|
||||
@ -131,7 +129,7 @@ namespace MyCore.Services.Devices
|
||||
return createdDevice;
|
||||
}
|
||||
|
||||
public static List<DeviceDetailDTO> CreateArloDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, List<Models.Arlo.ArloDevice> arloDevices, Provider provider)
|
||||
public static List<DeviceDetailDTO> CreateArloDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, List<ArloDevice> arloDevices, Provider provider)
|
||||
{
|
||||
List<DeviceDetailDTO> createdArloDevices = new List<DeviceDetailDTO>();
|
||||
|
||||
@ -181,7 +179,7 @@ namespace MyCore.Services.Devices
|
||||
return createdArloDevices;
|
||||
}
|
||||
|
||||
public static List<DeviceDetailDTO> CreateMerossDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, List<Models.Meross.MerossDevice> merossDevices, Provider provider)
|
||||
public static List<DeviceDetailDTO> CreateMerossDevices(DeviceDatabaseService _DeviceDatabaseService, ProviderDatabaseService _ProviderDatabaseService, LocationDatabaseService _LocationDatabaseService, string userId, List<MerossDevice> merossDevices, Provider provider)
|
||||
{
|
||||
List<DeviceDetailDTO> createdMerossDevices = new List<DeviceDetailDTO>();
|
||||
|
||||
|
||||
@ -2,10 +2,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using MyCore.Models;
|
||||
using MyCore.Interfaces.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MongoDB.Driver;
|
||||
using MyCore.Models.Energy;
|
||||
|
||||
namespace MyCore.Services
|
||||
{
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using MyCore.Models;
|
||||
using MyCore.Interfaces.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MongoDB.Driver;
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
using MyCore.DTO;
|
||||
using MyCore.Interfaces;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
|
||||
@ -1,11 +1,7 @@
|
||||
using MQTTnet;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Client.Options;
|
||||
using MyCore.Models;
|
||||
using MyCore.Models.Aqara;
|
||||
using MyCore.Models.Ikea;
|
||||
using MyCore.Models.Providers.Zigbee.Aqara;
|
||||
using MyCore.Models.Providers.Zigbee.Zigbee2Mqtt;
|
||||
using MyCore.Interfaces.Models;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
using EvtSource;
|
||||
using MyCore.Models.Arlo;
|
||||
using MyCore.Interfaces.Models;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using ServiceStack;
|
||||
|
||||
@ -3,7 +3,7 @@ using MQTTnet.Client;
|
||||
using MQTTnet.Client.Options;
|
||||
using MQTTnet.Formatter;
|
||||
using MQTTnet.Implementations;
|
||||
using MyCore.Models.Meross;
|
||||
using MyCore.Interfaces.Models;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
@ -15,7 +15,7 @@ using System.Security.Authentication;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using static MyCore.Models.Meross.DeviceAbilities;
|
||||
using static MyCore.Interfaces.Models.DeviceAbilities;
|
||||
|
||||
namespace MyCore.Services
|
||||
{
|
||||
|
||||
@ -2,11 +2,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using MyCore.Models;
|
||||
using MyCore.Interfaces.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MongoDB.Driver;
|
||||
using MyCore.Models.MyControlPanel;
|
||||
using MyCore.Models.MyControlPanel.Database;
|
||||
|
||||
namespace MyCore.Services.MyControlPanel
|
||||
{
|
||||
|
||||
@ -2,11 +2,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using MyCore.Models;
|
||||
using MyCore.Interfaces.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MongoDB.Driver;
|
||||
using MyCore.Models.MyControlPanel;
|
||||
using MyCore.Models.MyControlPanel.Database;
|
||||
|
||||
namespace MyCore.Services.MyControlPanel
|
||||
{
|
||||
|
||||
@ -2,11 +2,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using MyCore.Models;
|
||||
using MyCore.Interfaces.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MongoDB.Driver;
|
||||
using MyCore.Models.MyControlPanel;
|
||||
using MyCore.Models.MyControlPanel.Database;
|
||||
|
||||
namespace MyCore.Services.MyControlPanel
|
||||
{
|
||||
|
||||
@ -2,11 +2,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using MyCore.Models;
|
||||
using MyCore.Interfaces.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MongoDB.Driver;
|
||||
using MyCore.Models.MyControlPanel;
|
||||
using MyCore.Models.MyControlPanel.Database;
|
||||
|
||||
namespace MyCore.Services.MyControlPanel
|
||||
{
|
||||
|
||||
@ -2,10 +2,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using MyCore.Models;
|
||||
using MyCore.Interfaces.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MongoDB.Driver;
|
||||
using MyCore.Models.MyControlPanel.Database;
|
||||
|
||||
namespace MyCore.Services.MyControlPanel
|
||||
{
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using MyCore.Models;
|
||||
using MyCore.Interfaces.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MongoDB.Driver;
|
||||
|
||||
|
||||
@ -2,10 +2,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using MyCore.Models;
|
||||
using MyCore.Interfaces.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MongoDB.Driver;
|
||||
using MyCore.Models.MyControlPanel.Database;
|
||||
|
||||
namespace MyCore.Services
|
||||
{
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MyCore.DTO.MyControlPanel;
|
||||
using MyCore.Models.MyControlPanel.Database;
|
||||
using MyCore.Interfaces.DTO;
|
||||
using MyCore.Interfaces.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
@ -1,57 +0,0 @@
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyCore.Services
|
||||
{
|
||||
public class TokenService
|
||||
{
|
||||
public object GenerateToken(string username)
|
||||
{
|
||||
var secretKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("%G2YZ=\tgN7fC9M$FXDt#q*a&]Z")); // Put the secret in a file or something
|
||||
|
||||
var claims = new Claim[] {
|
||||
new Claim(ClaimTypes.Name, username),
|
||||
new Claim(JwtRegisteredClaimNames.Email, "john.doe@blinkingcaret.com"),
|
||||
new Claim(ClaimTypes.Role, "Admin")
|
||||
};
|
||||
|
||||
var token = new JwtSecurityToken(
|
||||
issuer: "MyCore App",
|
||||
audience: "Miotecher",
|
||||
claims: claims,
|
||||
notBefore: DateTime.Now,
|
||||
expires: DateTime.Now.AddDays(28),
|
||||
signingCredentials: new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256)
|
||||
);
|
||||
|
||||
string jwtToken = new JwtSecurityTokenHandler().WriteToken(token);
|
||||
|
||||
return jwtToken;
|
||||
}
|
||||
|
||||
public static string GenerateSHA256String(string inputString)
|
||||
{
|
||||
SHA256 sha256 = SHA256Managed.Create();
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(inputString);
|
||||
byte[] hash = sha256.ComputeHash(bytes);
|
||||
return GetStringFromHash(hash);
|
||||
}
|
||||
|
||||
public static string GetStringFromHash(byte[] hash)
|
||||
{
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (int i = 0; i < hash.Length; i++)
|
||||
{
|
||||
result.Append(hash[i].ToString("X2"));
|
||||
}
|
||||
return result.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
141
MyCore/Services/TokensService.cs
Normal file
141
MyCore/Services/TokensService.cs
Normal file
@ -0,0 +1,141 @@
|
||||
using MyCore.Framework.Business;
|
||||
using MyCore.Interfaces.DTO;
|
||||
using MyCore.Interfaces.Models;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace MyCore.Service.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Tokens service
|
||||
/// </summary>
|
||||
public class TokensService
|
||||
{
|
||||
private readonly ILogger<TokensService> _logger;
|
||||
private readonly TokensSettings _tokenSettings;
|
||||
private readonly ProfileLogic _profileLogic;
|
||||
|
||||
private readonly SigningCredentials _signingCredentials;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="logger">Logger</param>
|
||||
/// <param name="tokenSettings">Tokens settings</param>
|
||||
/// <param name="context">Database context</param>
|
||||
/// <param name="profileLogic">Profile logic</param>
|
||||
/// <param name="emailClient">Email client</param>
|
||||
public TokensService(ILogger<TokensService> logger, IOptions<TokensSettings> tokenSettings, ProfileLogic profileLogic)
|
||||
{
|
||||
_logger = logger;
|
||||
_tokenSettings = tokenSettings.Value;
|
||||
_profileLogic = profileLogic;
|
||||
|
||||
var key = Encoding.UTF8.GetBytes(_tokenSettings.Secret);
|
||||
_signingCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Authenticate
|
||||
/// </summary>
|
||||
/// <param name="email">Email</param>
|
||||
/// <param name="password">Password</param>
|
||||
/// <returns>Token DTO in case of success</returns>
|
||||
public TokenDTO Authenticate(string email, string password)
|
||||
{
|
||||
try
|
||||
{
|
||||
var claims = new List<System.Security.Claims.Claim>();
|
||||
var expiration = DateTime.UtcNow.AddMinutes(_tokenSettings.AccessTokenExpiration);
|
||||
|
||||
// Todo nothing good here..
|
||||
var profile = _profileLogic.Authenticate(email, password);
|
||||
|
||||
claims.Add(new Claim(ClaimTypes.Email, email));
|
||||
|
||||
// TODO: add refresh token support
|
||||
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var tokenDescriptor = new SecurityTokenDescriptor()
|
||||
{
|
||||
Subject = new ClaimsIdentity(claims),
|
||||
Expires = expiration,
|
||||
SigningCredentials = _signingCredentials
|
||||
};
|
||||
var token = tokenHandler.CreateToken(tokenDescriptor);
|
||||
|
||||
return new TokenDTO()
|
||||
{
|
||||
access_token = tokenHandler.WriteToken(token),
|
||||
expires_in = _tokenSettings.AccessTokenExpiration * 60,
|
||||
expiration = new DateTimeOffset(token.ValidTo),
|
||||
token_type = "Bearer",
|
||||
scope = Security.Scope
|
||||
};
|
||||
}
|
||||
/*catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
_logger?.LogError(ex, $"Authenticate error for user '{email}': unauthorized access");
|
||||
throw;
|
||||
}*/
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.LogError(ex, $"Authenticate error for user '{email}': {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public object GenerateToken(string username)
|
||||
{
|
||||
var secretKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("%G2YZ=\tgN7fC9M$FXDt#q*a&]Z")); // Put the secret in a file or something
|
||||
|
||||
var claims = new Claim[] {
|
||||
new Claim(ClaimTypes.Name, username),
|
||||
new Claim(JwtRegisteredClaimNames.Email, "john.doe@blinkingcaret.com"),
|
||||
new Claim(ClaimTypes.Role, "Admin")
|
||||
};
|
||||
|
||||
var token = new JwtSecurityToken(
|
||||
issuer: "MyCore App",
|
||||
audience: "Miotecher",
|
||||
claims: claims,
|
||||
notBefore: DateTime.Now,
|
||||
expires: DateTime.Now.AddDays(28),
|
||||
signingCredentials: new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256)
|
||||
);
|
||||
|
||||
string jwtToken = new JwtSecurityTokenHandler().WriteToken(token);
|
||||
|
||||
return jwtToken;
|
||||
}
|
||||
|
||||
public static string GenerateSHA256String(string inputString)
|
||||
{
|
||||
SHA256 sha256 = SHA256Managed.Create();
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(inputString);
|
||||
byte[] hash = sha256.ComputeHash(bytes);
|
||||
return GetStringFromHash(hash);
|
||||
}
|
||||
|
||||
public static string GetStringFromHash(byte[] hash)
|
||||
{
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (int i = 0; i < hash.Length; i++)
|
||||
{
|
||||
result.Append(hash[i].ToString("X2"));
|
||||
}
|
||||
return result.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -4,21 +4,32 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
using AspNetCore.Security.Jwt;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Diagnostics;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.HttpsPolicy;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using MyCore.Models;
|
||||
using MyCore.Framework.Models;
|
||||
using MyCore.Interfaces.Models;
|
||||
using MyCore.Service;
|
||||
using MyCore.Services;
|
||||
using MyCore.Services.MyControlPanel;
|
||||
using Swashbuckle.AspNetCore.Swagger;
|
||||
using NSwag;
|
||||
using NSwag.Generation.AspNetCore;
|
||||
using NSwag.Generation.Processors.Security;
|
||||
using MyCore.Framework.Business;
|
||||
using MyCore.Service.Services;
|
||||
|
||||
namespace MyCore
|
||||
{
|
||||
public class Startup
|
||||
@ -36,10 +47,6 @@ namespace MyCore
|
||||
/*YeelightService yeelighService = new YeelightService();
|
||||
yeelighService.GetDevices();*/
|
||||
|
||||
MQTTService mQTTService = new MQTTService("192.168.31.140", "mqtt", "mqtt");
|
||||
|
||||
mQTTService.GetDevices();
|
||||
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
@ -47,20 +54,10 @@ namespace MyCore
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
// Add the service (test purpose)
|
||||
services.AddScoped<BookService>();
|
||||
services.AddScoped<IoTDeviceService>();
|
||||
services.AddScoped<UserDatabaseService>();
|
||||
services.AddScoped<ProviderDatabaseService>();
|
||||
services.AddScoped<DeviceDatabaseService>();
|
||||
services.AddScoped<LocationDatabaseService>();
|
||||
services.AddScoped<TokenService>();
|
||||
services.AddScoped<ScreenDeviceDatabaseService>();
|
||||
|
||||
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
|
||||
//services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
|
||||
|
||||
// Register the Swagger generator, defining 1 or more Swagger documents
|
||||
services.AddSwaggerGen(c =>
|
||||
/*services.AddSwaggerGen(c =>
|
||||
{
|
||||
c.SwaggerDoc("v1", new Info { Title = "MyCoreApi", Version = "v1" });
|
||||
|
||||
@ -98,40 +95,167 @@ namespace MyCore
|
||||
|
||||
ClockSkew = TimeSpan.FromMinutes(5) //5 minute tolerance for the expiration date
|
||||
};
|
||||
});*/
|
||||
|
||||
|
||||
// Swagger
|
||||
|
||||
services.AddControllers()
|
||||
.AddJsonOptions(options =>
|
||||
{
|
||||
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
||||
});
|
||||
|
||||
services.AddOpenApiDocument(config =>
|
||||
{
|
||||
ConfigureSwagger(config);
|
||||
});
|
||||
|
||||
services.AddCors(o => o.AddPolicy("AllowAll", builder =>
|
||||
{
|
||||
builder.AllowAnyOrigin()
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyHeader();
|
||||
}));
|
||||
|
||||
// Authentication
|
||||
|
||||
var tokensConfiguration = Configuration.GetSection("Tokens");
|
||||
var tokenSettings = tokensConfiguration.Get<TokensSettings>();
|
||||
|
||||
services.Configure<TokensSettings>(tokensConfiguration);
|
||||
|
||||
foreach (var policy in Security.PoliciesConfiguration)
|
||||
services.AddAuthorization(options =>
|
||||
{
|
||||
options.AddPolicy(policy.Name, policyAdmin =>
|
||||
{
|
||||
foreach (var claim in policy.Claims)
|
||||
policyAdmin.RequireClaim(Security.ClaimTypes.Permission, claim);
|
||||
});
|
||||
});
|
||||
|
||||
services
|
||||
.AddAuthentication(x =>
|
||||
{
|
||||
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
x.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
})
|
||||
.AddJwtBearer(x =>
|
||||
{
|
||||
x.RequireHttpsMetadata = false;
|
||||
x.SaveToken = true;
|
||||
x.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenSettings.Secret)),
|
||||
ValidateIssuer = false,
|
||||
ValidateAudience = false,
|
||||
RequireExpirationTime = false,
|
||||
ValidateLifetime = true
|
||||
};
|
||||
});
|
||||
|
||||
services.AddScoped<TokensService>();
|
||||
services.AddScoped(typeof(ProfileLogic));
|
||||
|
||||
// Add the service (test purpose)
|
||||
services.AddScoped<BookService>();
|
||||
services.AddScoped<IoTDeviceService>();
|
||||
services.AddScoped<UserDatabaseService>();
|
||||
services.AddScoped<ProviderDatabaseService>();
|
||||
services.AddScoped<DeviceDatabaseService>();
|
||||
services.AddScoped<LocationDatabaseService>();
|
||||
|
||||
services.AddScoped<ScreenDeviceDatabaseService>();
|
||||
|
||||
|
||||
services.AddScoped<MQTTService>(c => {
|
||||
MQTTService mQTTService = new MQTTService("192.168.31.140", "mqtt", "mqtt");
|
||||
|
||||
mQTTService.GetDevices();
|
||||
return mQTTService;
|
||||
});
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
|
||||
app.UseCors(
|
||||
/*app.UseCors(
|
||||
options => options.WithOrigins("http://localhost:4200").AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials()
|
||||
);
|
||||
|
||||
// Enable middleware to serve generated Swagger as a JSON endpoint.
|
||||
app.UseSwagger();
|
||||
);*/
|
||||
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
else
|
||||
{
|
||||
app.UseHsts();
|
||||
//app.UseDeveloperExceptionPage();
|
||||
app.UseExceptionHandler(HandleError);
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseRouting();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
|
||||
// specifying the Swagger JSON endpoint.
|
||||
app.UseSwaggerUI(c =>
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyCoreApi V1");
|
||||
endpoints.MapControllers();
|
||||
});
|
||||
|
||||
//app.UseHttpsRedirection();
|
||||
app.UseMvc();
|
||||
app.UseOpenApi();
|
||||
app.UseSwaggerUi3();
|
||||
}
|
||||
|
||||
private void ConfigureSwagger(AspNetCoreOpenApiDocumentGeneratorSettings config)
|
||||
{
|
||||
config.AddSecurity("bearer", Enumerable.Empty<string>(), new OpenApiSecurityScheme
|
||||
{
|
||||
Type = OpenApiSecuritySchemeType.OAuth2,
|
||||
Description = "MyCore Authentication",
|
||||
Flow = OpenApiOAuth2Flow.Password,
|
||||
Flows = new OpenApiOAuthFlows()
|
||||
{
|
||||
|
||||
Password = new OpenApiOAuthFlow()
|
||||
{
|
||||
Scopes = new Dictionary<string, string>
|
||||
{
|
||||
{Security.Scope, "MyCore WebAPI"}
|
||||
},
|
||||
TokenUrl = "/api/authentication/Token",
|
||||
AuthorizationUrl = "/authentication/Token",
|
||||
}
|
||||
}
|
||||
});
|
||||
config.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("bearer"));
|
||||
|
||||
config.PostProcess = document =>
|
||||
{
|
||||
document.Info.Title = "MyCore Service";
|
||||
document.Info.Description = "API description";
|
||||
document.Info.Version = "Version Pre-Alpha";
|
||||
};
|
||||
|
||||
config.GenerateEnumMappingDescription = true;
|
||||
}
|
||||
|
||||
private void HandleError(IApplicationBuilder error)
|
||||
{
|
||||
error.Run(async context =>
|
||||
{
|
||||
var exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>();
|
||||
var exception = exceptionHandlerPathFeature?.Error as RequestException;
|
||||
|
||||
if (exception != null)
|
||||
{
|
||||
var json = exception.GetJson();
|
||||
context.Response.ContentType = "application/json";
|
||||
context.Response.StatusCode = exception.StatusCode;
|
||||
await context.Response.WriteAsync(json);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,10 +5,17 @@
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Warning"
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"Tokens": {
|
||||
"Secret": "%G2YZ=\tgN7fC9M$FXDt#q*a&]Z",
|
||||
"AccessTokenExpiration": 86400,
|
||||
"RefreshTokenExpiration": 518400
|
||||
},
|
||||
"SecuritySettings": {
|
||||
"Secret": "azertyuiopqsdfgh",
|
||||
"Issuer": "MyCore",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user