Skip to content

Commit a8b3018

Browse files
committed
Added username and password checking to OIDC protocol. Applications are still manually added by EF at the moment, but I'm probably going to trash EF.
1 parent 59b0e60 commit a8b3018

14 files changed

Lines changed: 426 additions & 7 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<PatchControl>
2+
<SchemaPatch filename="SchemaPatch1_001.sql" version="1.001" />
3+
</PatchControl>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
************************
3+
Patch Version: 1.001
4+
Written By: Marakai
5+
Description: Create the user and access rights for the API
6+
Depends On: None
7+
************************
8+
*/
9+
10+
begin transaction CREATE_API_USER
11+
set xact_abort on
12+
set transaction isolation level read committed
13+
14+
if not exists (select * from sys.schemas where name = 'API')
15+
exec('create schema API'); -- Allows it to run in its own batch
16+
17+
if not exists (select * from sys.server_principals where type = 'S' and name='api_user')
18+
create login api_user with password='ChangeMe';
19+
20+
if not exists (select * from sys.database_principals where type = 'S' and name='api_user')
21+
create user api_user for login api_user with default_schema=dbo;
22+
23+
grant EXECUTE on schema :: dbo to api_user;
24+
grant EXECUTE on schema :: API to api_user;
25+
26+
create table dbo.DBPatchVersion
27+
(
28+
PatchId int not null,
29+
PatchName nvarchar(500) not null,
30+
VersionNumber nvarchar(30) not null,
31+
DateApplied datetimeoffset not null,
32+
MessageLog nvarchar(max) null,
33+
constraint PK_DBPatchVersion_PatchId primary key (PatchId),
34+
constraint UQ_DBPatchVersion_VersionNumber unique (VersionNumber)
35+
);
36+
37+
insert into dbo.DBPatchVersion
38+
(
39+
PatchId,
40+
PatchName,
41+
VersionNumber,
42+
DateApplied,
43+
MessageLog
44+
)
45+
values
46+
(
47+
1000,
48+
'Create_Database_Init',
49+
'1.000',
50+
getdate(),
51+
'Initialised database with baseline settings'
52+
);
53+
54+
commit transaction CREATE_API_USER;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
if object_id('API.AuthenticateAndGetUserInfo', 'P') is not null
2+
drop procedure API.AuthenticateAndGetUserInfo;
3+
go
4+
5+
create procedure API.AuthenticateAndGetUserInfo
6+
@Email varchar(50),
7+
@Password varchar(100)
8+
as
9+
10+
select
11+
accountID as AccountId,
12+
email as Email,
13+
firstName as FirstName,
14+
lastName as LastName,
15+
accLevel as AccessLevel,
16+
lastLoggedIn as LastLoggedIn,
17+
creation as CreationDate,
18+
bantime as BanDate,
19+
banlength as BanLengthMinutes,
20+
emailConfirmed as EmailConfirmed,
21+
isactive as IsActive,
22+
totalMinsOnline as TotalMinutesOnline
23+
from accounts
24+
where email = @email and [password] = @password;

src/OpenPerpetuum.Api/Controllers/AuthorisationController.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using OpenPerpetuum.Core.Authorisation.Models;
2020
using OpenPerpetuum.Core.Authorisation.Queries;
2121
using OpenPerpetuum.Core.Foundation.Processing;
22+
using OpenPerpetuum.Core.Foundation.Security;
2223

2324
namespace OpenPerpetuum.Api.Controllers
2425
{
@@ -204,11 +205,20 @@ public async Task<IActionResult> Login([FromForm] LoginViewModel viewModel)
204205
if (!ModelState.IsValid)
205206
return BadRequest();
206207
await HttpContext.SignOutAsync("ServerCookie");
207-
// Check the username and password just testing for now so allow it
208+
209+
UserModel user = QueryProcessor.Process(new GAME_AuthenticateAndGetUserDetailsQuery
210+
{
211+
Email = viewModel.Username,
212+
EncryptedPassword = viewModel.Password.ToLegacyShaString()
213+
});
208214

209215
// Create the identity principal
210216
var userIdentity = new ClaimsIdentity(new List<Claim> { new Claim(ClaimTypes.Name, viewModel.Username), new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString()) }, "ServerCookie");
211217
ClaimsPrincipal principal = new ClaimsPrincipal(userIdentity);
218+
219+
if (user == UserModel.Default)
220+
return Unauthorized();
221+
212222
await HttpContext.SignInAsync(
213223
"ServerCookie",
214224
principal);

src/OpenPerpetuum.Api/appsettings.Development.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010
{
1111
"ProviderName": "API",
1212
"Username": "api_mgmt_user",
13-
"Password": "MyPassword",
13+
"Password": "MyPa55word",
1414
"DefaultDatabase": "api",
15-
"Server": "tcp:10.0.0.95",
15+
"Server": "tcp:192.168.1.159\\OPENPERPETUUM",
1616
"Type": "MicrosoftSql"
1717
},
1818
{
1919
"ProviderName": "Game",
2020
"Username": "api_user",
21-
"Password": "MyOtherPassword",
22-
"DefaultDatabase": "perpetuum",
23-
"Server": "tcp:10.0.0.95",
21+
"Password": "MyOtherPa55word",
22+
"DefaultDatabase": "perpetuumsa",
23+
"Server": "tcp:192.168.1.159\\OPENPERPETUUM",
2424
"Type": "MicrosoftSql"
2525
}
2626
]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("OpenPerpetuum.Core.DataServices")]
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using OpenPerpetuum.Core.Authorisation.Models;
2+
using OpenPerpetuum.Core.DataServices.Database;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Collections.ObjectModel;
6+
7+
namespace OpenPerpetuum.Core.Authorisation.DatabaseResults
8+
{
9+
internal class UserData
10+
{
11+
public int AccountId
12+
{
13+
get;
14+
set;
15+
}
16+
17+
public string Email
18+
{
19+
get;
20+
set;
21+
}
22+
23+
public string FirstName
24+
{
25+
get;
26+
set;
27+
}
28+
29+
public string LastName
30+
{
31+
get;
32+
set;
33+
}
34+
35+
public AccessLevel AccessLevel
36+
{
37+
get;
38+
set;
39+
}
40+
41+
public DateTime LastLoggedIn
42+
{
43+
get;
44+
set;
45+
}
46+
47+
public DateTime CreationDate
48+
{
49+
get;
50+
set;
51+
}
52+
53+
public DateTime BanDate
54+
{
55+
get;
56+
set;
57+
}
58+
59+
public int BanLengthMinutes
60+
{
61+
get;
62+
set;
63+
}
64+
65+
public bool EmailConfirmed
66+
{
67+
get;
68+
set;
69+
}
70+
71+
public bool IsActive
72+
{
73+
get;
74+
set;
75+
}
76+
77+
public int TotalMinutesOnline
78+
{
79+
get;
80+
set;
81+
}
82+
}
83+
84+
internal class UserResult : DatabaseResult
85+
{
86+
public ReadOnlyCollection<UserData> Users => ((ResultSet<UserData>)Results[0])?.Data ?? new List<UserData>().AsReadOnly();
87+
88+
public UserResult()
89+
{
90+
Results.Add(0, new ResultSet<UserData>(0));
91+
}
92+
}
93+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace OpenPerpetuum.Core.Authorisation.Models
2+
{
3+
// Ripped from the OP Server AccessLevel enum
4+
public enum AccessLevel : int // This is uint in the server code but int in the database. Database should be changed but there's a long list...
5+
{
6+
NotDefined = 0,
7+
Normal = 2,
8+
GameAdmin = 6,
9+
ToolAdmin = 14,
10+
Owner = 30,
11+
AllAdmin = ToolAdmin | GameAdmin,
12+
Admin = AllAdmin
13+
}
14+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
3+
namespace OpenPerpetuum.Core.Authorisation.Models
4+
{
5+
public class UserModel
6+
{
7+
public static UserModel Default = new UserModel
8+
{
9+
AccessLevel = AccessLevel.NotDefined,
10+
AccountId = -1,
11+
BanDate = DateTimeOffset.MinValue,
12+
BanExpires = DateTimeOffset.MaxValue,
13+
BanLength = DateTimeOffset.MaxValue.Subtract(DateTimeOffset.MinValue),
14+
CreationDate = DateTimeOffset.MinValue,
15+
Email = string.Empty,
16+
EmailConfirmed = false,
17+
FirstName = string.Empty,
18+
IsActive = false,
19+
LastLoggedIn = DateTimeOffset.MinValue,
20+
LastName = string.Empty,
21+
TotalTimeOnline = TimeSpan.FromTicks(0)
22+
};
23+
24+
public int AccountId
25+
{
26+
get;
27+
set;
28+
}
29+
30+
public string Email
31+
{
32+
get;
33+
set;
34+
}
35+
36+
public string FirstName
37+
{
38+
get;
39+
set;
40+
}
41+
42+
public string LastName
43+
{
44+
get;
45+
set;
46+
}
47+
48+
public AccessLevel AccessLevel
49+
{
50+
get;
51+
set;
52+
}
53+
54+
public DateTimeOffset LastLoggedIn
55+
{
56+
get;
57+
set;
58+
}
59+
60+
public DateTimeOffset CreationDate
61+
{
62+
get;
63+
set;
64+
}
65+
66+
public DateTimeOffset BanDate
67+
{
68+
get;
69+
set;
70+
}
71+
72+
public DateTimeOffset BanExpires
73+
{
74+
get;
75+
set;
76+
}
77+
78+
public TimeSpan BanLength
79+
{
80+
get;
81+
set;
82+
}
83+
84+
public bool EmailConfirmed
85+
{
86+
get;
87+
set;
88+
}
89+
90+
public bool IsActive
91+
{
92+
get;
93+
set;
94+
}
95+
96+
public TimeSpan TotalTimeOnline
97+
{
98+
get;
99+
set;
100+
}
101+
}
102+
}

src/OpenPerpetuum.Core.Authorisation/OpenPerpetuum.Core.Authorisation.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
<Copyright>OpenPerpetuum 2018</Copyright>
6+
<NeutralLanguage>en-GB</NeutralLanguage>
57
</PropertyGroup>
68

79
<ItemGroup>

0 commit comments

Comments
 (0)