From a56cf00b486ef88fc346a43f2bd783264290bf7f Mon Sep 17 00:00:00 2001 From: Peter Donker Date: Thu, 24 Sep 2020 22:09:21 +0200 Subject: [PATCH] Purge Expired JWT tokens task --- .../Schedule/PurgeExpiredTokensTask.cs | 52 +++++++++++++++++++ .../Dnn.AuthServices.Jwt/Data/DataService.cs | 2 +- .../Data/Scripts/09.08.00.SqlDataProvider | 42 +++++++++++++++ .../Data/Scripts/Uninstall.SqlDataProvider | 3 ++ .../Dnn.AuthServices.Jwt.csproj | 2 + DNN Platform/Dnn.AuthServices.Jwt/Dnn.Jwt.dnn | 7 ++- 6 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 DNN Platform/Dnn.AuthServices.Jwt/Components/Schedule/PurgeExpiredTokensTask.cs create mode 100644 DNN Platform/Dnn.AuthServices.Jwt/Data/Scripts/09.08.00.SqlDataProvider diff --git a/DNN Platform/Dnn.AuthServices.Jwt/Components/Schedule/PurgeExpiredTokensTask.cs b/DNN Platform/Dnn.AuthServices.Jwt/Components/Schedule/PurgeExpiredTokensTask.cs new file mode 100644 index 00000000000..2c329d7a3e7 --- /dev/null +++ b/DNN Platform/Dnn.AuthServices.Jwt/Components/Schedule/PurgeExpiredTokensTask.cs @@ -0,0 +1,52 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information + +namespace Dnn.AuthServices.Jwt.Components.Schedule +{ + using System; + + using Dnn.AuthServices.Jwt.Data; + using DotNetNuke.Instrumentation; + using DotNetNuke.Services.Exceptions; + using DotNetNuke.Services.Scheduling; + + /// + /// Scheduled task to delete tokens that linger in the database after having expired + /// + public class PurgeExpiredTokensTask : SchedulerClient + { + private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(PurgeExpiredTokensTask)); + + /// + /// Initializes a new instance of the class. + /// + /// The object used to record the results from this task + public PurgeExpiredTokensTask(ScheduleHistoryItem objScheduleHistoryItem) + { + this.ScheduleHistoryItem = objScheduleHistoryItem; + } + + /// + /// Runs when the task is triggered by DNN. + /// + public override void DoWork() + { + try + { + Logger.Info("Starting PurgeExpiredTokensTask"); + DataService.Instance.DeleteExpiredTokens(); + Logger.Info("Finished PurgeExpiredTokensTask"); + this.ScheduleHistoryItem.Succeeded = true; + } + catch (Exception exc) + { + this.ScheduleHistoryItem.Succeeded = false; + this.ScheduleHistoryItem.AddLogNote(string.Format("Purging expired tokens task failed: {0}.", exc.ToString())); + this.Errored(ref exc); + Logger.ErrorFormat("Error in PurgeExpiredTokensTask: {0}. {1}", exc.Message, exc.StackTrace); + Exceptions.LogException(exc); + } + } + } +} diff --git a/DNN Platform/Dnn.AuthServices.Jwt/Data/DataService.cs b/DNN Platform/Dnn.AuthServices.Jwt/Data/DataService.cs index fd919cd91e0..f42faaecd62 100644 --- a/DNN Platform/Dnn.AuthServices.Jwt/Data/DataService.cs +++ b/DNN Platform/Dnn.AuthServices.Jwt/Data/DataService.cs @@ -72,7 +72,7 @@ public virtual void DeleteUserTokens(int userId) public virtual void DeleteExpiredTokens() { - // don't worry aabout caching; these will already be invalidated by cache manager + // don't worry about caching; these will already be invalidated by cache manager this._dataProvider.ExecuteNonQuery("JsonWebTokens_DeleteExpired"); } diff --git a/DNN Platform/Dnn.AuthServices.Jwt/Data/Scripts/09.08.00.SqlDataProvider b/DNN Platform/Dnn.AuthServices.Jwt/Data/Scripts/09.08.00.SqlDataProvider new file mode 100644 index 00000000000..210651c1184 --- /dev/null +++ b/DNN Platform/Dnn.AuthServices.Jwt/Data/Scripts/09.08.00.SqlDataProvider @@ -0,0 +1,42 @@ +/* Add Schedule Job */ +/*************************************/ +IF NOT EXISTS(SELECT 1 FROM {databaseOwner}{objectQualifier}Schedule WHERE TypeFullName = 'Dnn.AuthServices.Jwt.Components.Schedule.PurgeExpiredTokensTask, Dnn.AuthServices.Jwt') +BEGIN + INSERT INTO {databaseOwner}{objectQualifier}Schedule + ( [TypeFullName] + ,[TimeLapse] + ,[TimeLapseMeasurement] + ,[RetryTimeLapse] + ,[RetryTimeLapseMeasurement] + ,[RetainHistoryNum] + ,[AttachToEvent] + ,[CatchUpEnabled] + ,[Enabled] + ,[ObjectDependencies] + ,[Servers] + ,[CreatedByUserID] + ,[CreatedOnDate] + ,[LastModifiedByUserID] + ,[LastModifiedOnDate] + ,[FriendlyName] + ) VALUES + ( 'Dnn.AuthServices.Jwt.Components.Schedule.PurgeExpiredTokensTask, Dnn.AuthServices.Jwt' + ,1 + ,N'd' + ,6 + ,N'h' + ,10 + ,N'' + ,0 + ,1 + ,N'' + ,NULL + ,NULL + ,NULL + ,NULL + ,NULL + ,N'Purge Expired JWT Tokens' + ) +END +GO + diff --git a/DNN Platform/Dnn.AuthServices.Jwt/Data/Scripts/Uninstall.SqlDataProvider b/DNN Platform/Dnn.AuthServices.Jwt/Data/Scripts/Uninstall.SqlDataProvider index f3cb2eed269..7ee08a2def9 100644 --- a/DNN Platform/Dnn.AuthServices.Jwt/Data/Scripts/Uninstall.SqlDataProvider +++ b/DNN Platform/Dnn.AuthServices.Jwt/Data/Scripts/Uninstall.SqlDataProvider @@ -42,6 +42,9 @@ IF OBJECT_ID(N'{databaseOwner}{objectQualifier}JsonWebTokens', N'U') IS NOT NULL DROP TABLE {databaseOwner}[{objectQualifier}JsonWebTokens] GO +DELETE FROM {databaseOwner}{objectQualifier}Schedule WHERE TypeFullName = 'Dnn.AuthServices.Jwt.Components.Schedule.PurgeExpiredTokensTask, Dnn.AuthServices.Jwt' +GO + /************************************************************/ /***** SqlDataProvider *****/ /************************************************************/ \ No newline at end of file diff --git a/DNN Platform/Dnn.AuthServices.Jwt/Dnn.AuthServices.Jwt.csproj b/DNN Platform/Dnn.AuthServices.Jwt/Dnn.AuthServices.Jwt.csproj index f048833cd07..b8b348b859c 100644 --- a/DNN Platform/Dnn.AuthServices.Jwt/Dnn.AuthServices.Jwt.csproj +++ b/DNN Platform/Dnn.AuthServices.Jwt/Dnn.AuthServices.Jwt.csproj @@ -73,6 +73,7 @@ + @@ -84,6 +85,7 @@ stylecop.json + Designer diff --git a/DNN Platform/Dnn.AuthServices.Jwt/Dnn.Jwt.dnn b/DNN Platform/Dnn.AuthServices.Jwt/Dnn.Jwt.dnn index 3cb0a361cf6..78d3432dbaa 100644 --- a/DNN Platform/Dnn.AuthServices.Jwt/Dnn.Jwt.dnn +++ b/DNN Platform/Dnn.AuthServices.Jwt/Dnn.Jwt.dnn @@ -1,6 +1,6 @@ - + DNN JWT Auth Handler DNN Json Web Token Authentication (JWT) library for cookie-less Mobile authentication clients @@ -37,6 +37,11 @@ 01.00.00.SqlDataProvider 01.00.00 +