Skip to content

Commit

Permalink
Purge Expired JWT tokens task (#4126)
Browse files Browse the repository at this point in the history
  • Loading branch information
donker authored Sep 24, 2020
1 parent 1c82b05 commit 20ea0d3
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Scheduled task to delete tokens that linger in the database after having expired
/// </summary>
public class PurgeExpiredTokensTask : SchedulerClient
{
private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(PurgeExpiredTokensTask));

/// <summary>
/// Initializes a new instance of the <see cref="PurgeExpiredTokensTask"/> class.
/// </summary>
/// <param name="objScheduleHistoryItem">The object used to record the results from this task</param>
public PurgeExpiredTokensTask(ScheduleHistoryItem objScheduleHistoryItem)
{
this.ScheduleHistoryItem = objScheduleHistoryItem;
}

/// <summary>
/// Runs when the task is triggered by DNN.
/// </summary>
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);
}
}
}
}
2 changes: 1 addition & 1 deletion DNN Platform/Dnn.AuthServices.Jwt/Data/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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 *****/
/************************************************************/
2 changes: 2 additions & 0 deletions DNN Platform/Dnn.AuthServices.Jwt/Dnn.AuthServices.Jwt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<Compile Include="Components\Entity\LoginData.cs" />
<Compile Include="Components\Entity\PersistedToken.cs" />
<Compile Include="Components\Entity\RenewalDto.cs" />
<Compile Include="Components\Schedule\PurgeExpiredTokensTask.cs" />
<Compile Include="Data\IDataService.cs" />
<Compile Include="Data\DataService.cs" />
<Compile Include="Components\Entity\LoginResultData.cs" />
Expand All @@ -84,6 +85,7 @@
<AdditionalFiles Include="..\..\stylecop.json">
<Link>stylecop.json</Link>
</AdditionalFiles>
<Content Include="Data\Scripts\09.08.00.SqlDataProvider" />
<None Include="Dnn.Jwt.dnn">
<SubType>Designer</SubType>
</None>
Expand Down
7 changes: 6 additions & 1 deletion DNN Platform/Dnn.AuthServices.Jwt/Dnn.Jwt.dnn
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<dotnetnuke type="Package" version="5.0">
<packages>
<package name="DNNJWT" type="Provider" version="09.07.02">
<package name="DNNJWT" type="Provider" version="09.08.00">
<friendlyName>DNN JWT Auth Handler</friendlyName>
<description>DNN Json Web Token Authentication (JWT) library for cookie-less Mobile authentication clients</description>
<dependencies/>
Expand Down Expand Up @@ -37,6 +37,11 @@
<name>01.00.00.SqlDataProvider</name>
<version>01.00.00</version>
</script>
<script type="Install">
<path>Data\Scripts</path>
<name>09.08.00.SqlDataProvider</name>
<version>09.08.00</version>
</script>
<script type="UnInstall">
<path>Data\Scripts</path>
<name>Uninstall.SqlDataProvider</name>
Expand Down

0 comments on commit 20ea0d3

Please sign in to comment.