Browse Source

简化密钥验证方式为MD5,系统成功启动并登录

v1.4
大石头 9 years ago
parent
commit
4cff5b2f23
  1. 2
      GitCandy/Configuration/UserConfiguration.cs
  2. 36
      GitCandy/Data/MembershipService.cs
  3. 21
      GitCandy/Entity/Entity/用户.Biz.cs
  4. 1
      GitCandy/Entity/NewLife.GitCandy.xml
  5. 5
      GitCandy/GitCandy.csproj
  6. 88
      Sql/Create.MsSql.sql
  7. 88
      Sql/Create.Sqlite.sql

2
GitCandy/Configuration/UserConfiguration.cs

@ -3,7 +3,7 @@ using NewLife.Xml;
namespace GitCandy.Configuration
{
[XmlConfigFile("Config\\UserConfiguration.xml", 15000)]
[XmlConfigFile("Config\\Git.Config", 15000)]
public class UserConfiguration : XmlConfig<UserConfiguration>
{
public UserConfiguration()

36
GitCandy/Data/MembershipService.cs

@ -41,21 +41,13 @@ namespace GitCandy.Data
Nickname = nickname,
Email = email,
PasswordVersion = -1,
Password = "",
Password = password.MD5(),
Description = description,
CreateTime = DateTime.Now,
};
user.Save();
using (var pp = PasswordProviderPool.Take())
{
user.PasswordVersion = pp.Version;
user.Password = pp.Compute(user.ID, name, password);
}
user.Save();
return user;
}
@ -106,24 +98,8 @@ namespace GitCandy.Data
public User Login(string id, string password)
{
var user = User.FindByName(id) ?? User.FindByEmail(id);
if (user != null)
{
using (var pp1 = PasswordProviderPool.Take(user.PasswordVersion))
if (user.Password == pp1.Compute(user.ID, user.Name, password))
{
if (user.PasswordVersion != PasswordProviderPool.LastVersion)
using (var pp2 = PasswordProviderPool.Take())
{
user.Password = pp2.Compute(user.ID, user.Name, password);
user.PasswordVersion = pp2.Version;
user.Logins++;
user.LastLogin = DateTime.Now;
user.LastLoginIP = WebHelper.UserHost;
user.Save();
}
return user;
}
}
if (user != null && user.Login(password)) return user;
return null;
}
@ -132,11 +108,7 @@ namespace GitCandy.Data
var user = User.FindByName(name);
if (user != null)
{
using (var pp = PasswordProviderPool.Take())
{
user.Password = pp.Compute(user.ID, user.Name, newPassword);
user.PasswordVersion = pp.Version;
}
user.Password = newPassword.MD5();
var auths = AuthorizationLog.FindAllByUserID(user.ID);
foreach (var auth in auths)

21
GitCandy/Entity/Entity/用户.Biz.cs

@ -11,6 +11,7 @@ using System.Linq;
using GitCandy.Security;
using NewLife.Data;
using NewLife.Log;
using NewLife.Web;
using XCode;
namespace NewLife.GitCandy.Entity
@ -35,17 +36,11 @@ namespace NewLife.GitCandy.Entity
var entity = new User();
entity.Name = "admin";
entity.Nickname = "管理员";
//entity.Password = "abc";
entity.Password = "admin".MD5();
entity.Enable = true;
entity.IsAdmin = true;
entity.RegisterTime = DateTime.Now;
using (var pp = PasswordProviderPool.Take())
{
entity.PasswordVersion = pp.Version;
entity.Password = pp.Compute(entity.ID, entity.Name, "admin");
}
entity.Insert();
if (XTrace.Debug) XTrace.WriteLine("完成初始化{0}[{1}]数据!", typeof(User).Name, Meta.Table.DataTable.DisplayName);
@ -188,6 +183,18 @@ namespace NewLife.GitCandy.Entity
#endregion
#region 业务
public Boolean Login(String password)
{
var user = this;
if (user.Password != password.MD5()) return false;
user.Logins++;
user.LastLogin = DateTime.Now;
user.LastLoginIP = WebHelper.UserHost;
user.Save();
return true;
}
#endregion
}
}

1
GitCandy/Entity/NewLife.GitCandy.xml

@ -7,7 +7,6 @@
<Column Name="Nickname" DataType="String" Description="显示名。昵称、中文名等" />
<Column Name="Email" DataType="String" Description="邮件" />
<Column Name="Password" DataType="String" Description="密码" />
<Column Name="PasswordVersion" DataType="Int32" Description="密码版本" />
<Column Name="Enable" DataType="Boolean" Description="启用" />
<Column Name="IsAdmin" DataType="Boolean" Description="系统管理员" />
<Column Name="RegisterTime" DataType="DateTime" Description="注册时间" />

5
GitCandy/GitCandy.csproj

