-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df90acc
commit 008464b
Showing
8 changed files
with
179 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace Olive.MFA | ||
{ | ||
using Olive.Email; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
public interface IMFAManager | ||
{ | ||
Task SendOtpViaEmail(IEmailMessage emailMessage); | ||
Task<bool> SendOtpViaSms(string otp, string phoneNumber); | ||
string GetOtp(); | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace Olive.MFA | ||
{ | ||
using Olive.Entities; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
[LogEvents(false)] | ||
[CacheObjects(false)] | ||
public interface ITemporaryLogin : IEntity | ||
{ | ||
DateTime CreatedAt { get; set; } | ||
int ExpiryMinutes { get; set; } | ||
string MFACode { get; set; } | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Olive.MFA | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
public interface ITemporaryLoginService | ||
{ | ||
Task DeleteTemporaryLogin(ITemporaryLogin login); | ||
Task<bool> IsSessionExpired(ITemporaryLogin login); | ||
Task<bool> IsOtpValid(ITemporaryLogin login, string code); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace Olive.MFA | ||
{ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Olive.SMS; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
public static class MFAExtensions | ||
{ | ||
public static IServiceCollection AddMFA(this IServiceCollection @this) | ||
{ | ||
return @this | ||
.AddSingleton<IMFAManager, MFAManager>() | ||
.AddSingleton<ITemporaryLoginService, TemporaryLoginService>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
namespace Olive.MFA | ||
{ | ||
using Olive.Email; | ||
using Olive.Entities; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Twilio; | ||
using Twilio.Rest.Api.V2010.Account; | ||
using Twilio.Types; | ||
|
||
public class MFAManager : IMFAManager | ||
{ | ||
IDatabase Database => Context.Current.Database(); | ||
public Task SendOtpViaEmail(IEmailMessage emailMessage) | ||
{ | ||
return Database.Save(emailMessage); | ||
} | ||
|
||
public Task<bool> SendOtpViaSms(string otp, string phoneNumber) | ||
{ | ||
var accountId = Config.Get("TwilioAccountId"); | ||
var authKey = Config.Get("TwilioAuthKey"); | ||
var sender = Config.Get("TwilioSenderNumber"); | ||
|
||
if (accountId.IsEmpty() || authKey.IsEmpty() || sender.IsEmpty()) | ||
throw new Exception("TwilioAccountId | TwilioAuthKey | TwilioSenderNumber configuration not set"); | ||
|
||
|
||
|
||
TwilioClient.Init(accountId, authKey); | ||
var message = MessageResource.Create( | ||
to: new PhoneNumber(phoneNumber), | ||
from: new PhoneNumber(sender), | ||
body: $"Your OTP code is: {otp}"); | ||
|
||
return Task.FromResult(message != null); | ||
} | ||
public string GetOtp() | ||
{ | ||
var random = new Random(); | ||
var otp = random.Next(100000, 999999).ToString(); | ||
return otp; | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Twilio" Version="7.8.5" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Olive.Email\Olive.Email.csproj" /> | ||
<ProjectReference Include="..\Olive.SMS\Olive.SMS.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace Olive.MFA | ||
{ | ||
using Olive.Entities; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
public class TemporaryLoginService : ITemporaryLoginService | ||
{ | ||
IDatabase Database => Context.Current.Database(); | ||
public Task DeleteTemporaryLogin(ITemporaryLogin login) | ||
{ | ||
return Database.Delete(login); | ||
} | ||
|
||
public async Task<bool> IsOtpValid(ITemporaryLogin login, string code) | ||
{ | ||
return login.MFACode.HasValue() && login.CreatedAt.AddMinutes((double)login.ExpiryMinutes) > LocalTime.Now && | ||
login.MFACode.Equals(code); | ||
} | ||
|
||
public async Task<bool> IsSessionExpired(ITemporaryLogin login) | ||
{ | ||
return login.CreatedAt.AddMinutes(login.ExpiryMinutes) < LocalTime.Now; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters