Skip to content

Commit

Permalink
global: move more config to env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodrr committed Dec 29, 2022
1 parent 9463d48 commit 3f89ebc
Show file tree
Hide file tree
Showing 24 changed files with 101 additions and 167 deletions.
21 changes: 20 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
# Required variables
NOTESNOOK_API_SECRET= # This should be a randomly generated secret
NOTESNOOK_API_SECRET= # This should be a randomly generated secret

# S3 related variables for storing attachments
S3_ACCESS_KEY=
S3_ACCESS_KEY_ID=
S3_SERVICE_URL=
S3_REGION=

# SMTP settings required for delivering emails
SMTP_USERNAME=
SMTP_PASSWORD=
SMTP_HOST=
SMTP_PORT=
SMTP_REPLYTO_NAME= # optional
SMTP_REPLYTO_EMAIL= # optional
NOTESNOOK_SENDER_EMAIL= # optional
NOTESNOOK_SENDER_NAME= # optional

# MessageBird is used for 2FA via SMS
MESSAGEBIRD_ACCESS_KEY=
1 change: 0 additions & 1 deletion Notesnook.API/Accessors/SyncItemsRepositoryAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ You should have received a copy of the Affero GNU General Public License
using Notesnook.API.Interfaces;
using Notesnook.API.Models;
using Notesnook.API.Repositories;
using Streetwriters.Common.Models;
using Streetwriters.Data.Repositories;

namespace Notesnook.API.Accessors
Expand Down
1 change: 0 additions & 1 deletion Notesnook.API/Authorization/NotesnookUserRequirement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;

Expand Down
1 change: 0 additions & 1 deletion Notesnook.API/Authorization/SyncRequirement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ You should have received a copy of the Affero GNU General Public License
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization.Policy;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR;

