2016-11-18 09:28:39 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Emby.Server.Implementations.Data;
|
|
|
|
|
using MediaBrowser.Controller;
|
|
|
|
|
using MediaBrowser.Controller.Security;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Querying;
|
|
|
|
|
using SQLitePCL.pretty;
|
2017-08-09 19:56:38 +00:00
|
|
|
|
using MediaBrowser.Model.Extensions;
|
2016-11-18 09:28:39 +00:00
|
|
|
|
|
|
|
|
|
namespace Emby.Server.Implementations.Security
|
|
|
|
|
{
|
|
|
|
|
public class AuthenticationRepository : BaseSqliteRepository, IAuthenticationRepository
|
|
|
|
|
{
|
|
|
|
|
private readonly IServerApplicationPaths _appPaths;
|
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
|
|
|
|
|
public AuthenticationRepository(ILogger logger, IServerApplicationPaths appPaths)
|
|
|
|
|
: base(logger)
|
|
|
|
|
{
|
|
|
|
|
_appPaths = appPaths;
|
|
|
|
|
DbFilePath = Path.Combine(appPaths.DataPath, "authentication.db");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Initialize()
|
|
|
|
|
{
|
|
|
|
|
using (var connection = CreateConnection())
|
|
|
|
|
{
|
2016-11-29 19:12:37 +00:00
|
|
|
|
RunDefaultInitialization(connection);
|
2016-11-21 03:52:58 +00:00
|
|
|
|
|
2016-11-18 09:28:39 +00:00
|
|
|
|
string[] queries = {
|
|
|
|
|
|
2016-11-21 03:52:58 +00:00
|
|
|
|
"create table if not exists AccessTokens (Id GUID PRIMARY KEY, AccessToken TEXT NOT NULL, DeviceId TEXT, AppName TEXT, AppVersion TEXT, DeviceName TEXT, UserId TEXT, IsActive BIT, DateCreated DATETIME NOT NULL, DateRevoked DATETIME)",
|
2016-11-18 09:28:39 +00:00
|
|
|
|
"create index if not exists idx_AccessTokens on AccessTokens(Id)"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
connection.RunQueries(queries);
|
|
|
|
|
|
|
|
|
|
connection.RunInTransaction(db =>
|
|
|
|
|
{
|
2016-11-19 05:52:49 +00:00
|
|
|
|
var existingColumnNames = GetColumnNames(db, "AccessTokens");
|
|
|
|
|
|
|
|
|
|
AddColumn(db, "AccessTokens", "AppVersion", "TEXT", existingColumnNames);
|
2016-11-28 19:26:48 +00:00
|
|
|
|
|
|
|
|
|
}, TransactionMode);
|
2016-11-18 09:28:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task Create(AuthenticationInfo info, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
info.Id = Guid.NewGuid().ToString("N");
|
|
|
|
|
|
|
|
|
|
return Update(info, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Update(AuthenticationInfo info, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (info == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("info");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2016-12-11 05:12:00 +00:00
|
|
|
|
using (WriteLock.Write())
|
2016-11-18 09:28:39 +00:00
|
|
|
|
{
|
2016-12-11 05:12:00 +00:00
|
|
|
|
using (var connection = CreateConnection())
|
2016-11-18 09:28:39 +00:00
|
|
|
|
{
|
|
|
|
|
connection.RunInTransaction(db =>
|
|
|
|
|
{
|
2016-11-20 05:59:36 +00:00
|
|
|
|
using (var statement = db.PrepareStatement("replace into AccessTokens (Id, AccessToken, DeviceId, AppName, AppVersion, DeviceName, UserId, IsActive, DateCreated, DateRevoked) values (@Id, @AccessToken, @DeviceId, @AppName, @AppVersion, @DeviceName, @UserId, @IsActive, @DateCreated, @DateRevoked)"))
|
|
|
|
|
{
|
2017-05-07 20:02:32 +00:00
|
|
|
|
statement.TryBind("@Id", info.Id.ToGuidBlob());
|
2016-11-20 07:10:07 +00:00
|
|
|
|
statement.TryBind("@AccessToken", info.AccessToken);
|
2016-11-20 05:59:36 +00:00
|
|
|
|
|
2016-11-20 07:10:07 +00:00
|
|
|
|
statement.TryBind("@DeviceId", info.DeviceId);
|
|
|
|
|
statement.TryBind("@AppName", info.AppName);
|
|
|
|
|
statement.TryBind("@AppVersion", info.AppVersion);
|
|
|
|
|
statement.TryBind("@DeviceName", info.DeviceName);
|
|
|
|
|
statement.TryBind("@UserId", info.UserId);
|
|
|
|
|
statement.TryBind("@IsActive", info.IsActive);
|
|
|
|
|
statement.TryBind("@DateCreated", info.DateCreated.ToDateTimeParamValue());
|
2016-11-20 05:59:36 +00:00
|
|
|
|
|
|
|
|
|
if (info.DateRevoked.HasValue)
|
|
|
|
|
{
|
2016-11-20 07:10:07 +00:00
|
|
|
|
statement.TryBind("@DateRevoked", info.DateRevoked.Value.ToDateTimeParamValue());
|
2016-11-20 05:59:36 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-11-20 07:10:07 +00:00
|
|
|
|
statement.TryBindNull("@DateRevoked");
|
2016-11-20 05:59:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
statement.MoveNext();
|
|
|
|
|
}
|
2016-11-28 19:26:48 +00:00
|
|
|
|
|
|
|
|
|
}, TransactionMode);
|
2016-11-18 09:28:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private const string BaseSelectText = "select Id, AccessToken, DeviceId, AppName, AppVersion, DeviceName, UserId, IsActive, DateCreated, DateRevoked from AccessTokens";
|
|
|
|
|
|
2016-11-20 05:59:36 +00:00
|
|
|
|
private void BindAuthenticationQueryParams(AuthenticationInfoQuery query, IStatement statement)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.AccessToken))
|
|
|
|
|
{
|
2016-11-20 07:10:07 +00:00
|
|
|
|
statement.TryBind("@AccessToken", query.AccessToken);
|
2016-11-20 05:59:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.UserId))
|
|
|
|
|
{
|
2016-11-20 07:10:07 +00:00
|
|
|
|
statement.TryBind("@UserId", query.UserId);
|
2016-11-20 05:59:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.DeviceId))
|
|
|
|
|
{
|
2016-11-20 07:10:07 +00:00
|
|
|
|
statement.TryBind("@DeviceId", query.DeviceId);
|
2016-11-20 05:59:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query.IsActive.HasValue)
|
|
|
|
|
{
|
2016-11-20 07:10:07 +00:00
|
|
|
|
statement.TryBind("@IsActive", query.IsActive.Value);
|
2016-11-20 05:59:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-18 09:28:39 +00:00
|
|
|
|
public QueryResult<AuthenticationInfo> Get(AuthenticationInfoQuery query)
|
|
|
|
|
{
|
|
|
|
|
if (query == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("query");
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-29 19:12:37 +00:00
|
|
|
|
var commandText = BaseSelectText;
|
2016-11-18 09:28:39 +00:00
|
|
|
|
|
2016-11-29 19:12:37 +00:00
|
|
|
|
var whereClauses = new List<string>();
|
2016-11-18 09:28:39 +00:00
|
|
|
|
|
2016-11-29 19:12:37 +00:00
|
|
|
|
var startIndex = query.StartIndex ?? 0;
|
2016-11-18 09:28:39 +00:00
|
|
|
|
|
2016-11-29 19:12:37 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.AccessToken))
|
|
|
|
|
{
|
|
|
|
|
whereClauses.Add("AccessToken=@AccessToken");
|
|
|
|
|
}
|
2016-11-18 09:28:39 +00:00
|
|
|
|
|
2016-11-29 19:12:37 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.UserId))
|
|
|
|
|
{
|
|
|
|
|
whereClauses.Add("UserId=@UserId");
|
|
|
|
|
}
|
2016-11-25 17:36:00 +00:00
|
|
|
|
|
2016-11-29 19:12:37 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.DeviceId))
|
|
|
|
|
{
|
|
|
|
|
whereClauses.Add("DeviceId=@DeviceId");
|
|
|
|
|
}
|
2016-11-18 09:28:39 +00:00
|
|
|
|
|
2016-11-29 19:12:37 +00:00
|
|
|
|
if (query.IsActive.HasValue)
|
|
|
|
|
{
|
|
|
|
|
whereClauses.Add("IsActive=@IsActive");
|
|
|
|
|
}
|
2016-11-18 09:28:39 +00:00
|
|
|
|
|
2016-11-29 19:12:37 +00:00
|
|
|
|
if (query.HasUser.HasValue)
|
|
|
|
|
{
|
|
|
|
|
if (query.HasUser.Value)
|
|
|
|
|
{
|
|
|
|
|
whereClauses.Add("UserId not null");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
whereClauses.Add("UserId is null");
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-25 17:36:00 +00:00
|
|
|
|
|
2016-11-29 19:12:37 +00:00
|
|
|
|
var whereTextWithoutPaging = whereClauses.Count == 0 ?
|
|
|
|
|
string.Empty :
|
2017-08-09 19:56:38 +00:00
|
|
|
|
" where " + string.Join(" AND ", whereClauses.ToArray(whereClauses.Count));
|
2016-11-18 09:28:39 +00:00
|
|
|
|
|
2016-11-29 19:12:37 +00:00
|
|
|
|
if (startIndex > 0)
|
|
|
|
|
{
|
|
|
|
|
var pagingWhereText = whereClauses.Count == 0 ?
|
|
|
|
|
string.Empty :
|
2017-08-09 19:56:38 +00:00
|
|
|
|
" where " + string.Join(" AND ", whereClauses.ToArray(whereClauses.Count));
|
2016-11-18 09:28:39 +00:00
|
|
|
|
|
2016-11-29 19:12:37 +00:00
|
|
|
|
whereClauses.Add(string.Format("Id NOT IN (SELECT Id FROM AccessTokens {0} ORDER BY DateCreated LIMIT {1})",
|
|
|
|
|
pagingWhereText,
|
|
|
|
|
startIndex.ToString(_usCulture)));
|
|
|
|
|
}
|
2016-11-18 09:28:39 +00:00
|
|
|
|
|
2016-11-29 19:12:37 +00:00
|
|
|
|
var whereText = whereClauses.Count == 0 ?
|
|
|
|
|
string.Empty :
|
2017-08-09 19:56:38 +00:00
|
|
|
|
" where " + string.Join(" AND ", whereClauses.ToArray(whereClauses.Count));
|
2016-11-18 09:28:39 +00:00
|
|
|
|
|
2016-11-29 19:12:37 +00:00
|
|
|
|
commandText += whereText;
|
2016-11-18 09:28:39 +00:00
|
|
|
|
|
2016-11-29 19:12:37 +00:00
|
|
|
|
commandText += " ORDER BY DateCreated";
|
2016-11-18 09:28:39 +00:00
|
|
|
|
|
2016-11-29 19:12:37 +00:00
|
|
|
|
if (query.Limit.HasValue)
|
|
|
|
|
{
|
|
|
|
|
commandText += " LIMIT " + query.Limit.Value.ToString(_usCulture);
|
|
|
|
|
}
|
2016-11-25 17:36:00 +00:00
|
|
|
|
|
2016-11-29 19:12:37 +00:00
|
|
|
|
var list = new List<AuthenticationInfo>();
|
2016-11-18 09:28:39 +00:00
|
|
|
|
|
2016-12-11 05:12:00 +00:00
|
|
|
|
using (WriteLock.Read())
|
2016-11-29 19:12:37 +00:00
|
|
|
|
{
|
2016-12-11 05:12:00 +00:00
|
|
|
|
using (var connection = CreateConnection(true))
|
2016-11-29 19:12:37 +00:00
|
|
|
|
{
|
2016-12-13 07:36:30 +00:00
|
|
|
|
return connection.RunInTransaction(db =>
|
2016-11-20 05:59:36 +00:00
|
|
|
|
{
|
2016-12-13 07:36:30 +00:00
|
|
|
|
var result = new QueryResult<AuthenticationInfo>();
|
|
|
|
|
|
2016-12-12 05:49:19 +00:00
|
|
|
|
var statementTexts = new List<string>();
|
|
|
|
|
statementTexts.Add(commandText);
|
|
|
|
|
statementTexts.Add("select count (Id) from AccessTokens" + whereTextWithoutPaging);
|
2016-11-20 05:59:36 +00:00
|
|
|
|
|
2016-12-13 08:45:04 +00:00
|
|
|
|
var statements = PrepareAllSafe(db, statementTexts)
|
2016-12-12 05:49:19 +00:00
|
|
|
|
.ToList();
|
2016-11-20 05:59:36 +00:00
|
|
|
|
|
2016-12-12 05:49:19 +00:00
|
|
|
|
using (var statement = statements[0])
|
2016-11-20 05:59:36 +00:00
|
|
|
|
{
|
2016-12-12 05:49:19 +00:00
|
|
|
|
BindAuthenticationQueryParams(query, statement);
|
2016-11-21 03:52:58 +00:00
|
|
|
|
|
2016-12-12 05:49:19 +00:00
|
|
|
|
foreach (var row in statement.ExecuteQuery())
|
|
|
|
|
{
|
|
|
|
|
list.Add(Get(row));
|
|
|
|
|
}
|
2016-11-21 03:52:58 +00:00
|
|
|
|
|
2016-12-12 05:49:19 +00:00
|
|
|
|
using (var totalCountStatement = statements[1])
|
2016-11-21 03:52:58 +00:00
|
|
|
|
{
|
2016-12-12 05:49:19 +00:00
|
|
|
|
BindAuthenticationQueryParams(query, totalCountStatement);
|
|
|
|
|
|
2016-12-12 08:53:25 +00:00
|
|
|
|
result.TotalRecordCount = totalCountStatement.ExecuteQuery()
|
2016-12-12 05:49:19 +00:00
|
|
|
|
.SelectScalarInt()
|
|
|
|
|
.First();
|
|
|
|
|
}
|
2016-11-21 03:52:58 +00:00
|
|
|
|
}
|
2016-12-12 05:49:19 +00:00
|
|
|
|
|
2017-08-09 19:56:38 +00:00
|
|
|
|
result.Items = list.ToArray(list.Count);
|
2016-12-13 07:36:30 +00:00
|
|
|
|
return result;
|
2016-12-12 05:49:19 +00:00
|
|
|
|
|
2016-12-13 07:36:30 +00:00
|
|
|
|
}, ReadTransactionMode);
|
2016-11-20 05:59:36 +00:00
|
|
|
|
}
|
2016-11-18 09:28:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AuthenticationInfo Get(string id)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(id))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("id");
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-11 05:12:00 +00:00
|
|
|
|
using (WriteLock.Read())
|
2016-11-18 09:28:39 +00:00
|
|
|
|
{
|
2016-12-11 05:12:00 +00:00
|
|
|
|
using (var connection = CreateConnection(true))
|
2016-11-18 09:28:39 +00:00
|
|
|
|
{
|
2016-11-20 05:59:36 +00:00
|
|
|
|
var commandText = BaseSelectText + " where Id=@Id";
|
2016-11-18 09:28:39 +00:00
|
|
|
|
|
2016-11-20 05:59:36 +00:00
|
|
|
|
using (var statement = connection.PrepareStatement(commandText))
|
2016-11-18 09:28:39 +00:00
|
|
|
|
{
|
2017-05-07 20:02:32 +00:00
|
|
|
|
statement.BindParameters["@Id"].Bind(id.ToGuidBlob());
|
2016-11-20 05:59:36 +00:00
|
|
|
|
|
|
|
|
|
foreach (var row in statement.ExecuteQuery())
|
|
|
|
|
{
|
|
|
|
|
return Get(row);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2016-11-18 09:28:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private AuthenticationInfo Get(IReadOnlyList<IResultSetValue> reader)
|
|
|
|
|
{
|
|
|
|
|
var info = new AuthenticationInfo
|
|
|
|
|
{
|
2017-05-07 20:02:32 +00:00
|
|
|
|
Id = reader[0].ReadGuidFromBlob().ToString("N"),
|
2016-11-18 09:28:39 +00:00
|
|
|
|
AccessToken = reader[1].ToString()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (reader[2].SQLiteType != SQLiteType.Null)
|
|
|
|
|
{
|
|
|
|
|
info.DeviceId = reader[2].ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (reader[3].SQLiteType != SQLiteType.Null)
|
|
|
|
|
{
|
|
|
|
|
info.AppName = reader[3].ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (reader[4].SQLiteType != SQLiteType.Null)
|
|
|
|
|
{
|
|
|
|
|
info.AppVersion = reader[4].ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (reader[5].SQLiteType != SQLiteType.Null)
|
|
|
|
|
{
|
|
|
|
|
info.DeviceName = reader[5].ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (reader[6].SQLiteType != SQLiteType.Null)
|
|
|
|
|
{
|
|
|
|
|
info.UserId = reader[6].ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
info.IsActive = reader[7].ToBool();
|
|
|
|
|
info.DateCreated = reader[8].ReadDateTime();
|
|
|
|
|
|
|
|
|
|
if (reader[9].SQLiteType != SQLiteType.Null)
|
|
|
|
|
{
|
|
|
|
|
info.DateRevoked = reader[9].ReadDateTime();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|