2 changed files
WPE.App.Authentication.Maui | ||
Business | ||
NativeAuthenticationStateProvider.cs | ||
WPE.App.Authentication.Maui.csproj | ||
NativeAuthenticationStateProvider.cs
/WPE.App.Authentication.Maui/Business/NativeAuthenticationStateProvider.cs-9+31/WPE.App.Authentication.Maui/Business/NativeAuthenticationStateProvider.cs
Add comment 10 namespace Infsoft.WPE.App.Authentication.Maui.Business
Add comment 11 {
Add comment 12 internal class NativeAuthenticationStateProvider(ITokenManager TokenManager, IAuthClient AuthClient,
Add comment 13 Minus ILogger<NativeAuthenticationStateProvider> logger, IPlatformPreferenceService platformPreferenceService) : AppAuthenticationStateProvider(logger, platformPreferenceService)
Add comment 13 Plus ILogger<NativeAuthenticationStateProvider> logger, IPlatformPreferenceService platformPreferenceService) : AppAuthenticationStateProvider(logger, platformPreferenceService), IAsyncDisposable
Add comment 14 {
Add comment 15 Plus private Timer? Timer { get; set; }
Add comment 16 Plus
Add comment 15 17 public override async Task<AuthenticationState> GetAuthenticationStateAsync()
Add comment 16 18 {
Add comment 17 19 var user = await GetUser();
Add comment 19 21
Add comment 20 22 if (user.Identity != null && user.Identity.IsAuthenticated && user.Identity.AuthenticationType != "anonymous")
Add comment 21 23 {
Add comment 24 Plus if (Timer is not null)
Add comment 25 Plus {
Add comment 22 26 Logger.LogInformation("starting background check..");
Add comment 23 Minus Timer? timer = null;
Add comment 24 Minus
Add comment 25 Minus timer = new Timer(async _ =>
Add comment 27 Plus Timer = new Timer(async _ =>
Add comment 26 28 {
Add comment 27 29 var currentUser = await GetUser(false);
Add comment 28 30 if (currentUser.Identity?.IsAuthenticated == false)
Add comment 29 31 {
Add comment 30 32 Logger.LogInformation("user logged out");
Add comment 31 33 NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(currentUser)));
Add comment 32 Minus await timer!.DisposeAsync();
Add comment 34 Plus await StopTimer();
Add comment 35 Plus }
Add comment 36 Plus }, null, dueTime: TimeSpan.FromMinutes(5), period: TimeSpan.FromMinutes(5));
Add comment 33 37 }
Add comment 34 Minus }, null, 1000, 30000);
Add comment 35 38 }
Add comment 39 Plus else
Add comment 40 Plus await StopTimer();
Add comment 36 41
Add comment 37 42 return state;
Add comment 38 43 }
Add comment 39 44
Add comment 45 Plus private async Task StopTimer()
Add comment 46 Plus {
Add comment 47 Plus if (Timer is null) return;
Add comment 48 Plus
Add comment 49 Plus await Timer.DisposeAsync();
Add comment 50 Plus Timer = null;
Add comment 51 Plus }
Add comment 52 Plus
Add comment 53 Plus
Add comment 40 54 protected override Task SignIn(LoginType loginType, string emailOrVoucherCode)
Add comment 41 55 {
Add comment 42 56 var loginTask = SignInWithSilent();
Add comment 108 122 if (useCache)
Add comment 109 123 {
Add comment 110 124 if (cachedUser.Identity?.AuthenticationType == "anonymous")
Add comment 125 Plus {
Add comment 126 Plus Logger.LogDebug("Taking anonymous user from cache");
Add comment 111 127 return cachedUser;
Add comment 112 Minus else if (now < userLastCheck + UserCacheRefreshInterval)
Add comment 128 Plus }
Add comment 129 Plus else if (now < userLastCheck.Add(UserCacheRefreshInterval))
Add comment 113 130 {
Add comment 114 131 Logger.LogDebug("Taking user from cache");
Add comment 115 132 return cachedUser;
Add comment 125 142
Add comment 126 143 private async Task<ClaimsPrincipal> ExtractUser()
Add comment 127 144 {
Add comment 128 Minus Logger.LogInformation("Querying user information from userInfo endpoint");
Add comment 145 Plus Logger.LogDebug("Querying user information from userInfo endpoint");
Add comment 129 146 var token = await TokenManager.GetAccessToken();
Add comment 130 147 if (token is null)
Add comment 131 148 return NoUser;
Add comment 133 150 var profile = await AuthClient.GetUserInfo(token);
Add comment 134 151 if (profile.IsError)
Add comment 135 152 {
Add comment 136 Minus Logger.LogDebug("Error on fetching profile information: {Error}, {Description}", profile.Error, profile.ErrorDescription);
Add comment 153 Plus Logger.LogError("Error on fetching profile information: {Error}, {Description}", profile.Error, profile.ErrorDescription);
Add comment 137 154 return NoUser;
Add comment 138 155 }
Add comment 139 156 var identity = new ClaimsIdentity(
Add comment 145 162
Add comment 146 163 return new ClaimsPrincipal(identity);
Add comment 147 164 }
Add comment 165 Plus
Add comment 166 Plus public async ValueTask DisposeAsync()
Add comment 167 Plus {
Add comment 168 Plus await StopTimer();
Add comment 169 Plus }
Add comment 148 170 }
Add comment 149 171 }
Add comment 150 172
WPE.App.Authentication.Maui.csproj
/WPE.App.Authentication.Maui/WPE.App.Authentication.Maui.csproj-1+1/WPE.App.Authentication.Maui/WPE.App.Authentication.Maui.csproj
Add comment 24 <PackageIcon>icon.png</PackageIcon>
Add comment 25 <PackageReadmeFile>ReadMe.md</PackageReadmeFile>
Add comment 26 <PackageTags>WPE;Workplace Experience;infsoft</PackageTags>
Add comment 27 Minus <Version>0.3.9</Version>
Add comment 27 Plus <Version>0.3.10</Version>
Add comment 28 <GenerateDocumentationFile>true</GenerateDocumentationFile>
Add comment 29 </PropertyGroup>
Add comment 30