@ -400,11 +400,6 @@
<Compile Include="Schedules\Runner.cs" />
<Compile Include="Schedules\Scheduler.cs" />
<Compile Include="Schedules\SingleJob.cs" />
<Compile Include="Security\PasswordProvider.cs" />
<Compile Include="Security\PasswordProviderPool.cs" />
<Compile Include="Security\PasswordProviderV1.cs" />
<Compile Include="Security\PasswordProviderV2.cs" />
<Compile Include="Security\PasswordProviderVersionAttribute.cs" />
<Compile Include="Security\Token.cs" />
<Compile Include="Ssh\Algorithms\CipherInfo.cs" />
<Compile Include="Ssh\Algorithms\CipherModeEx.cs" />

88
Sql/Create.MsSql.sql

@ -1,88 +0,0 @@
CREATE TABLE [Users](
[ID] BigInt PRIMARY KEY IDENTITY(1,1) NOT NULL,
[Name] NVarChar(20) COLLATE Latin1_General_CI_AS NOT NULL,
[Nickname] NVarChar(20) NOT NULL,
[Email] NVarChar(50) NOT NULL,
[PasswordVersion] Int NOT NULL,
[Password] Char(32) NOT NULL,
[Description] NVarChar(500) NOT NULL,
[IsSystemAdministrator] Bit NOT NULL,
[CreationDate] Datetime NOT NULL
);
CREATE TABLE [Teams](
[ID] BigInt PRIMARY KEY IDENTITY(1,1) NOT NULL,
[Name] NVarChar(20) COLLATE Latin1_General_CI_AS NOT NULL,
[Description] NVarChar(500) NOT NULL,
[CreationDate] Datetime NOT NULL
);
CREATE TABLE [Repositories](
[ID] BigInt PRIMARY KEY IDENTITY(1,1) NOT NULL,
[Name] NVarChar(50) COLLATE Latin1_General_CI_AS NOT NULL,
[Description] NVarChar(500) NOT NULL,
[CreationDate] Datetime NOT NULL,
[IsPrivate] Bit NOT NULL,
[AllowAnonymousRead] Bit NOT NULL,
[AllowAnonymousWrite] Bit NOT NULL
);
CREATE TABLE [UserTeamRole](
[UserID] BigInt NOT NULL,
[TeamID] BigInt NOT NULL,
[IsAdministrator] Bit NOT NULL,
Constraint [UNQ_User_Team] Unique ([UserID], [TeamID]),
Foreign Key ([UserID]) References [Users]([ID]),
Foreign Key ([TeamID]) References [Teams]([ID])
);
CREATE TABLE [UserRepositoryRole](
[UserID] BigInt NOT NULL,
[RepositoryID] BigInt NOT NULL,
[AllowRead] Bit NOT NULL,
[AllowWrite] Bit NOT NULL,
[IsOwner] Bit NOT NULL,
Constraint [UNQ_User_Repository] Unique ([UserID], [RepositoryID]),
Foreign Key ([UserID]) References [Users]([ID]),
Foreign Key ([RepositoryID]) References [Repositories]([ID])
);
CREATE TABLE [TeamRepositoryRole](
[TeamID] BigInt NOT NULL,
[RepositoryID] BigInt NOT NULL,
[AllowRead] Bit NOT NULL,
[AllowWrite] Bit NOT NULL,
Constraint [UNQ_Team_Repository] Unique ([TeamID], [RepositoryID]),
Foreign Key ([TeamID]) References [Teams]([ID]),
Foreign Key ([RepositoryID]) References [Repositories]([ID])
);
CREATE TABLE [AuthorizationLog] (
[AuthCode] UniqueIdentifier PRIMARY KEY NOT NULL,
[UserID] BigInt NOT NULL,
[IssueDate] Datetime NOT NULL,
[Expires] Datetime NOT NULL,
[IssueIp] VarChar(40) NOT NULL,
[LastIp] VarChar(40) NOT NULL,
[IsValid] Bit NOT NULL,
Foreign Key ([UserID]) References [Users]([ID])
);
CREATE TABLE [SshKeys] (
[ID] BigInt PRIMARY KEY IDENTITY(1,1) NOT NULL,
[UserID] BigInt NOT NULL,
[KeyType] VarChar(20) NOT NULL,
[Fingerprint] Char(47) NOT NULL,
[PublicKey] VarChar(600) NOT NULL,
[ImportData] Datetime NOT NULL,
[LastUse] Datetime NOT NULL,
Foreign Key ([UserID]) References [Users]([ID])
);
CREATE UNIQUE INDEX [Users_IX_User_Email] ON [Users] ([Name] ASC);
CREATE UNIQUE INDEX [Users_IX_User_Name] ON [Users] ([Email] ASC);
CREATE UNIQUE INDEX [Teams_IX_Team_Name] ON [Teams] ([Name] ASC);
CREATE UNIQUE INDEX [Repositories_IX_Repository_Name] ON [Repositories] ([Name] ASC);
-- Create an administrator user with admin:gitcandy
INSERT INTO [Users] VALUES ('admin', 'admin', 'admin@GitCandy', 1, '6BBBDB60C90AD35F944A934B6E83ABDC', 'System administrator', 1, GetDate())

