forked from winauth/winauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogleAuthenticator.cs
167 lines (145 loc) · 4.37 KB
/
GoogleAuthenticator.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright (C) 2011 Colin Mackie.
* This software is distributed under the terms of the GNU General Public License.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Macs;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Paddings;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Generators;
#if NUNIT
using NUnit.Framework;
#endif
#if NETCF
using OpenNETCF.Security.Cryptography;
#endif
namespace WinAuth
{
/// <summary>
/// Class that implements Google's authenticator
/// </summary>
public class GoogleAuthenticator : Authenticator
{
/// <summary>
/// Number of digits in code
/// </summary>
private const int CODE_DIGITS = 6;
/// <summary>
/// Number of minutes to ignore syncing if network error
/// </summary>
private const int SYNC_ERROR_MINUTES = 5;
/// <summary>
/// URL used to sync time
/// </summary>
private const string TIME_SYNC_URL = "http://www.google.com";
/// <summary>
/// Time of last Sync error
/// </summary>
private static DateTime _lastSyncError = DateTime.MinValue;
#region Authenticator data
public string Serial
{
get
{
return Base32.getInstance().Encode(SecretKey);
}
}
#endregion
/// <summary>
/// Create a new Authenticator object
/// </summary>
public GoogleAuthenticator()
: base(CODE_DIGITS)
{
}
/// <summary>
/// Enroll the authenticator with the server.
public void Enroll(string b32key)
{
SecretKey = Base32.getInstance().Decode(b32key);
Sync();
}
/// <summary>
/// Synchronise this authenticator's time with Google. We update our data record with the difference from our UTC time.
/// </summary>
public override void Sync()
{
// check if data is protected
if (this.SecretKey == null && this.EncryptedData != null)
{
throw new EncrpytedSecretDataException();
}
// don't retry for 5 minutes
if (_lastSyncError >= DateTime.Now.AddMinutes(0 - SYNC_ERROR_MINUTES))
{
return;
}
try
{
// we use the Header response field from a request to www.google.come
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(TIME_SYNC_URL);
request.Method = "GET";
request.ContentType = "text/html";
request.Timeout = 5000;
// get response
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
// OK?
if (response.StatusCode != HttpStatusCode.OK)
{
throw new ApplicationException(string.Format("{0}: {1}", (int)response.StatusCode, response.StatusDescription));
}
string headerdate = response.Headers["Date"];
if (string.IsNullOrEmpty(headerdate) == false)
{
DateTime dt;
if (DateTime.TryParse(headerdate, out dt) == true)
{
// get as ms since epoch
long dtms = Convert.ToInt64((dt.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalMilliseconds);
// get the difference between the server time and our current time
long serverTimeDiff = dtms - CurrentTime;
// update the Data object
ServerTimeDiff = serverTimeDiff;
LastServerTime = DateTime.Now.Ticks;
}
}
// clear any sync error
_lastSyncError = DateTime.MinValue;
}
}
catch (WebException )
{
// don't retry for a while after error
_lastSyncError = DateTime.Now;
// set to zero to force reset
ServerTimeDiff = 0;
}
}
}
}