-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathAcmeAccount.cs
More file actions
234 lines (204 loc) · 7.26 KB
/
AcmeAccount.cs
File metadata and controls
234 lines (204 loc) · 7.26 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Waher.Content;
using Waher.Content.Xml;
namespace Waher.Security.ACME
{
/// <summary>
/// ACME Account status enumeration
/// </summary>
public enum AcmeAccountStatus
{
/// <summary>
/// Account is valid
/// </summary>
valid,
/// <summary>
/// Client has deactivated account
/// </summary>
deactivated,
/// <summary>
/// Server has deactivated account
/// </summary>
revoked
};
/// <summary>
/// Represents an ACME account.
/// </summary>
public class AcmeAccount : AcmeResource
{
private readonly AcmeAccountStatus status;
private readonly string[] contact = null;
private readonly string initialIp = null;
private readonly Uri orders = null;
private readonly DateTime? createdAt = null;
private readonly bool? termsOfServiceAgreed = null;
internal AcmeAccount(AcmeClient Client, Uri Location, IEnumerable<KeyValuePair<string, object>> Obj)
: base(Client, Location, Location)
{
foreach (KeyValuePair<string, object> P in Obj)
{
switch (P.Key)
{
case "status":
if (!Enum.TryParse(P.Value as string, out this.status))
throw new ArgumentException("Invalid ACME account status: " + P.Value.ToString(), "status");
break;
case "contact":
if (P.Value is Array A)
{
List<string> Contact = new List<string>();
foreach (object Obj2 in A)
{
if (Obj2 is string s)
Contact.Add(s);
}
this.contact = Contact.ToArray();
}
break;
case "termsOfServiceAgreed":
if (CommonTypes.TryParse(P.Value as string, out bool b))
this.termsOfServiceAgreed = b;
else
throw new ArgumentException("Invalid boolean value.", "termsOfServiceAgreed");
break;
case "orders":
this.orders = new Uri(P.Value as string);
break;
case "initialIp":
this.initialIp = P.Value as string;
break;
case "createdAt":
if (XML.TryParse(P.Value as string, out DateTime TP))
this.createdAt = TP;
else
throw new ArgumentException("Invalid date and time value.", "createdAt");
break;
}
}
}
/// <summary>
/// Optional array of URLs that the server can use to contact the client for issues related
/// to this account.
/// </summary>
public string[] Contact => this.contact;
/// <summary>
/// The status of this account.
/// </summary>
public AcmeAccountStatus Status => this.status;
/// <summary>
/// Including this field in a new-account request, with a value of true, indicates the client's
/// agreement with the terms of service.This field is not updateable by the client.
/// </summary>
public bool? TermsOfServiceAgreed => this.termsOfServiceAgreed;
/// <summary>
/// A URL from which a list of orders submitted by this account can be fetched via a GET request
/// </summary>
public Uri Orders => this.orders;
/// <summary>
/// Initial IP address.
/// </summary>
public string InitialIp => this.initialIp;
/// <summary>
/// Date and time of creation, if available.
/// </summary>
public DateTime? CreatedAt => this.createdAt;
/// <summary>
/// Updates the account.
/// </summary>
/// <param name="Contact">New contact information.</param>
/// <returns>New account object.</returns>
public Task<AcmeAccount> Update(string[] Contact)
{
return this.Client.UpdateAccount(this.Location, Contact);
}
/// <summary>
/// Deactivates the account.
/// </summary>
/// <returns>New account object.</returns>
public Task<AcmeAccount> Deactivate()
{
return this.Client.DeactivateAccount(this.Location);
}
/// <summary>
/// Creates a new key for the account.
/// </summary>
/// <returns>New account.</returns>
public Task<AcmeAccount> NewKey()
{
return this.Client.NewKey(this.Location);
}
/// <summary>
/// Orders certificate.
/// </summary>
/// <param name="Domains">Domain names to include in certificate.</param>
/// <param name="NotBefore">If provided, certificate is not valid before this point in time.</param>
/// <param name="NotAfter">If provided, certificate is not valid after this point in time.</param>
/// <returns>ACME order object.</returns>
public Task<AcmeOrder> OrderCertificate(string[] Domains, DateTime? NotBefore, DateTime? NotAfter)
{
int i, c = Domains.Length;
AcmeIdentifier[] Identifiers = new AcmeIdentifier[c];
for (i = 0; i < c; i++)
Identifiers[i] = new AcmeIdentifier(this.Client, "dns", Domains[i]);
return this.OrderCertificate(Identifiers, NotBefore, NotAfter);
}
/// <summary>
/// Orders certificate.
/// </summary>
/// <param name="Domain">Domain name to include in certificate.</param>
/// <param name="NotBefore">If provided, certificate is not valid before this point in time.</param>
/// <param name="NotAfter">If provided, certificate is not valid after this point in time.</param>
/// <returns>ACME order object.</returns>
public Task<AcmeOrder> OrderCertificate(string Domain, DateTime? NotBefore, DateTime? NotAfter)
{
return this.OrderCertificate("dns", Domain, NotBefore, NotAfter);
}
/// <summary>
/// Orders certificate.
/// </summary>
/// <param name="Type">Type of identifier to include in the certificate.</param>
/// <param name="Value">Value of identifier to include in the certifiate.</param>
/// <param name="NotBefore">If provided, certificate is not valid before this point in time.</param>
/// <param name="NotAfter">If provided, certificate is not valid after this point in time.</param>
/// <returns>ACME order object.</returns>
public Task<AcmeOrder> OrderCertificate(string Type, string Value, DateTime? NotBefore, DateTime? NotAfter)
{
return this.OrderCertificate(new AcmeIdentifier(this.Client, Type, Value), NotBefore, NotAfter);
}
/// <summary>
/// Orders certificate.
/// </summary>
/// <param name="Identifier">Identifier to include in the certificate.</param>
/// <param name="NotBefore">If provided, certificate is not valid before this point in time.</param>
/// <param name="NotAfter">If provided, certificate is not valid after this point in time.</param>
/// <returns>ACME order object.</returns>
public Task<AcmeOrder> OrderCertificate(AcmeIdentifier Identifier,
DateTime? NotBefore, DateTime? NotAfter)
{
return this.OrderCertificate(new AcmeIdentifier[] { Identifier }, NotBefore, NotAfter);
}
/// <summary>
/// Orders certificate.
/// </summary>
/// <param name="Identifiers">Identifiers to include in the certificate.</param>
/// <param name="NotBefore">If provided, certificate is not valid before this point in time.</param>
/// <param name="NotAfter">If provided, certificate is not valid after this point in time.</param>
/// <returns>ACME order object.</returns>
public Task<AcmeOrder> OrderCertificate(AcmeIdentifier[] Identifiers,
DateTime? NotBefore, DateTime? NotAfter)
{
return this.Client.OrderCertificate(this.Location, Identifiers, NotBefore, NotAfter);
}
/// <summary>
/// Gets the list of current orders.
/// </summary>
public Task<AcmeOrder[]> GetOrders()
{
if (this.orders is null)
throw new Exception("Account object did not report a URI for the list of orders.");
return this.Client.GetOrders(this.Location, this.orders);
}
}
}