-
Notifications
You must be signed in to change notification settings - Fork 9
/
AuthService.cs
64 lines (61 loc) · 1.82 KB
/
AuthService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using io.harness.cfsdk.client.connector;
using io.harness.cfsdk.HarnessOpenAPIService;
using Serilog;
using System.Threading;
using System.Threading.Tasks;
namespace io.harness.cfsdk.client.api
{
interface IAuthCallback
{
void OnAuthenticationSuccess();
}
interface IAuthService
{
void Start();
void Stop();
}
/// <summary>
/// The class is in charge of initiating authentication requests and retry until successful authentication.
/// </summary>
internal class AuthService : IAuthService
{
private IConnector connector;
private Config config;
private Timer authTimer;
private IAuthCallback callback;
public AuthService(IConnector connector, Config config, IAuthCallback callback)
{
this.connector = connector;
this.config = config;
this.callback = callback;
}
public void Start()
{
// initiate authentication
authTimer = new Timer(new TimerCallback(OnTimedEvent), null, 0, this.config.PollIntervalInMiliSeconds);
}
public void Stop()
{
if (authTimer != null)
{
authTimer.Dispose();
authTimer = null;
}
}
private void OnTimedEvent(object source)
{
try
{
connector.Authenticate();
callback.OnAuthenticationSuccess();
Stop();
Log.Information("Stopping authentication service");
}
catch
{
// Exception thrown on Authentication. Timer will retry authentication.
Log.Error($"Exception while authenticating, retry in {this.config.pollIntervalInSeconds}");
}
}
}
}