-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtoken.cs
More file actions
executable file
·121 lines (96 loc) · 3.44 KB
/
Copy pathtoken.cs
File metadata and controls
executable file
·121 lines (96 loc) · 3.44 KB
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
using System;
using System.IO;
using System.Net;
using System.Xml;
namespace PardotAPI
{
public class token
{
// ----------------------------------
// --------- properties -------------
// ----------------------------------
private string _email;
private string _password;
private string _url = "/login/version/3";
private double _expiration = 1;
private string _hash;
private DateTime _timestamp;
private string _user_key;
private string _status = "Token not instantiated.";
// ----------------------------------
// --------- instantiation ----------
// ----------------------------------
public token(string thisEmail, string thisPassword, string thisUserKey)
{
// set private properties from instantiation
_email = thisEmail;
_password = thisPassword;
_user_key = thisUserKey;
// prepend API url to login url
_url = GlobalConstants.pardotAPIUrl + _url;
// call create method to get security token
create();
}
// ---------------------------------------------------------
// ------- getters & setters for private properties --------
// ---------------------------------------------------------
public string hash
{
get
{
// if either _hash or _timestamp property are null, or token timestamp has expired (expires after 60 minutes)
if ( !is_valid() )
{
// call create again
create();
}
return _hash;
}
}
public DateTime timestamp
{
get
{
return _timestamp;
}
}
public string user_key
{
get
{
return _user_key;
}
}
// ---------------------------------------------------
// ------------ private & public methods -------------
// ---------------------------------------------------
private void create()
{
// create post data string
string message = string.Format("email={0}&password={1}&user_key={2}", _email, _password, _user_key);
PardotAPI.request request = new PardotAPI.request(_url, message, "authentication");
// validate this was successful
if ( request.response.isValid() )
{
// get the api_key from the response
XmlNode key = request.response.xml.SelectSingleNode("/rsp/api_key");
// set the hash and timestamp
_hash = key.InnerText;
_timestamp = DateTime.Now;
_status = "Token created successfully.";
}
else
{
_status = "Authentication failed. Response from server: " + request.response.message;
}
}
public bool is_valid()
{
if ( _hash != null && _timestamp != null && DateTime.Compare(DateTime.Now, _timestamp.AddHours(_expiration)) < 0)
{
return true;
}
return false;
}
}
}