-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphAPI.cs
116 lines (98 loc) · 4.5 KB
/
GraphAPI.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
using Newtonsoft.Json;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace SampleCertCall
{
class GraphAPI
{
public static async Task<HttpStatusCode> AddKeyWithPassword(string poP, string objectId, string api, string accessToken, string key, string password)
{
var client = new HttpClient();
var url = $"{api}/{objectId}/addKey";
var defaultRequestHeaders = client.DefaultRequestHeaders;
if (defaultRequestHeaders.Accept == null || !defaultRequestHeaders.Accept.Any(m => m.MediaType == "application/json"))
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
defaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var payload = new
{
keyCredential = new
{
type = "X509CertAndPassword",
usage = "Sign",
key,
},
passwordCredential = new
{
secretText = password,
},
proof = poP
};
var stringPayload = JsonConvert.SerializeObject(payload);
var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
var res = await client.PostAsync(url, httpContent);
return res.StatusCode;
}
public static async Task<HttpStatusCode> AddKey(string poP, string objectId, string api, string accessToken, string key)
{
var client = new HttpClient();
var url = $"{api}/{objectId}/addKey";
var defaultRequestHeaders = client.DefaultRequestHeaders;
if (defaultRequestHeaders.Accept == null || !defaultRequestHeaders.Accept.Any(m => m.MediaType == "application/json"))
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
defaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
string pass = null;
var payload = new
{
keyCredential = new
{
type = "AsymmetricX509Cert",
usage = "Verify",
key,
},
passwordCredential = pass,
proof = poP
};
var stringPayload = JsonConvert.SerializeObject(payload);
var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
var res = await client.PostAsync(url, httpContent);
return res.StatusCode;
}
public static async Task<HttpStatusCode> RemoveKeyAsync(string poP, string objectId, string api, string keyId, string accessToken)
{
var client = new HttpClient();
var url = $"{api}/{objectId}/removeKey";
var defaultRequestHeaders = client.DefaultRequestHeaders;
if (defaultRequestHeaders.Accept == null || !defaultRequestHeaders.Accept.Any(m => m.MediaType == "application/json"))
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
defaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var payload = new
{
keyId,
proof = poP
};
var stringPayload = JsonConvert.SerializeObject(payload);
var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
var res = await client.PostAsync(url, httpContent);
var contents = await res.Content.ReadAsStringAsync();
if (res.Content.ReadAsStringAsync().Result.Contains("No credentials found to be removed"))
{
throw new HttpRequestException("CertID Not Found", new HttpRequestException(contents, null, res.StatusCode));
}
if (res.Content.ReadAsStringAsync().Result.Contains("Access Token missing or malformed"))
{
throw new HttpRequestException("proof-of-possession (PoP) token is invalid", new HttpRequestException(contents, null, res.StatusCode));
}
return res.StatusCode;
}
}
}