88
Sql/Create.Sqlite.sql

@ -1,88 +0,0 @@
CREATE TABLE [Users](
[ID] INTEGER PRIMARY KEY AUTOINCREMENT,
[Name] NVarChar(20) NOT NULL COLLATE NOCASE,
[Nickname] NVarChar(20) NOT NULL,
[Email] NVarChar(50) NOT NULL,
[PasswordVersion] Int NOT NULL,
[Password] Char(32) NOT NULL,
[Description] NVarChar(500) NOT NULL,
[IsSystemAdministrator] Bit NOT NULL,
[CreationDate] Datetime NOT NULL
);
CREATE TABLE [Teams](
[ID] INTEGER PRIMARY KEY AUTOINCREMENT,
[Name] NVarChar(20) NOT NULL COLLATE NOCASE,
[Description] NVarChar(500) NOT NULL,
[CreationDate] Datetime NOT NULL
);
CREATE TABLE [Repositories](
[ID] INTEGER PRIMARY KEY AUTOINCREMENT,
[Name] NVarChar(50) NOT NULL COLLATE NOCASE,
[Description] NVarChar(500) NOT NULL,
[CreationDate] Datetime NOT NULL,
[IsPrivate] Bit NOT NULL,
[AllowAnonymousRead] Bit NOT NULL,
[AllowAnonymousWrite] Bit NOT NULL
);
CREATE TABLE [UserTeamRole](
[UserID] INTEGER NOT NULL,
[TeamID] INTEGER NOT NULL,
[IsAdministrator] Bit NOT NULL,
Constraint [UNQ_User_Team] Unique ([UserID], [TeamID]),
Foreign Key ([UserID]) References [Users]([ID]),
Foreign Key ([TeamID]) References [Teams]([ID])
);
CREATE TABLE [UserRepositoryRole](
[UserID] INTEGER NOT NULL,
[RepositoryID] INTEGER NOT NULL,
[AllowRead] Bit NOT NULL,
[AllowWrite] Bit NOT NULL,
[IsOwner] Bit NOT NULL,
Constraint [UNQ_User_Repository] Unique ([UserID], [RepositoryID]),
Foreign Key ([UserID]) References [Users]([ID]),
Foreign Key ([RepositoryID]) References [Repositories]([ID])
);
CREATE TABLE [TeamRepositoryRole](
[TeamID] INTEGER NOT NULL,
[RepositoryID] INTEGER NOT NULL,
[AllowRead] Bit NOT NULL,
[AllowWrite] Bit NOT NULL,
Constraint [UNQ_Team_Repository] Unique ([TeamID], [RepositoryID]),
Foreign Key ([TeamID]) References [Teams]([ID]),
Foreign Key ([RepositoryID]) References [Repositories]([ID])
);
CREATE TABLE [AuthorizationLog] (
[AuthCode] GUID PRIMARY KEY NOT NULL,
[UserID] INTEGER NOT NULL,
[IssueDate] Datetime NOT NULL,
[Expires] Datetime NOT NULL,
[IssueIp] VarChar(40) NOT NULL,
[LastIp] VarChar(40) NOT NULL,
[IsValid] Bit NOT NULL,
Foreign Key ([UserID]) References [Users]([ID])
);
CREATE TABLE [SshKeys] (
[ID] INTEGER PRIMARY KEY NOT NULL,
[UserID] INTEGER NOT NULL,
[KeyType] VarChar(20) NOT NULL,
[Fingerprint] Char(47) NOT NULL,
[PublicKey] VarChar(600) NOT NULL,
[ImportData] Datetime NOT NULL,
[LastUse] Datetime NOT NULL,
Foreign Key ([UserID]) References [Users]([ID])
);
CREATE UNIQUE INDEX [Users_IX_User_Email] ON [Users] ([Name] ASC);
CREATE UNIQUE INDEX [Users_IX_User_Name] ON [Users] ([Email] ASC);
CREATE UNIQUE INDEX [Teams_IX_Team_Name] ON [Teams] ([Name] ASC);
CREATE UNIQUE INDEX [Repositories_IX_Repository_Name] ON [Repositories] ([Name] ASC);
-- Create an administrator user with admin:gitcandy
INSERT INTO [Users] VALUES (NULL, 'admin', 'admin', 'admin@GitCandy', 1, '6BBBDB60C90AD35F944A934B6E83ABDC', 'System administrator', 1, datetime('now', 'localtime'))
Loading…
Cancel
Save