namespace Notesnook.API.Authorization
{
Expand Down
1 change: 0 additions & 1 deletion Notesnook.API/Controllers/MonographsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ You should have received a copy of the Affero GNU General Public License
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Notesnook.API.Models;
using Streetwriters.Common.Models;
using Streetwriters.Data.Interfaces;
using Streetwriters.Data.Repositories;

Expand Down
5 changes: 0 additions & 5 deletions Notesnook.API/Controllers/S3Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@ You should have received a copy of the Affero GNU General Public License

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Amazon.S3;
using Amazon.Runtime;
using Amazon.S3.Model;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Security.Claims;
using System.Net.Http;
using System.Linq;
using Notesnook.API.Interfaces;
using System;

Expand Down
2 changes: 0 additions & 2 deletions Notesnook.API/Extensions/AuthorizationResultTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ You should have received a copy of the Affero GNU General Public License
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization.Policy;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Notesnook.API.Authorization;

namespace Notesnook.API.Extensions
{
Expand Down
2 changes: 0 additions & 2 deletions Notesnook.API/Models/Responses/SignupResponse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
using Streetwriters.Common.Interfaces;
using Streetwriters.Common.Models;

namespace Notesnook.API.Models.Responses
Expand Down
2 changes: 0 additions & 2 deletions Notesnook.API/Models/Responses/UserResponse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
using Notesnook.API.Interfaces;
using Streetwriters.Common.Interfaces;
using Streetwriters.Common.Models;

Expand Down
4 changes: 0 additions & 4 deletions Notesnook.API/Models/S3Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using Microsoft.AspNetCore.Mvc;

namespace Notesnook.API.Models
{
public class S3Options
Expand Down
9 changes: 5 additions & 4 deletions Notesnook.API/Services/S3Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ public class S3Service : IS3Service
private AmazonS3Client S3Client { get; }
private HttpClient httpClient = new HttpClient();

public S3Service(IOptions<S3Options> s3Options)
public S3Service()
{

var config = new AmazonS3Config
{
#if DEBUG
ServiceURL = Servers.S3Server.ToString(),
#else
ServiceURL = s3Options.Value.ServiceUrl,
AuthenticationRegion = s3Options.Value.Region,
ServiceURL = Constants.S3_SERVICE_URL,
AuthenticationRegion = Constants.S3_REGION,
#endif
ForcePathStyle = true,
SignatureMethod = SigningAlgorithm.HmacSHA256,
Expand All @@ -56,7 +57,7 @@ public S3Service(IOptions<S3Options> s3Options)
#if DEBUG
S3Client = new AmazonS3Client("S3RVER", "S3RVER", config);
#else
S3Client = new AmazonS3Client(s3Options.Value.AccessKeyId, s3Options.Value.SecretAccessKey, config);
S3Client = new AmazonS3Client(Constants.S3_ACCESS_KEY_ID, Constants.S3_ACCESS_KEY, config);
#endif
AWSConfigsS3.UseSignatureVersion4 = true;
}
Expand Down
2 changes: 1 addition & 1 deletion Notesnook.API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void ConfigureServices(IServiceCollection services)
.AddOAuth2Introspection("introspection", options =>
{
options.Authority = Servers.IdentityServer.ToString();
options.ClientSecret = Environment.GetEnvironmentVariable("NOTESNOOK_API_SECRET");
options.ClientSecret = Constants.NOTESNOOK_API_SECRET;
options.ClientId = "notesnook";
options.DiscoveryPolicy.RequireHttps = false;
options.TokenRetriever = new Func<HttpRequest, string>(req =>
Expand Down
49 changes: 2 additions & 47 deletions Streetwriters.Common/Clients.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,10 @@ public class Clients
{
Id = "notesnook",
Name = "Notesnook",
ProductIds = new string[]
{
"com.streetwriters.notesnook",
"org.streetwriters.notesnook",
"com.streetwriters.notesnook.sub.mo",
"com.streetwriters.notesnook.sub.yr",
"com.streetwriters.notesnook.sub.mo.15",
"com.streetwriters.notesnook.sub.yr.15",
"com.streetwriters.notesnook.sub.yr.trialoffer",
"com.streetwriters.notesnook.sub.mo.trialoffer",
"com.streetwriters.notesnook.sub.mo.tier1",
"com.streetwriters.notesnook.sub.yr.tier1",
"com.streetwriters.notesnook.sub.mo.tier2",
"com.streetwriters.notesnook.sub.yr.tier2",
"com.streetwriters.notesnook.sub.mo.tier3",
"com.streetwriters.notesnook.sub.yr.tier3",
"9822", // dev
"648884", // monthly tier 1
"658759", // yearly tier 1
"763942", // monthly tier 2
"763945", // yearly tier 2
"763943", // monthly tier 3
"763944", // yearly tier 3
},
SenderEmail = "support@notesnook.com",
SenderName = "Notesnook",
SenderEmail = Constants.NOTESNOOK_SENDER_EMAIL,
SenderName = Constants.NOTESNOOK_SENDER_NAME,
Type = ApplicationType.NOTESNOOK,
AppId = ApplicationType.NOTESNOOK,
WelcomeEmailTemplateId = "d-87768b3ee17d41fdbe4bcf0eb2583682"
};

public static Dictionary<string, IClient> ClientsMap = new Dictionary<string, IClient>
Expand All @@ -84,29 +59,9 @@ public static IClient FindClientByAppId(ApplicationType appId)
return null;
}

public static IClient FindClientByProductId(string productId)
{
foreach (var client in ClientsMap)
{
if (client.Value.ProductIds.Contains(productId)) return client.Value;
}
return null;
}

public static bool IsValidClient(string id)
{
return ClientsMap.ContainsKey(id);
}

public static SubscriptionProvider? PlatformToSubscriptionProvider(string platform)
{
return platform switch
{
"ios" => SubscriptionProvider.APPLE,
"android" => SubscriptionProvider.GOOGLE,
"web" => SubscriptionProvider.PADDLE,
_ => null,
};
}
}
}
47 changes: 47 additions & 0 deletions Streetwriters.Common/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2022 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

using System;

namespace Streetwriters.Common
{
public class Constants
{
// S3 related
public static string S3_ACCESS_KEY = Environment.GetEnvironmentVariable("S3_ACCESS_KEY");
public static string S3_ACCESS_KEY_ID = Environment.GetEnvironmentVariable("S3_ACCESS_KEY_ID");
public static string S3_SERVICE_URL = Environment.GetEnvironmentVariable("S3_HOST");
public static string S3_REGION = Environment.GetEnvironmentVariable("S3_HOST");

// SMTP settings
public static string SMTP_USERNAME = Environment.GetEnvironmentVariable("SMTP_USERNAME");
public static string SMTP_PASSWORD = Environment.GetEnvironmentVariable("SMTP_PASSWORD");
public static string SMTP_HOST = Environment.GetEnvironmentVariable("SMTP_HOST");
public static string SMTP_PORT = Environment.GetEnvironmentVariable("SMTP_PORT");
public static string SMTP_REPLYTO_NAME = Environment.GetEnvironmentVariable("SMTP_REPLYTO_NAME");
public static string SMTP_REPLYTO_EMAIL = Environment.GetEnvironmentVariable("SMTP_REPLYTO_EMAIL");
public static string NOTESNOOK_SENDER_EMAIL = Environment.GetEnvironmentVariable("NOTESNOOK_SENDER_EMAIL");
public static string NOTESNOOK_SENDER_NAME = Environment.GetEnvironmentVariable("NOTESNOOK_SENDER_NAME");

public static string NOTESNOOK_API_SECRET = Environment.GetEnvironmentVariable("NOTESNOOK_API_SECRET");

// MessageBird is used for SMS sending
public static string MESSAGEBIRD_ACCESS_KEY = Environment.GetEnvironmentVariable("MESSAGEBIRD_ACCESS_KEY");
}
}
2 changes: 0 additions & 2 deletions Streetwriters.Common/Interfaces/IClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ public interface IClient
{
string Id { get; set; }
string Name { get; set; }
string[] ProductIds { get; set; }
ApplicationType Type { get; set; }
ApplicationType AppId { get; set; }
string SenderEmail { get; set; }
string SenderName { get; set; }
string WelcomeEmailTemplateId { get; set; }
}
}
2 changes: 0 additions & 2 deletions Streetwriters.Common/Models/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ public class Client : IClient
{
public string Id { get; set; }
public string Name { get; set; }
public string[] ProductIds { get; set; }
public ApplicationType Type { get; set; }
public ApplicationType AppId { get; set; }
public string SenderEmail { get; set; }
public string SenderName { get; set; }
public string WelcomeEmailTemplateId { get; set; }
}
}
3 changes: 2 additions & 1 deletion Streetwriters.Identity/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ You should have received a copy of the Affero GNU General Public License

using IdentityServer4;
using IdentityServer4.Models;
using Streetwriters.Common;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -44,7 +45,7 @@ public static class Config
{
new ApiResource("notesnook", "Notesnook API", new string[] { "verified" })
{
ApiSecrets = { new Secret(Environment.GetEnvironmentVariable("NOTESNOOK_API_SECRET")?.Sha256()) },
ApiSecrets = { new Secret(Constants.NOTESNOOK_API_SECRET?.Sha256()) },
Scopes = { "notesnook.sync" }
},
// local API
Expand Down
1 change: 0 additions & 1 deletion Streetwriters.Identity/Interfaces/IEmailSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace Streetwriters.Identity.Interfaces
{
public interface IEmailSender
{
Task SendWelcomeEmailAsync(string email, IClient client);
Task SendConfirmationEmailAsync(string email, string callbackUrl, IClient client);
Task SendChangeEmailConfirmationAsync(string email, string code, IClient client);
Task SendPasswordResetEmailAsync(string email, string callbackUrl, IClient client);
Expand Down
30 changes: 0 additions & 30 deletions Streetwriters.Identity/Models/MessageBirdOptions.cs

This file was deleted.

33 changes: 0 additions & 33 deletions Streetwriters.Identity/Models/SmtpOptions.cs

This file was deleted.

Loading

0 comments on commit 3f89ebc

Please sign in to comment.