mirror of
https://github.com/streetwriters/notesnook-sync-server.git
synced 2026-02-12 19:22:45 +00:00
open source Notesnook API
This commit is contained in:
211
Notesnook.API/Services/S3Service.cs
Normal file
211
Notesnook.API/Services/S3Service.cs
Normal file
@@ -0,0 +1,211 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Amazon;
|
||||
using Amazon.Runtime;
|
||||
using Amazon.S3;
|
||||
using Amazon.S3.Model;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Notesnook.API.Interfaces;
|
||||
using Notesnook.API.Models;
|
||||
using Streetwriters.Common;
|
||||
|
||||
namespace Notesnook.API.Services
|
||||
{
|
||||
public class S3Service : IS3Service
|
||||
{
|
||||
private readonly string BUCKET_NAME = "nn-attachments";
|
||||
private AmazonS3Client S3Client { get; }
|
||||
private HttpClient httpClient = new HttpClient();
|
||||
|
||||
public S3Service(IOptions<S3Options> s3Options)
|
||||
{
|
||||
var config = new AmazonS3Config
|
||||
{
|
||||
#if DEBUG
|
||||
ServiceURL = Servers.S3Server.ToString(),
|
||||
#else
|
||||
ServiceURL = s3Options.Value.ServiceUrl,
|
||||
AuthenticationRegion = s3Options.Value.Region,
|
||||
#endif
|
||||
ForcePathStyle = true,
|
||||
SignatureMethod = SigningAlgorithm.HmacSHA256,
|
||||
SignatureVersion = "4"
|
||||
};
|
||||
#if DEBUG
|
||||
S3Client = new AmazonS3Client("S3RVER", "S3RVER", config);
|
||||
#else
|
||||
S3Client = new AmazonS3Client(s3Options.Value.AccessKeyId, s3Options.Value.SecretAccessKey, config);
|
||||
#endif
|
||||
AWSConfigsS3.UseSignatureVersion4 = true;
|
||||
}
|
||||
|
||||
public async Task DeleteObjectAsync(string userId, string name)
|
||||
{
|
||||
var objectName = GetFullObjectName(userId, name);
|
||||
if (objectName == null) throw new Exception("Invalid object name."); ;
|
||||
|
||||
var response = await S3Client.DeleteObjectAsync(BUCKET_NAME, objectName);
|
||||
|
||||
if (!IsSuccessStatusCode(((int)response.HttpStatusCode)))
|
||||
throw new Exception("Could not delete object.");
|
||||
}
|
||||
|
||||
public async Task DeleteDirectoryAsync(string userId)
|
||||
{
|
||||
var request = new ListObjectsV2Request
|
||||
{
|
||||
BucketName = BUCKET_NAME,
|
||||
Prefix = userId,
|
||||
};
|
||||
|
||||
var response = new ListObjectsV2Response();
|
||||
var keys = new List<KeyVersion>();
|
||||
do
|
||||
{
|
||||
response = await S3Client.ListObjectsV2Async(request);
|
||||
response.S3Objects.ForEach(obj => keys.Add(new KeyVersion
|
||||
{
|
||||
Key = obj.Key,
|
||||
}));
|
||||
|
||||
request.ContinuationToken = response.NextContinuationToken;
|
||||
}
|
||||
while (response.IsTruncated);
|
||||
|
||||
if (keys.Count <= 0) return;
|
||||
|
||||
var deleteObjectsResponse = await S3Client
|
||||
.DeleteObjectsAsync(new DeleteObjectsRequest
|
||||
{
|
||||
BucketName = BUCKET_NAME,
|
||||
Objects = keys,
|
||||
});
|
||||
|
||||
if (!IsSuccessStatusCode((int)deleteObjectsResponse.HttpStatusCode))
|
||||
throw new Exception("Could not delete directory.");
|
||||
}
|
||||
|
||||
public async Task<long?> GetObjectSizeAsync(string userId, string name)
|
||||
{
|
||||
var url = this.GetPresignedURL(userId, name, HttpVerb.HEAD);
|
||||
if (url == null) return null;
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Head, url);
|
||||
var response = await httpClient.SendAsync(request);
|
||||
return response.Content.Headers.ContentLength;
|
||||
}
|
||||
|
||||
|
||||
public string GetUploadObjectUrl(string userId, string name)
|
||||
{
|
||||
var url = this.GetPresignedURL(userId, name, HttpVerb.PUT);
|
||||
if (url == null) return null;
|
||||
return url;
|
||||
}
|
||||
|
||||
public string GetDownloadObjectUrl(string userId, string name)
|
||||
{
|
||||
var url = this.GetPresignedURL(userId, name, HttpVerb.GET);
|
||||
if (url == null) return null;
|
||||
return url;
|
||||
}
|
||||
|
||||
public async Task<MultipartUploadMeta> StartMultipartUploadAsync(string userId, string name, int parts, string uploadId = null)
|
||||
{
|
||||
var objectName = GetFullObjectName(userId, name);
|
||||
if (userId == null || objectName == null) throw new Exception("Could not initiate multipart upload.");
|
||||
|
||||
if (string.IsNullOrEmpty(uploadId))
|
||||
{
|
||||
var response = await S3Client.InitiateMultipartUploadAsync(BUCKET_NAME, objectName);
|
||||
if (!IsSuccessStatusCode(((int)response.HttpStatusCode))) throw new Exception("Failed to initiate multipart upload.");
|
||||
|
||||
uploadId = response.UploadId;
|
||||
}
|
||||
|
||||
var signedUrls = new string[parts];
|
||||
for (var i = 0; i < parts; ++i)
|
||||
{
|
||||
signedUrls[i] = GetPresignedURLForUploadPart(objectName, uploadId, i + 1);
|
||||
}
|
||||
|
||||
return new MultipartUploadMeta
|
||||
{
|
||||
UploadId = uploadId,
|
||||
Parts = signedUrls
|
||||
};
|
||||
}
|
||||
|
||||
public async Task AbortMultipartUploadAsync(string userId, string name, string uploadId)
|
||||
{
|
||||
var objectName = GetFullObjectName(userId, name);
|
||||
if (userId == null || objectName == null) throw new Exception("Could not abort multipart upload.");
|
||||
|
||||
var response = await S3Client.AbortMultipartUploadAsync(BUCKET_NAME, objectName, uploadId);
|
||||
if (!IsSuccessStatusCode(((int)response.HttpStatusCode))) throw new Exception("Failed to abort multipart upload.");
|
||||
}
|
||||
|
||||
public async Task CompleteMultipartUploadAsync(string userId, CompleteMultipartUploadRequest uploadRequest)
|
||||
{
|
||||
var objectName = GetFullObjectName(userId, uploadRequest.Key);
|
||||
if (userId == null || objectName == null) throw new Exception("Could not abort multipart upload.");
|
||||
|
||||
uploadRequest.Key = objectName;
|
||||
uploadRequest.BucketName = BUCKET_NAME;
|
||||
var response = await S3Client.CompleteMultipartUploadAsync(uploadRequest);
|
||||
if (!IsSuccessStatusCode(((int)response.HttpStatusCode))) throw new Exception("Failed to complete multipart upload.");
|
||||
}
|
||||
|
||||
private string GetPresignedURL(string userId, string name, HttpVerb httpVerb)
|
||||
{
|
||||
var objectName = GetFullObjectName(userId, name);
|
||||
if (userId == null || objectName == null) return null;
|
||||
|
||||
var request = new GetPreSignedUrlRequest
|
||||
{
|
||||
BucketName = BUCKET_NAME,
|
||||
Expires = System.DateTime.Now.AddHours(1),
|
||||
Verb = httpVerb,
|
||||
Key = objectName,
|
||||
#if DEBUG
|
||||
Protocol = Protocol.HTTP,
|
||||
#else
|
||||
Protocol = Protocol.HTTPS,
|
||||
#endif
|
||||
};
|
||||
return S3Client.GetPreSignedURL(request);
|
||||
}
|
||||
|
||||
private string GetPresignedURLForUploadPart(string objectName, string uploadId, int partNumber)
|
||||
{
|
||||
return S3Client.GetPreSignedURL(new GetPreSignedUrlRequest
|
||||
{
|
||||
BucketName = BUCKET_NAME,
|
||||
Expires = System.DateTime.Now.AddHours(1),
|
||||
Verb = HttpVerb.PUT,
|
||||
Key = objectName,
|
||||
PartNumber = partNumber,
|
||||
UploadId = uploadId,
|
||||
#if DEBUG
|
||||
Protocol = Protocol.HTTP,
|
||||
#else
|
||||
Protocol = Protocol.HTTPS,
|
||||
#endif
|
||||
});
|
||||
}
|
||||
|
||||
private string GetFullObjectName(string userId, string name)
|
||||
{
|
||||
if (userId == null || !Regex.IsMatch(name, "[0-9a-zA-Z!" + Regex.Escape("-") + "_.*'()]")) return null;
|
||||
return $"{userId}/{name}";
|
||||
}
|
||||
|
||||
bool IsSuccessStatusCode(int statusCode)
|
||||
{
|
||||
return ((int)statusCode >= 200) && ((int)statusCode <= 299);
|
||||
}
|
||||
}
|
||||
}
|
||||
194
Notesnook.API/Services/UserService.cs
Normal file
194
Notesnook.API/Services/UserService.cs
Normal file
@@ -0,0 +1,194 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Notesnook.API.Interfaces;
|
||||
using Notesnook.API.Models;
|
||||
using Notesnook.API.Models.Responses;
|
||||
using Streetwriters.Common;
|
||||
using Streetwriters.Common.Enums;
|
||||
using Streetwriters.Common.Extensions;
|
||||
using Streetwriters.Common.Messages;
|
||||
using Streetwriters.Common.Models;
|
||||
using Streetwriters.Data.Interfaces;
|
||||
|
||||
namespace Notesnook.API.Services
|
||||
{
|
||||
public class UserService : IUserService
|
||||
{
|
||||
private static readonly System.Security.Cryptography.RandomNumberGenerator Rng = System.Security.Cryptography.RandomNumberGenerator.Create();
|
||||
private readonly HttpClient httpClient;
|
||||
private IHttpContextAccessor HttpContextAccessor { get; }
|
||||
private ISyncItemsRepositoryAccessor Repositories { get; }
|
||||
private IS3Service S3Service { get; set; }
|
||||
private readonly IUnitOfWork unit;
|
||||
|
||||
public UserService(IHttpContextAccessor accessor,
|
||||
ISyncItemsRepositoryAccessor syncItemsRepositoryAccessor,
|
||||
IUnitOfWork unitOfWork, IS3Service s3Service)
|
||||
{
|
||||
httpClient = new HttpClient();
|
||||
|
||||
Repositories = syncItemsRepositoryAccessor;
|
||||
HttpContextAccessor = accessor;
|
||||
unit = unitOfWork;
|
||||
S3Service = s3Service;
|
||||
}
|
||||
|
||||
public async Task CreateUserAsync()
|
||||
{
|
||||
SignupResponse response = await httpClient.ForwardAsync<SignupResponse>(this.HttpContextAccessor, $"{Servers.IdentityServer}/signup", HttpMethod.Post);
|
||||
if (!response.Success || (response.Errors != null && response.Errors.Length > 0))
|
||||
{
|
||||
await Slogger<UserService>.Error(nameof(CreateUserAsync), "Couldn't sign up.", JsonSerializer.Serialize(response));
|
||||
if (response.Errors != null && response.Errors.Length > 0) throw new Exception(string.Join(" ", response.Errors));
|
||||
else throw new Exception("Could not create a new account. Error code: " + response.StatusCode);
|
||||
}
|
||||
|
||||
await Repositories.UsersSettings.InsertAsync(new UserSettings
|
||||
{
|
||||
UserId = response.UserId,
|
||||
LastSynced = 0,
|
||||
Salt = GetSalt()
|
||||
});
|
||||
|
||||
await WampServers.SubscriptionServer.PublishMessageAsync(WampServers.SubscriptionServer.Topics.CreateSubscriptionTopic, new CreateSubscriptionMessage
|
||||
{
|
||||
AppId = ApplicationType.NOTESNOOK,
|
||||
Provider = SubscriptionProvider.STREETWRITERS,
|
||||
Type = SubscriptionType.BASIC,
|
||||
UserId = response.UserId,
|
||||
StartTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
|
||||
});
|
||||
|
||||
await Slogger<UserService>.Info(nameof(CreateUserAsync), "New user created.", JsonSerializer.Serialize(response));
|
||||
}
|
||||
|
||||
public async Task<UserResponse> GetUserAsync(bool repair = true)
|
||||
{
|
||||
UserResponse response = await httpClient.ForwardAsync<UserResponse>(this.HttpContextAccessor, $"{Servers.IdentityServer.ToString()}/account", HttpMethod.Get);
|
||||
if (!response.Success) return response;
|
||||
|
||||
SubscriptionResponse subscriptionResponse = await httpClient.ForwardAsync<SubscriptionResponse>(this.HttpContextAccessor, $"{Servers.SubscriptionServer}/subscriptions", HttpMethod.Get);
|
||||
if (repair && subscriptionResponse.StatusCode == 404)
|
||||
{
|
||||
await Slogger<UserService>.Error(nameof(GetUserAsync), "Repairing user subscription.", JsonSerializer.Serialize(response));
|
||||
// user was partially created. We should continue the process here.
|
||||
await WampServers.SubscriptionServer.PublishMessageAsync(WampServers.SubscriptionServer.Topics.CreateSubscriptionTopic, new CreateSubscriptionMessage
|
||||
{
|
||||
AppId = ApplicationType.NOTESNOOK,
|
||||
Provider = SubscriptionProvider.STREETWRITERS,
|
||||
Type = SubscriptionType.TRIAL,
|
||||
UserId = response.UserId,
|
||||
StartTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
|
||||
ExpiryTime = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeMilliseconds()
|
||||
});
|
||||
// just a dummy object
|
||||
subscriptionResponse.Subscription = new Subscription
|
||||
{
|
||||
AppId = ApplicationType.NOTESNOOK,
|
||||
Provider = SubscriptionProvider.STREETWRITERS,
|
||||
Type = SubscriptionType.TRIAL,
|
||||
UserId = response.UserId,
|
||||
StartDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
|
||||
ExpiryDate = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeMilliseconds()
|
||||
};
|
||||
}
|
||||
|
||||
var userSettings = await Repositories.UsersSettings.FindOneAsync((u) => u.UserId == response.UserId);
|
||||
if (repair && userSettings == null)
|
||||
{
|
||||
await Slogger<UserService>.Error(nameof(GetUserAsync), "Repairing user settings.", JsonSerializer.Serialize(response));
|
||||
userSettings = new UserSettings
|
||||
{
|
||||
UserId = response.UserId,
|
||||
LastSynced = 0,
|
||||
Salt = GetSalt()
|
||||
};
|
||||
await Repositories.UsersSettings.InsertAsync(userSettings);
|
||||
}
|
||||
response.AttachmentsKey = userSettings.AttachmentsKey;
|
||||
response.Salt = userSettings.Salt;
|
||||
response.Subscription = subscriptionResponse.Subscription;
|
||||
return response;
|
||||
}
|
||||
|
||||
public async Task SetUserAttachmentsKeyAsync(string userId, IEncrypted key)
|
||||
{
|
||||
var userSettings = await Repositories.UsersSettings.FindOneAsync((u) => u.UserId == userId);
|
||||
userSettings.AttachmentsKey = (EncryptedData)key;
|
||||
await Repositories.UsersSettings.UpdateAsync(userSettings.Id, userSettings);
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteUserAsync(string userId, string jti)
|
||||
{
|
||||
var cc = new CancellationTokenSource();
|
||||
|
||||
Repositories.Notes.DeleteByUserId(userId);
|
||||
Repositories.Notebooks.DeleteByUserId(userId);
|
||||
Repositories.Shortcuts.DeleteByUserId(userId);
|
||||
Repositories.Contents.DeleteByUserId(userId);
|
||||
Repositories.Settings.DeleteByUserId(userId);
|
||||
Repositories.Attachments.DeleteByUserId(userId);
|
||||
Repositories.UsersSettings.Delete((u) => u.UserId == userId);
|
||||
|
||||
await WampServers.SubscriptionServer.PublishMessageAsync(WampServers.SubscriptionServer.Topics.DeleteSubscriptionTopic, new DeleteSubscriptionMessage
|
||||
{
|
||||
AppId = ApplicationType.NOTESNOOK,
|
||||
UserId = userId
|
||||
});
|
||||
|
||||
await WampServers.MessengerServer.PublishMessageAsync(WampServers.MessengerServer.Topics.SendSSETopic, new SendSSEMessage
|
||||
{
|
||||
SendToAll = false,
|
||||
OriginTokenId = jti,
|
||||
UserId = userId,
|
||||
Message = new Message
|
||||
{
|
||||
Type = "userDeleted",
|
||||
Data = JsonSerializer.Serialize(new { reason = "accountDeleted" })
|
||||
}
|
||||
});
|
||||
|
||||
await S3Service.DeleteDirectoryAsync(userId);
|
||||
|
||||
return await unit.Commit();
|
||||
}
|
||||
|
||||
public async Task<bool> ResetUserAsync(string userId, bool removeAttachments)
|
||||
{
|
||||
var cc = new CancellationTokenSource();
|
||||
|
||||
Repositories.Notes.DeleteByUserId(userId);
|
||||
Repositories.Notebooks.DeleteByUserId(userId);
|
||||
Repositories.Shortcuts.DeleteByUserId(userId);
|
||||
Repositories.Contents.DeleteByUserId(userId);
|
||||
Repositories.Settings.DeleteByUserId(userId);
|
||||
Repositories.Attachments.DeleteByUserId(userId);
|
||||
Repositories.Monographs.DeleteMany((m) => m.UserId == userId);
|
||||
if (!await unit.Commit()) return false;
|
||||
|
||||
var userSettings = await Repositories.UsersSettings.FindOneAsync((s) => s.UserId == userId);
|
||||
|
||||
userSettings.AttachmentsKey = null;
|
||||
userSettings.VaultKey = null;
|
||||
userSettings.LastSynced = 0;
|
||||
|
||||
await Repositories.UsersSettings.UpsertAsync(userSettings, (s) => s.UserId == userId);
|
||||
|
||||
if (removeAttachments)
|
||||
await S3Service.DeleteDirectoryAsync(userId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private string GetSalt()
|
||||
{
|
||||
byte[] salt = new byte[16];
|
||||
Rng.GetNonZeroBytes(salt);
|
||||
return Convert.ToBase64String(salt).TrimEnd('=').Replace('+', '-').Replace('/', '_');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user