Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.

Commit 1fa6409

Browse files
Merge pull request #13 from Standaard-boos/development
Sprint 1
2 parents eda846c + 3968d1c commit 1fa6409

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+4002
-17
lines changed

ConsoleTest/ConsoleTest.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\Gateway\Gateway.csproj" />
10+
<ProjectReference Include="..\Model\Model.csproj" />
11+
<ProjectReference Include="..\ViewModel\ViewModel.csproj" />
12+
<ProjectReference Include="..\View\View.csproj" />
13+
</ItemGroup>
14+
15+
</Project>

ConsoleTest/Program.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using Gateway;
3+
4+
namespace ConsoleTest
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
SetupSQLConnection connection = new SetupSQLConnection();
11+
Console.WriteLine($"MySQL version : {connection.Connection.ServerVersion}");
12+
13+
}
14+
}
15+
}

Gateway.Test/Gateway.Test.csproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
11+
<PackageReference Include="NUnit" Version="3.13.1" />
12+
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
13+
<PackageReference Include="coverlet.collector" Version="3.0.2" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\Gateway\Gateway.csproj" />
18+
</ItemGroup>
19+
20+
</Project>

Gateway.Test/UnitTest1.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using NUnit.Framework;
2+
3+
namespace Gateway.Test
4+
{
5+
public class Tests
6+
{
7+
[SetUp]
8+
public void Setup()
9+
{
10+
}
11+
12+
[Test]
13+
public void Test1()
14+
{
15+
Assert.Pass();
16+
}
17+
}
18+
}

Gateway/DataAccess.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using Dapper;
2+
using System.Data;
3+
using Model;
4+
using System;
5+
6+
namespace Gateway
7+
{
8+
public static class DataAccess
9+
{
10+
#region Methods
11+
// These methods might be able to turn into generic methods, but that might be a bit too difficult or not useful enough.
12+
13+
/// <summary>
14+
/// Searches the database for an account.
15+
/// </summary>
16+
/// <param name="account">The account to be searched for.</param>
17+
/// <returns>True if the account exists in the database and false if it does not.</returns>
18+
public static bool CheckIfAccountExists(Account account)
19+
{
20+
try
21+
{
22+
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(FiddleHelper.GetConnectionStringSql("StudentMatcherDB")))
23+
{
24+
connection.QuerySingle("SELECT * FROM Account WHERE Email = @Email", account);
25+
}
26+
}
27+
catch (InvalidOperationException)
28+
{
29+
return false;
30+
}
31+
return true;
32+
}
33+
34+
/// <summary>
35+
/// Searches for the hashed and salted password for an existing account in a database.
36+
/// </summary>
37+
/// <param name="account">The account to be searched for.</param>
38+
/// <returns>The hashed and salted password.</returns>
39+
public static string GetHashedPassswordFromAccount(Account account)
40+
{
41+
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(FiddleHelper.GetConnectionStringSql("StudentMatcherDB")))
42+
{
43+
return connection.QuerySingle<string>("SELECT Password FROM Account WHERE Email = @Email", account);
44+
}
45+
}
46+
47+
/// <summary>
48+
/// Creates an account in the database.
49+
/// </summary>
50+
/// <param name="account">The account to be added to the database.</param>
51+
/// <param name="verificationCode">The verification code generated by the program.</param>
52+
public static void CreateAccount(Account account, string verificationCode)
53+
{
54+
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(FiddleHelper.GetConnectionStringSql("StudentMatcherDB")))
55+
{
56+
connection.Execute($"INSERT INTO Account(Email, Password, VerificationCode) VALUES (@Email, @Password, {verificationCode})", account); // Better is to use a StoredProcedure later on
57+
}
58+
}
59+
60+
/// <summary>
61+
/// Checks if the given verification code matches the verification code in the database for the account. Also sets the verified column to true (1).
62+
/// </summary>
63+
/// <param name="verificationCode">The verification code that needs to be checked with the database.</param>
64+
/// <param name="account">The account the verification code needs to be checked for.</param>
65+
/// <returns>True if the verification code matches, false if not.</returns>
66+
public static bool CheckIfVerificationCodeMatches(string verificationCode, Account account)
67+
{
68+
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(FiddleHelper.GetConnectionStringSql("StudentMatcherDB")))
69+
{
70+
string verificationCodeInDatabase = connection.QuerySingle<string>("SELECT VerificationCode FROM Account WHERE Email = @Email", account);
71+
if (verificationCodeInDatabase == verificationCode)
72+
{
73+
connection.Execute($"UPDATE Account SET AccountVerified = 1 WHERE Email = @Email", account);
74+
return true;
75+
}
76+
return false;
77+
}
78+
}
79+
#endregion
80+
}
81+
}

