Skip to content

Commit

Permalink
Fix authentication services
Browse files Browse the repository at this point in the history
  • Loading branch information
Strypper committed Feb 11, 2023
1 parent 04f4e33 commit 0fcb0e0
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
public class LoginSuccessModelRespone
{
public string AccessToken { get; set; }
public TotechsUserModel UserInfo { get; set; }
public PrincipalUserModel UserInfo { get; set; }
}
15 changes: 15 additions & 0 deletions src/Features/Chat/Models/Refits/PrincipalUserModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace MAUIsland;

public record PrincipalUserModel
(
Guid guid,
string userName,
string firstName,
string lastName,
string profilePicUrl,
string email,
string phoneNumber,
string country,
bool gender,
List<string> roles
);
1 change: 1 addition & 0 deletions src/Features/Chat/Models/Refits/ServiceUserInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public record ServiceUserInfo(string guid, string userName, string avatarGuild);
14 changes: 0 additions & 14 deletions src/Features/Chat/Models/Refits/TotechsUserModel.cs

This file was deleted.

6 changes: 6 additions & 0 deletions src/Features/Chat/Models/Sqlite/UserInformation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace MAUIsland;

public class UserInformation
{

}
34 changes: 31 additions & 3 deletions src/Features/Chat/Popups/AuthenticatePopupViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public AuthenticatePopupViewModel(IAppNavigator appNavigator,

[ObservableProperty]
string phoneNumber = "0348164682";

[ObservableProperty]
string email = "[email protected]";

[ObservableProperty]
string avatarUrl;
#endregion

#region [Relay Commands]
Expand All @@ -41,12 +47,34 @@ async Task LoginAsync()
{
try
{
var accessToken = await this.authenticationServices.SignInWithPhoneNumber(PhoneNumber, Password);
var authenticationToken = await this.authenticationServices.AuthenticateWithPhoneNumber(PhoneNumber, Password);
Guard.IsNotNullOrWhiteSpace(authenticationToken);

var userInfo = await this.userServices.GetUserInfo(authenticationToken);
Guard.IsNotNull(userInfo);

await this.userServices.SaveUserToLocalAsync(userInfo);
WeakReferenceMessenger.Default.Send(new LoginMessage(userInfo));
await AppNavigator.GoBackAsync();
}
catch (Exception ex)
{
await AppNavigator.ShowSnackbarAsync(ex.Message);
throw;
}
}

Guard.IsNotNullOrWhiteSpace(accessToken);
var userInfo = await this.userServices.GetUserByAccessToken(accessToken);
[RelayCommand]
async Task SignUpAsync()
{
try
{
var authenticationAccessToken = await this.authenticationServices.CreatePrincipleUserInfo(PhoneNumber, UserName, Email, Password, "", "", "");
Guard.IsNotNullOrWhiteSpace(authenticationAccessToken);

var userInfo = await this.userServices.GetUserInfo(authenticationAccessToken);
Guard.IsNotNull(userInfo);

await this.userServices.SaveUserToLocalAsync(userInfo);
WeakReferenceMessenger.Default.Send(new LoginMessage(userInfo));
await AppNavigator.GoBackAsync();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using Refit;
using System.Net;

namespace MAUIsland;

public interface ITotechsIdentityAuthentication
public interface ITotechsIdentityAuthenticationRefit
{
[Post("/Access/Register")]
Task Register(string userName,
string password,
string firstName,
Task<HttpStatusCode> Register(string userName,
string password,
string firstName,
string lastName,
string email,
string phoneNumber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,56 @@

public class BogusAuthenticationService : IAuthenticationServices
{
public Task<string> SignIn(string userName, string password)

public Task<string> AuthenticateWithPhoneNumber(string phoneNumer, string password)
{
return Task.Run(() =>
{
return string.Empty;
var faker = new Faker();
return faker.System.ApplePushToken();
});
}

public Task<string> SignInWithPhoneNumber(string phoneNumer, string password)
public Task<string> SignUp(string authenticateAccessToken, string avatarUrl, string userName)
{
return Task.Run(() =>
{
//Send signup information to Totechs Identity
//Get accessToken from Totechs Identity
//Get User info from Totechs Identity using Totechs Identity AccessToken and save down to application data
//Send signup information to Totechs Intranet signup
//Get accessToken from Totechs Intranet
//Get User info from Totechs Intranet using Totechs Intranet AccessToken and save down to application data
var faker = new Faker();
return faker.System.ApplePushToken();
});
}

public Task<string> SignUp(string phoneNumber, string userName, string email, string password)
public Task<string> Authenticate(string username, string password)
{
return Task.Run(() =>
{
return string.Empty;
var faker = new Faker();
return faker.System.ApplePushToken();
});
}

public Task<PrincipalUserModel> GetPrincipleUserInfo(string userGuid, string authenticateAccessToken)
{
throw new NotImplementedException();
}

public Task<ServiceUserInfo> GetServiceUserInfo(string userGuid, string authenticateAccessToken)
{
throw new NotImplementedException();
}

public Task<string> CreatePrincipleUserInfo(string phoneNumber, string userName, string email, string password, string firstName, string lastName, string profilePicUrl)
{
return Task.Run(() =>
{
var faker = new Faker();
return faker.System.ApplePushToken();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public Task<UserModel> GetCurrentUser()
});
}

public Task<UserModel> GetUserByguid(string guid)
public Task<UserModel> GetUserByguid(string guid, string authenticationToken)
{
return Task.Run(() =>
{
Expand All @@ -38,7 +38,7 @@ public Task<UserModel> GetUserByguid(string guid)
});
}

public Task<UserModel> GetUserByAccessToken(string accesToken)
public Task<UserModel> GetUserInfo(string authenticationToken)
{
return Task.Run(() =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,59 @@
using Refit;
using System.Net;

namespace MAUIsland;

public class RefitAuthenticationService : IAuthenticationServices
{
private readonly ITotechsIdentityAuthentication totechsIdentityAuthentication;
private readonly ITotechsIdentityAuthenticationRefit totechsIdentityAuthenticationRefit;
private readonly IUserServices userServices;
private readonly IAppNavigator appNavigator;
#region [CTor]
public RefitAuthenticationService(ITotechsIdentityAuthentication totechsIdentityAuthentication, IAppNavigator appNavigator)
public RefitAuthenticationService(ITotechsIdentityAuthenticationRefit totechsIdentityAuthenticationRefit, IUserServices userServices, IAppNavigator appNavigator)
{
this.totechsIdentityAuthentication = totechsIdentityAuthentication;
this.totechsIdentityAuthenticationRefit = totechsIdentityAuthenticationRefit;
this.userServices = userServices;
this.appNavigator = appNavigator;
}
#endregion
public Task<string> SignIn(string userName, string password)

#region [Methods]


public async Task<string> Authenticate(string username, string password)
{
return Task.Run(() =>
{
return string.Empty;
}
);
var response = await this.totechsIdentityAuthenticationRefit.Login(username, password);
return response.AccessToken;
}

public async Task<string> SignInWithPhoneNumber(string phoneNumber, string password)
public async Task<string> AuthenticateWithPhoneNumber(string phoneNumer, string password)
{
try
{
var totechsIdentityInfo = await this.totechsIdentityAuthentication.LoginWithPhoneNumber(new PhoneNumberLoginDTO(phoneNumber, password));
var response = await this.totechsIdentityAuthenticationRefit.LoginWithPhoneNumber(new PhoneNumberLoginDTO(phoneNumer, password));
return response.AccessToken;
}

return string.Empty;
}
catch (ApiException ex)
public async Task<string> CreatePrincipleUserInfo(string phoneNumber, string userName, string email, string password, string firstName, string lastName, string profilePicUrl)
{
var response = await this.totechsIdentityAuthenticationRefit.Register(userName, password, firstName, lastName, email, phoneNumber, profilePicUrl, new string[] { "", "" });
if (response == HttpStatusCode.NoContent)
{
await this.appNavigator.ShowSnackbarAsync(ex.Message);
throw;
return string.Empty;
}
else return string.Empty;
}

public Task<string> SignUp(string phoneNumber, string userName, string email, string password)
public Task<PrincipalUserModel> GetPrincipleUserInfo(string userGuid, string authenticateAccessToken)
{
throw new NotImplementedException();
}

public Task<ServiceUserInfo> GetServiceUserInfo(string userGuid, string authenticateAccessToken)
{
throw new NotImplementedException();
}

public Task<string> SignUp(string authenticateAccessToken, string avatarUrl, string userName)
{
throw new NotImplementedException();
}
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

public interface IAuthenticationServices
{
Task<string> SignUp(string phoneNumber, string userName, string email, string password);
Task<string> SignIn(string userName, string password);
Task<string> SignInWithPhoneNumber(string phoneNumer, string password);
Task<string> Authenticate(string username, string password);
Task<string> CreatePrincipleUserInfo(string phoneNumber, string userName, string email, string password, string firstName, string lastName, string profilePicUrl);
Task<PrincipalUserModel> GetPrincipleUserInfo(string userGuid, string authenticateAccessToken);
Task<ServiceUserInfo> GetServiceUserInfo(string userGuid, string authenticateAccessToken);
Task<string> SignUp(string authenticateAccessToken, string avatarUrl, string userName);
Task<string> AuthenticateWithPhoneNumber(string phoneNumer, string password);
}
4 changes: 2 additions & 2 deletions src/Features/Chat/Services/Interfaces/IUserServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ public interface IUserServices
{
Task<UserModel> GetCurrentUser();

Task<UserModel> GetUserByguid(string guid);
Task<UserModel> GetUserByguid(string guid, string authenticationToken);

Task<UserModel> GetUserByAccessToken(string accesToken);
Task<UserModel> GetUserInfo(string authenticationToken);

Task SaveUserToLocalAsync(UserModel user);
}
10 changes: 5 additions & 5 deletions src/Features/Gallery/ContentViews/BrandIconContentView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
Source="{x:Binding ComponentData.IconUrl,
Source={x:Reference root}}"
WidthRequest="100">
<FlyoutBase.ContextFlyout>
<!--<FlyoutBase.ContextFlyout>
<MenuFlyout>
<MenuFlyoutItem
Clicked="DetailInNewWindow_Clicked"
IconImageSource="{x:StaticResource OpenNewWindowIcon}"
IsEnabled="False"
Text="Open in new window" />
</MenuFlyout>
</FlyoutBase.ContextFlyout>
</FlyoutBase.ContextFlyout>-->
</ImageButton>
<!--
Source="{x:Binding ComponentData.LottieUrl,
Expand All @@ -61,15 +61,15 @@
<skia:SKLottieView.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" />
</skia:SKLottieView.GestureRecognizers>
<FlyoutBase.ContextFlyout>
<!--<FlyoutBase.ContextFlyout>
<MenuFlyout>
<MenuFlyoutItem
Clicked="DetailInNewWindow_Clicked"
IconImageSource="{x:StaticResource OpenNewWindowIcon}"
IsEnabled="False"
IsEnabled="False"
Text="Open in new window" />
</MenuFlyout>
</FlyoutBase.ContextFlyout>
</FlyoutBase.ContextFlyout>-->
</skia:SKLottieView>
<Label
FontAttributes="Bold"
Expand Down
3 changes: 1 addition & 2 deletions src/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using CommunityToolkit.Maui;
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Storage;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Handlers;
using Refit;
using SkiaSharp.Views.Maui.Controls.Hosting;
Expand Down Expand Up @@ -62,7 +61,7 @@ static MauiAppBuilder RegisterPopups(this MauiAppBuilder builder)

static MauiAppBuilder RegisterRefitApi(this MauiAppBuilder builder)
{
builder.Services.AddRefitClient<ITotechsIdentityAuthentication>()
builder.Services.AddRefitClient<ITotechsIdentityAuthenticationRefit>()
.ConfigureHttpClient(c => c.BaseAddress = new Uri("https://totechsidentityprovider.azurewebsites.net/api"));
return builder;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Platforms/Windows/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap rescap">

<Identity Name="maui-package-name-placeholder" Publisher="CN=Strypper" Version="0.0.5.0" />
<Identity Name="maui-package-name-placeholder" Publisher="CN=Strypper" Version="0.0.7.0" />

<mp:PhoneIdentity PhoneProductId="AC4A1DA6-8E13-472D-AFB5-1EA1DDC289FB" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>

Expand Down

0 comments on commit 0fcb0e0

Please sign in to comment.