Added login stuff

This commit is contained in:
Eljakim Herrewijnen 2020-02-12 21:47:19 +01:00
parent b67195ae56
commit 72f849ef89
36 changed files with 14366 additions and 12635 deletions

File diff suppressed because it is too large Load Diff

View File

@ -4,6 +4,7 @@
"private": true,
"dependencies": {
"bootstrap": "^3.4.1",
"js-cookie": "^2.2.1",
"react": "^16.0.0",
"react-bootstrap": "^0.31.5",
"react-dom": "^16.0.0",

View File

@ -2,6 +2,9 @@ import React, { Component } from 'react';
import { Route } from 'react-router';
import { Layout } from './components/Layout';
import { Home } from './components/Home';
import { Leden } from './components/Leden';
import { OverIchthus } from './components/OverIchthus';
import { Login } from './components/Login';
export default class App extends Component {
displayName = App.name
@ -10,6 +13,9 @@ export default class App extends Component {
return (
<Layout>
<Route exact path='/' component={Home} />
<Route exact path='/leden' component={Leden} />
<Route exact path='/overichthus' component={OverIchthus} />
<Route exact path='/login' component={Login} />
</Layout>
);
}

View File

@ -6,7 +6,7 @@ export class Home extends Component {
render() {
return (
<div>
<h1>Hello, world!</h1>
<h1>Ichthus</h1>
<p>Welcome to your new single-page application, built with:</p>
</div>
);

View File

@ -7,7 +7,12 @@ export class Layout extends Component {
render() {
return (
<div>
<NavMenu />
<div>
{this.props.children}
</div>
</div>
);
}
}

View File

@ -0,0 +1,29 @@
#LoginBlock{
width: 80%;
/*border: 1px solid black;*/
height: 30%;
margin:10%;
}
#TextBlock {
/*display: inline-block;*/
margin-left: 40%;
width: 100%;
padding: 1%;
}
#LoginTextUsername {
padding: 1%;
margin: 0 auto;
width: 20%;
}
#LoginTextPassword {
padding: 1%;
margin: 0 auto;
width: 20%;
}
#CreateInputFieldText {
width: 100%;
padding: 1%;
margin: 0 auto;
}

View File

@ -0,0 +1,52 @@
import React, { Component } from 'react';
import Cookies from 'js-cookie';
import { Redirect } from 'react-router';
export class Leden extends Component {
constructor(props) {
super(props)
this.WriteData = this.WriteData.bind(this);
this.state = {
username: "",
loggedIn: false,
session: ""
};
}
//This function will run when the site is loading
componentWillMount() {
document.title = "CSFR - Ichthus";
this.getData();
}
componentDidMount() {
let current = this;
}
WriteData() {
this.setState({
})
}
getData() {
var ses = Cookies.get('session');
if (ses !== "" && typeof ses !== 'undefined') {
this.setState({ session: ses, loggedIn: true });
}
}
render() {
if (!this.state.loggedIn) {
return (<Redirect to="/login" />);
}
else {
return (
<div>
<h1>Leden</h1>
<p>Welcome to your new single-page application, built with:</p>
</div>
);
}
}
}

View File

@ -0,0 +1,21 @@
import React, { Component } from 'react';
import { Col, Grid, Row } from 'react-bootstrap';
import { NavMenu } from './NavMenu';
import "./Leden.css";
export class Login extends Component {
render() {
return (
<div id="LoginBlock">
<div id="TextBlock">
<input type="text" id="LoginTextUsername" onChange={this.CreatePersonenChange} placeholder="Gebruikersnaam" />
</div>
<div id="TextBlock">
<input type="password" id="LoginTextPassword" onChange={this.CreatePersonenChange} placeholder="Wachtwoord" />
</div>
</div>
);
}
}

View File

@ -1,7 +1,7 @@
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import { Glyphicon, Nav, Navbar, NavItem } from 'react-bootstrap';
import { LinkContainer } from 'react-router-bootstrap';
import { Home } from './Home';
import Cookies from 'js-cookie';
import './NavMenu.css';
export class NavMenu extends Component {
@ -18,7 +18,7 @@ export class NavMenu extends Component {
{/* Hier menu knoppen op volgorde van Rechts naar Links */}
<div id="MenuBlock">
<Link to={`/leden/`} id="MenuText">
<Link to={'/leden/'} id="MenuText">
Leden
</Link>
</div>
@ -39,7 +39,6 @@ export class NavMenu extends Component {
<Link to={`/overichthus/`} id="MenuText">
Over Ichthus
</Link>
</div>
</div>
);

View File

@ -0,0 +1,15 @@
import React, { Component } from 'react';
export class OverIchthus extends Component {
displayName = OverIchthus.name
render() {
return (
<div>
<h1>Over Ichthus</h1>
<p>Welcome to your new single-page application, built with:</p>
</div>
);
}
}

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Website.Managers
{
public class PasswordManager
{
private int saltLengthLimit = 32;
public byte[] GetSalt()
{
return GetSalt(saltLengthLimit);
}
public byte[] GetSalt(int maximumSaltLength)
{
var salt = new byte[maximumSaltLength];
using (var random = new RNGCryptoServiceProvider())
{
random.GetNonZeroBytes(salt);
}
return salt;
}
public byte[] Hash(string value, byte[] salt)
{
return Hash(Encoding.UTF8.GetBytes(value), salt);
}
public byte[] Hash(byte[] value, byte[] salt)
{
byte[] saltedValue = value.Concat(salt).ToArray();
return new SHA256Managed().ComputeHash(saltedValue);
}
public bool ConfirmPassword(string password, byte[] salt, byte[] passwordhash)
{
byte[] passwordHash = Hash(password, salt);
return passwordhash.SequenceEqual(passwordHash);
}
}
}

View File

@ -0,0 +1,134 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Website.Models;
namespace Website.Managers
{
public class UserManager
{
private readonly string SuperAdminToken = "jklqwufl8uq0qqjwvpioOP*U&Q@(JDJAHUIALDIWaklshfagejhakdfhlae";
private readonly LedenDatabase _db;
private readonly PasswordManager _pw;
public UserManager(LedenDatabase context)
{
_db = context;
_pw = new PasswordManager();
}
public Token LoginUser(string email, string password)
{
var z = _db.members.Where(d => d.Email == email);
if (z.Any())
{
var x = z.First();
if (_pw.ConfirmPassword(password, Array.ConvertAll(x.PasswordSalt, c => (byte)c), Array.ConvertAll(x.PasswordHash, c => (byte)c)))// Array.ConvertAll(x.PasswordSalt, c => (int)c), Array.ConvertAll(x.PasswordHash, c => (int)c)))
{
if (x.Tokens.Any())
{
x.Tokens = x.Tokens.Where(d => d.Expiration > DateTime.UtcNow).ToList();
}
Token token = new Token()
{
Expiration = DateTime.UtcNow.AddDays(7),
ServiceName = "CSFR Ichthus",
Session = Guid.NewGuid(),
Permissions = new TokenPermissions()
{
ViewData = true,
ViewEmail = true,
ViewName = true
}
};
//add token to database before returning it.
x.Tokens.Add(token);
_db.members.Update(x);
_db.SaveChanges();
return token;
}
}
return null;
}
public Token AddUser(API_MemberRegisterData user, string token)
{
if(token != SuperAdminToken) { return null; }
if (_db.members.Where(d => d.Email == user.Email).Any())
{
return null;
}
Member mem = new Member()
{
Firstname = user.Firstname,
Lastname = user.Lastname,
Dispuutsleeftijd = user.Dispuutsleeftijd,
UserID = Guid.NewGuid().ToString().Replace("-", ""),
Email = user.Email,
Geslacht = user.Geslacht
};
byte[] salt = _pw.GetSalt();
mem.PasswordHash = Array.ConvertAll(_pw.Hash(user.Password, salt), c => (int)c);//Encoding.ASCII.GetString(Hash(user.Password, salt));
mem.PasswordSalt = Array.ConvertAll(salt, c => (int)c);//Encoding.ASCII.GetString(salt);
_db.members.Add(mem);
_db.SaveChanges();
return LoginUser(user.Email, user.Password);
}
private bool ValidSession(string userid, string session, string action = "Login")
{
var x = _db.members.Where(d => d.UserID == userid).First();
if (x != null)
{
if (x.Tokens.Any())
{
//get all tokens that are from the future
x.Tokens = x.Tokens.Where(d => d.Expiration > DateTime.UtcNow).ToList();
//update all tokens so the old ones get removed
_db.members.Update(x);
Token token = x.Tokens.Where(d => d.Session.ToString() == session).First();
if (token != null)
{
switch (action)
{
case "Login":
return true;
case "ChangePassword":
return token.Permissions.ChangePassword;
case "ViewData":
return token.Permissions.ViewData;
}
}
}
}
return false;
}
public API_MemberData GetMemberInfo(string userid, string session)
{
if (ValidSession(userid, session, "ViewData"))
{
var member = _db.members.Where(d => d.UserID == userid).First();
API_MemberData retval = new API_MemberData()
{
ContactEmail = member.Email,
Dispuutsleeftijd = member.Dispuutsleeftijd,
Leeftijd = member.Leeftijd,
Geslacht = member.Geslacht,
StudieInfo = member.StudieInfo,
Firstname = member.Firstname,
Lastname = member.Lastname,
Rechten = member.Rechten,
Tokens = member.Tokens,
OudLid = member.OudLid
};
return retval;
}
else
{
return null;
}
}
}
}

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Website.Models
{
public class API_MemberRegisterData
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public int Dispuutsleeftijd { get; set; } = 1;
public string Password { get; set; }
public string Email { get; set; }
public Sex Geslacht { get; set; } = 0;
}
public class API_MemberData
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public List<Token> Tokens { get; set; } = new List<Token>();
public int Leeftijd { get; set; }
public int Dispuutsleeftijd { get; set; }
public string ContactEmail { get; set; }
public StudieInfo StudieInfo { get; set; }
public Sex Geslacht { get; set; }
public Rechten Rechten { get; set; } = 0;
public bool OudLid { get; set; } = false;
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Website.Models
{
public class Commissie
{
public int ID { get; set; }
public string Description { get; set; }
public List<int> Members { get; set; } = new List<int>();
public int CommissiePreases { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace Website.Models
{
public class LedenDatabase : DbContext
{
public LedenDatabase(DbContextOptions<LedenDatabase> options) : base(options) { }
public DbSet<Member> members { get; set; }
}
}

View File

@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Website.Models
{
public class Member
{
public int ID { get; set; }
public string UserID { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public List<Token> Tokens { get; set; } = new List<Token>();
public int Leeftijd { get; set; }
public int Dispuutsleeftijd { get; set; }
public string Opmerking { get; set; }
//Credentials
public string Email { get; set; }
public int[] PasswordHash { get; set; }
public int[] PasswordSalt { get; set; }
//End Credentials
public StudieInfo StudieInfo { get; set; }
public Sex Geslacht { get; set; }
public Rechten Rechten { get; set; } = 0;
public DateTime Verjaardag { get; set; }
public bool OudLid { get; set; } = false;
}
public enum Sex
{
Man = 0,
Vrouw = 1
}
public enum Rechten
{
Gebruiker = 0,
Moderator = 1,
Admin = 2
}
public class StudieInfo
{
public int ID { get; set; }
public string StudieNaam { get; set; }
public string StudieOmschrijving { get; set; }
public int StudieJaar { get; set; }
public string Opmerking { get; set; }
}
public class Token
{
public int ID { get; set; }
public Guid Session { get; set; }
public string ServiceName { get; set; }
public DateTime Expiration { get; set; }
public TokenPermissions Permissions { get; set; }
}
public class TokenPermissions
{
public int ID { get; set; }
public bool ViewName { get; set; } = true; //first and last name
public bool ViewEmail { get; set; } = false; //email address
public bool ViewData { get; set; } = false; //age, dispuut age, address etc.
public bool ChangePassword { get; set; } = false;
}
}

View File

@ -5,6 +5,9 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer;
using Microsoft.EntityFrameworkCore;
using Website.Models;
namespace Website
{
@ -22,6 +25,10 @@ namespace Website
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
//Add database
services.AddDbContext<LedenDatabase>(options => options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
// In production, the React files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
@ -13,6 +13,7 @@
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="2.1.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.1.0" />
</ItemGroup>
<ItemGroup>

View File

@ -4,5 +4,8 @@
"Default": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Port=5432;Database=Website_Ichthus;User Id=postgres;Password=password;"
}
}