2016-11-18 02:28:39 -07: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;
|
|
|
|
|
|
|
|
|
|
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 12:12:37 -07:00
|
|
|
|
RunDefaultInitialization(connection);
|
2016-11-20 20:52:58 -07:00
|
|
|
|
|
2016-11-18 02:28:39 -07:00
|
|
|
|
string[] queries = {
|
|
|
|
|
|
2016-11-20 20:52:58 -07: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 02:28:39 -07:00
|
|
|
|
"create index if not exists idx_AccessTokens on AccessTokens(Id)"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
connection.RunQueries(queries);
|
|
|
|
|
|
|
|
|
|
connection.RunInTransaction(db =>
|
|
|
|
|
{
|
2016-11-18 22:52:49 -07:00
|
|
|
|
var existingColumnNames = GetColumnNames(db, "AccessTokens");
|
|
|
|
|
|
|
|
|
|
AddColumn(db, "AccessTokens", "AppVersion", "TEXT", existingColumnNames);
|
2016-11-28 12:26:48 -07:00
|
|
|
|
|
|
|
|
|
}, TransactionMode);
|
2016-11-18 02:28:39 -07: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-11-28 12:26:48 -07:00
|
|
|
|
using (var connection = CreateConnection())
|
2016-11-18 02:28:39 -07:00
|
|
|
|
{
|
2016-11-28 12:26:48 -07:00
|
|
|
|
using (WriteLock.Write())
|
2016-11-18 02:28:39 -07:00
|
|
|
|
{
|
|
|
|
|
connection.RunInTransaction(db =>
|
|
|
|
|
{
|
2016-11-19 22:59:36 -07: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)"))
|
|
|
|
|
{
|
2016-11-20 00:10:07 -07:00
|
|
|
|
statement.TryBind("@Id", info.Id.ToGuidParamValue());
|
|
|
|
|
statement.TryBind("@AccessToken", info.AccessToken);
|
2016-11-19 22:59:36 -07:00
|
|
|
|
|
2016-11-20 00:10:07 -07: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-19 22:59:36 -07:00
|
|
|
|
|
|
|
|
|
if (info.DateRevoked.HasValue)
|
|
|
|
|
{
|
2016-11-20 00:10:07 -07:00
|
|
|
|
statement.TryBind("@DateRevoked", info.DateRevoked.Value.ToDateTimeParamValue());
|
2016-11-19 22:59:36 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-11-20 00:10:07 -07:00
|
|
|
|
statement.TryBindNull("@DateRevoked");
|
2016-11-19 22:59:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
statement.MoveNext();
|
|
|
|
|
}
|
2016-11-28 12:26:48 -07:00
|
|
|
|
|
|
|
|
|
}, TransactionMode);
|
2016-11-18 02:28:39 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private const string BaseSelectText = "select Id, AccessToken, DeviceId, AppName, AppVersion, DeviceName, UserId, IsActive, DateCreated, DateRevoked from AccessTokens";
|
|
|
|
|
|
2016-11-19 22:59:36 -07:00
|
|
|
|
private void BindAuthenticationQueryParams(AuthenticationInfoQuery query, IStatement statement)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.AccessToken))
|
|
|
|
|
{
|
2016-11-20 00:10:07 -07:00
|
|
|
|
statement.TryBind("@AccessToken", query.AccessToken);
|
2016-11-19 22:59:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.UserId))
|
|
|
|
|
{
|
2016-11-20 00:10:07 -07:00
|
|
|
|
statement.TryBind("@UserId", query.UserId);
|
2016-11-19 22:59:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.DeviceId))
|
|
|
|
|
{
|
2016-11-20 00:10:07 -07:00
|
|
|
|
statement.TryBind("@DeviceId", query.DeviceId);
|
2016-11-19 22:59:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query.IsActive.HasValue)
|
|
|
|
|
{
|
2016-11-20 00:10:07 -07:00
|
|
|
|
statement.TryBind("@IsActive", query.IsActive.Value);
|
2016-11-19 22:59:36 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-18 02:28:39 -07:00
|
|
|
|
public QueryResult<AuthenticationInfo> Get(AuthenticationInfoQuery query)
|
|
|
|
|
{
|
|
|
|
|
if (query == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("query");
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-29 12:12:37 -07:00
|
|
|
|
var commandText = BaseSelectText;
|
2016-11-18 02:28:39 -07:00
|
|
|
|
|
2016-11-29 12:12:37 -07:00
|
|
|
|
var whereClauses = new List<string>();
|
2016-11-18 02:28:39 -07:00
|
|
|
|
|
2016-11-29 12:12:37 -07:00
|
|
|
|
var startIndex = query.StartIndex ?? 0;
|
2016-11-18 02:28:39 -07:00
|
|
|
|
|
2016-11-29 12:12:37 -07:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.AccessToken))
|
|
|
|
|
{
|
|
|
|
|
whereClauses.Add("AccessToken=@AccessToken");
|
|
|
|
|
}
|
2016-11-18 02:28:39 -07:00
|
|
|
|
|
2016-11-29 12:12:37 -07:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.UserId))
|
|
|
|
|
{
|
|
|
|
|
whereClauses.Add("UserId=@UserId");
|
|
|
|
|
}
|
2016-11-25 10:36:00 -07:00
|
|
|
|
|
2016-11-29 12:12:37 -07:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.DeviceId))
|
|
|
|
|
{
|
|
|
|
|
whereClauses.Add("DeviceId=@DeviceId");
|
|
|
|
|
}
|
2016-11-18 02:28:39 -07:00
|
|
|
|
|
2016-11-29 12:12:37 -07:00
|
|
|
|
if (query.IsActive.HasValue)
|
|
|
|
|
{
|
|
|
|
|
whereClauses.Add("IsActive=@IsActive");
|
|
|
|
|
}
|
2016-11-18 02:28:39 -07:00
|
|
|
|
|
2016-11-29 12:12:37 -07:00
|
|
|
|
if (query.HasUser.HasValue)
|
|
|
|
|
{
|
|
|
|
|
if (query.HasUser.Value)
|
|
|
|
|
{
|
|
|
|
|
whereClauses.Add("UserId not null");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
whereClauses.Add("UserId is null");
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-25 10:36:00 -07:00
|
|
|
|
|
2016-11-29 12:12:37 -07:00
|
|
|
|
var whereTextWithoutPaging = whereClauses.Count == 0 ?
|
|
|
|
|
string.Empty :
|
|
|
|
|
" where " + string.Join(" AND ", whereClauses.ToArray());
|
2016-11-18 02:28:39 -07:00
|
|
|
|
|
2016-11-29 12:12:37 -07:00
|
|
|
|
if (startIndex > 0)
|
|
|
|
|
{
|
|
|
|
|
var pagingWhereText = whereClauses.Count == 0 ?
|
|
|
|
|
string.Empty :
|
|
|
|
|
" where " + string.Join(" AND ", whereClauses.ToArray());
|
2016-11-18 02:28:39 -07:00
|
|
|
|
|
2016-11-29 12:12:37 -07: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 02:28:39 -07:00
|
|
|
|
|
2016-11-29 12:12:37 -07:00
|
|
|
|
var whereText = whereClauses.Count == 0 ?
|
|
|
|
|
string.Empty :
|
|
|
|
|
" where " + string.Join(" AND ", whereClauses.ToArray());
|
2016-11-18 02:28:39 -07:00
|
|
|
|
|
2016-11-29 12:12:37 -07:00
|
|
|
|
commandText += whereText;
|
2016-11-18 02:28:39 -07:00
|
|
|
|
|
2016-11-29 12:12:37 -07:00
|
|
|
|
commandText += " ORDER BY DateCreated";
|
2016-11-18 02:28:39 -07:00
|
|
|
|
|
2016-11-29 12:12:37 -07:00
|
|
|
|
if (query.Limit.HasValue)
|
|
|
|
|
{
|
|
|
|
|
commandText += " LIMIT " + query.Limit.Value.ToString(_usCulture);
|
|
|
|
|
}
|
2016-11-25 10:36:00 -07:00
|
|
|
|
|
2016-11-29 12:12:37 -07:00
|
|
|
|
var list = new List<AuthenticationInfo>();
|
2016-11-18 02:28:39 -07:00
|
|
|
|
|
2016-11-29 12:12:37 -07:00
|
|
|
|
using (var connection = CreateConnection(true))
|
|
|
|
|
{
|
|
|
|
|
using (WriteLock.Read())
|
|
|
|
|
{
|
2016-11-20 20:52:58 -07:00
|
|
|
|
using (var statement = connection.PrepareStatement(commandText))
|
2016-11-19 22:59:36 -07:00
|
|
|
|
{
|
2016-11-20 20:52:58 -07:00
|
|
|
|
BindAuthenticationQueryParams(query, statement);
|
2016-11-19 22:59:36 -07:00
|
|
|
|
|
2016-11-20 20:52:58 -07:00
|
|
|
|
foreach (var row in statement.ExecuteQuery())
|
|
|
|
|
{
|
|
|
|
|
list.Add(Get(row));
|
|
|
|
|
}
|
2016-11-19 22:59:36 -07:00
|
|
|
|
|
2016-11-20 20:52:58 -07:00
|
|
|
|
using (var totalCountStatement = connection.PrepareStatement("select count (Id) from AccessTokens" + whereTextWithoutPaging))
|
2016-11-19 22:59:36 -07:00
|
|
|
|
{
|
2016-11-20 20:52:58 -07:00
|
|
|
|
BindAuthenticationQueryParams(query, totalCountStatement);
|
|
|
|
|
|
|
|
|
|
var count = totalCountStatement.ExecuteQuery()
|
|
|
|
|
.SelectScalarInt()
|
|
|
|
|
.First();
|
|
|
|
|
|
|
|
|
|
return new QueryResult<AuthenticationInfo>()
|
|
|
|
|
{
|
|
|
|
|
Items = list.ToArray(),
|
|
|
|
|
TotalRecordCount = count
|
|
|
|
|
};
|
|
|
|
|
}
|
2016-11-19 22:59:36 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-18 02:28:39 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AuthenticationInfo Get(string id)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(id))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("id");
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-28 12:26:48 -07:00
|
|
|
|
using (var connection = CreateConnection(true))
|
2016-11-18 02:28:39 -07:00
|
|
|
|
{
|
2016-11-28 12:26:48 -07:00
|
|
|
|
using (WriteLock.Read())
|
2016-11-18 02:28:39 -07:00
|
|
|
|
{
|
2016-11-19 22:59:36 -07:00
|
|
|
|
var commandText = BaseSelectText + " where Id=@Id";
|
2016-11-18 02:28:39 -07:00
|
|
|
|
|
2016-11-19 22:59:36 -07:00
|
|
|
|
using (var statement = connection.PrepareStatement(commandText))
|
2016-11-18 02:28:39 -07:00
|
|
|
|
{
|
2016-11-19 22:59:36 -07:00
|
|
|
|
statement.BindParameters["@Id"].Bind(id.ToGuidParamValue());
|
|
|
|
|
|
|
|
|
|
foreach (var row in statement.ExecuteQuery())
|
|
|
|
|
{
|
|
|
|
|
return Get(row);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2016-11-18 02:28:39 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private AuthenticationInfo Get(IReadOnlyList<IResultSetValue> reader)
|
|
|
|
|
{
|
|
|
|
|
var info = new AuthenticationInfo
|
|
|
|
|
{
|
|
|
|
|
Id = reader[0].ReadGuid().ToString("N"),
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|