Gateway/EmailService.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using MailKit.Net.Smtp;
2+
using MimeKit;
3+
using Model;
4+
5+
namespace Gateway
6+
{
7+
public static class EmailService
8+
{
9+
/// <summary>
10+
/// Sends a verification mail to an account.
11+
/// </summary>
12+
/// <param name="account">The account the mail needs to be send to.</param>
13+
/// <param name="verificationCode">The verification code that needs to be in the mail.</param>
14+
public static void SendVerificationMail(Account account, string verificationCode)
15+
{
16+
MimeMessage mailMessage = new MimeMessage();
17+
mailMessage.From.Add(new MailboxAddress("Stugether", "[email protected]"));
18+
mailMessage.To.Add(new MailboxAddress($"Stugether gebruiker", account.Email));
19+
mailMessage.Subject = "Welkom bij Stugether! Dit is je verificatiecode.";
20+
mailMessage.Body = new TextPart("plain")
21+
{
22+
Text = $"Welkom bij het Stugether platform! We hopen dat je veel plezier gaat hebben met de applicatie. " +
23+
$"Vul je verificatiecode in bij het registreren. Je verificatiecode is: {verificationCode}"
24+
};
25+
26+
using (SmtpClient smtpClient = new SmtpClient())
27+
{
28+
smtpClient.Connect("stugether.wafoe.nl", 465, true);
29+
smtpClient.Authenticate("[email protected]", @"Z:`bX_H&FmJ)QX+AmxbkV\5;72&,N9~,");
30+
smtpClient.Send(mailMessage);
31+
smtpClient.Disconnect(true);
32+
}
33+
}
34+
}
35+
}

Gateway/Gateway.csproj

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<None Remove="NoPush\DatabaseInfo.txt" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<Content Include="NoPush\DatabaseInfo.txt">
13+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
14+
</Content>
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<Resource Include="NoPush\DatabaseInfo.txt">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</Resource>
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<PackageReference Include="BCrypt.Net-Next" Version="4.0.2" />
25+
<PackageReference Include="Dapper" Version="2.0.123" />
26+
<PackageReference Include="MailKit" Version="2.15.0" />
27+
<PackageReference Include="SSH.NET" Version="2020.0.1" />
28+
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
29+
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
30+
</ItemGroup>
31+
32+
<ItemGroup>
33+
<ProjectReference Include="..\Model\Model.csproj" />
34+
</ItemGroup>
35+
36+
</Project>

Gateway/Helpers/FiddleHelper.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Configuration;
2+
3+
namespace Gateway
4+
{
5+
public static class FiddleHelper
6+
{
7+
/// <summary>
8+
/// Assigns the right connection name to the connection string.
9+
/// </summary>
10+
/// <param name="name">The name of the connection.</param>
11+
/// <returns>A string of the ConfigurationManager.</returns>
12+
public static string GetConnectionStringSql(string name)
13+
{
14+
return ConfigurationManager.ConnectionStrings[name].ConnectionString;
15+
}
16+
}
17+
}

Gateway/NoPush/DatabaseInfo.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
145.44.233.153
2+
student
3+
Spidb1@
4+
1433
5+
sa
6+
Spidb1@!#
7+
StudentMatcher

