-
Notifications
You must be signed in to change notification settings - Fork 7
/
UAuthenticator.pas
140 lines (120 loc) · 3.19 KB
/
UAuthenticator.pas
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
unit UAuthenticator;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,GoogleOTP, Vcl.StdCtrls, Vcl.WinXCtrls,
Vcl.ExtCtrls;
type
TForm1 = class(TForm)
tmAtualizaToken: TTimer;
grTop: TGroupBox;
TsManual: TToggleSwitch;
lblinfo: TLabel;
grbase: TGroupBox;
btnGerarToken: TButton;
lblToken: TLabel;
edtSECRETKEYTOP: TEdit;
lblSuaKey: TLabel;
tsAutomatico: TToggleSwitch;
lblCodigo: TLabel;
btnValidar: TButton;
lblValido: TLabel;
procedure btnGerarTokenClick(Sender: TObject);
procedure edtSECRETKEYTOPExit(Sender: TObject);
procedure tsAutomaticoClick(Sender: TObject);
procedure TsManualClick(Sender: TObject);
procedure tmAtualizaTokenTimer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnValidarClick(Sender: TObject);
private
{ Private declarations }
var Fkey: String;
Fcodigo: integer;
function Validar( codigo : String) : String;
function SetOTP_SECRET_KEY(Key : String): String;
function GeraToken(OTPSECRET : String) : String;
public
{ Public declarations }
property key: String read FKey;
property codigo: integer read FCodigo;
procedure Create(); reintroduce;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.btnGerarTokenClick(Sender: TObject);
begin
lblToken.Caption := GeraToken(Fkey);
end;
procedure TForm1.btnValidarClick(Sender: TObject);
begin
lblValido.Caption:= Validar(IntToStr(Fcodigo));
end;
procedure TForm1.Create;
begin
/// Caso já tenha a key informada, sem ser usada no evento OnExit()
if not(edtSECRETKEYTOP.Text = EmptyStr) then
SetOTP_SECRET_KEY(edtSECRETKEYTOP.Text);
end;
procedure TForm1.edtSECRETKEYTOPExit(Sender: TObject);
begin
SetOTP_SECRET_KEY(edtSECRETKEYTOP.Text);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Create;
end;
function TForm1.GeraToken(OTPSECRET: String): String;
var
vToken : integer;
begin
if OTPSECRET.IsEmpty then
begin
tmAtualizaToken.Enabled:= false;
raise Exception.Create('Key não informada!');
end;
vToken := CalculateOTP(Fkey);
Fcodigo := vToken;
/// Formatação padrão Google
Result := copy(Format('%.6d',[vToken]),1,3) + ' ' + copy(Format('%.6d',[vToken]),4,6) ;
end;
function TForm1.SetOTP_SECRET_KEY(Key: String): String;
begin
Fkey := Key;
end;
procedure TForm1.tmAtualizaTokenTimer(Sender: TObject);
begin
lblToken.Caption := GeraToken(Fkey);
end;
procedure TForm1.tsAutomaticoClick(Sender: TObject);
begin
if tsAutomatico.State = tssOn then
edtSECRETKEYTOP.PasswordChar := '*'
else
edtSECRETKEYTOP.PasswordChar := #0 ;
end;
procedure TForm1.TsManualClick(Sender: TObject);
begin
if TsManual.State = tssOn then
begin
tmAtualizaToken.Enabled := True;
btnGerarToken.Visible := False;
end
else
begin
tmAtualizaToken.Enabled := False;
btnGerarToken.Visible := True;
end;
end;
function TForm1.Validar(codigo: String): String;
var
vCodigo: String;
begin
result := 'Código inválido';
vCodigo := codigo;
if ValidateTOPT(Fkey,vcodigo.ToInteger) then
result := 'Código Válido';
end;
end.