Gateway/SSHConnection.cs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using Renci.SshNet;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
6+
namespace Gateway
7+
{
8+
public static class SSHConnection // Source: https://mysqlconnector.net/tutorials/connect-ssh/
9+
{
10+
#region Fields
11+
private static SshClient _sshConnection;
12+
private static uint _sshPort;
13+
#endregion
14+
15+
#region Properties
16+
public static SshClient SshConnection
17+
{
18+
get { return _sshConnection; }
19+
}
20+
public static uint SshPort
21+
{
22+
get { return _sshPort; }
23+
}
24+
#endregion
25+
26+
#region Methods
27+
/// <summary>
28+
/// Sets up a SSH connection to a remote SSH server. Needs: dotnet add package SSH.NET
29+
/// </summary>
30+
/// <param name="sshHostName">Hostname of the SSH server to connect to.</param>
31+
/// <param name="sshUserName">Username of the SSH server to connect to.</param>
32+
/// <param name="sshPassword">Password of the SSH user on the SSH server.</param>
33+
/// <param name="sshKeyFile">Optional certificate file instead of a password.</param>
34+
/// <param name="sshPassPhrase">Optional pass phrase instead of a password.</param>
35+
/// <param name="sshPort">Optional change of the SSH port on the remote server.</param>
36+
/// <param name="databaseServer">Optional change of the local database server address.</param>
37+
/// <param name="databasePort">Optional change of the IP address of the remote SSH server.</param>
38+
/// <returns>
39+
/// A tuple of the SshClient and the randomized local port.
40+
/// </returns>
41+
public static Tuple<SshClient, uint> ConnectSsh(string sshHostName, string sshUserName, string sshPassword = null,
42+
string sshKeyFile = null, string sshPassPhrase = null, int sshPort = 22, string databaseServer = "localhost", int databasePort = 1433)
43+
{
44+
// Check arguments
45+
if (string.IsNullOrEmpty(sshHostName))
46+
throw new ArgumentException($"{nameof(sshHostName)} must be specified.", nameof(sshHostName));
47+
if (string.IsNullOrEmpty(sshHostName))
48+
throw new ArgumentException($"{nameof(sshUserName)} must be specified.", nameof(sshUserName));
49+
if (string.IsNullOrEmpty(sshPassword) && string.IsNullOrEmpty(sshKeyFile))
50+
throw new ArgumentException($"One of {nameof(sshPassword)} and {nameof(sshKeyFile)} must be specified.");
51+
if (string.IsNullOrEmpty(databaseServer))
52+
throw new ArgumentException($"{nameof(databaseServer)} must be specified.", nameof(databaseServer));
53+
54+
// Define the authentication methods to use (in order)
55+
List<AuthenticationMethod> authenticationMethods = new List<AuthenticationMethod>();
56+
if (!string.IsNullOrEmpty(sshKeyFile))
57+
{
58+
authenticationMethods.Add(new PrivateKeyAuthenticationMethod(sshUserName,
59+
new PrivateKeyFile(sshKeyFile, string.IsNullOrEmpty(sshPassPhrase) ? null : sshPassPhrase)));
60+
}
61+
if (!string.IsNullOrEmpty(sshPassword))
62+
{
63+
authenticationMethods.Add(new PasswordAuthenticationMethod(sshUserName, sshPassword));
64+
}
65+
66+
// Connect to the SSH server
67+
SshClient sshClient = new SshClient(new ConnectionInfo(sshHostName, sshPort, sshUserName, authenticationMethods.ToArray()));
68+
sshClient.Connect();
69+
70+
// Forward a local port to the database server and port, using the SSH server
71+
//ForwardedPortLocal forwardedPort = new ForwardedPortLocal("127.0.0.1", databaseServer, (uint)databasePort); // Uses a random port
72+
ForwardedPortLocal forwardedPort = new ForwardedPortLocal("127.0.0.1", (uint)databasePort, databaseServer, (uint)databasePort); // Makes port same as database port
73+
sshClient.AddForwardedPort(forwardedPort);
74+
forwardedPort.Start();
75+
76+
return Tuple.Create(sshClient, forwardedPort.BoundPort);
77+
}
78+
79+
/// <summary>
80+
/// Sets up an SSL connection and binds the SshClient and SshPort to properties.
81+
/// </summary>
82+
public static void InitializeSsh()
83+
{
84+
string[] databaseInfo = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"NoPush\DatabaseInfo.txt"));
85+
Tuple<SshClient, uint> sshConnection = SSHConnection.ConnectSsh(databaseInfo[0], databaseInfo[1], databaseInfo[2]);
86+
_sshConnection = sshConnection.Item1;
87+
_sshPort = sshConnection.Item2;
88+
}
89+
#endregion
90+
}
91+
}

0 commit comments

Comments
 (0)