-
Notifications
You must be signed in to change notification settings - Fork 0
/
AzureGermlin.cs
100 lines (85 loc) · 3.47 KB
/
AzureGermlin.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web.Http;
using Gremlin.Net.Driver;
using Gremlin.Net.Structure.IO.GraphSON;
using Grpc.Core.Logging;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Newtonsoft.Json;
namespace FireBaseToken
{
public class AzureResult
{
public int status { get; set; }
public object result { get; set; }
}
public class GraphParameter
{
public string Name { get; set; }
public string Value { get; set; }
}
public class GraphQuery
{
public string Query { get; set; }
public List<GraphParameter> Parameters { get; set; }
}
public static class AzureGermlinAppFunction
{
const string YOUR_AZURE_GERMLIN_DB_PASSWORD = "YOUR_AZURE_GERMLIN_DB_PASSWORD";
const string YOU_AZURE_GERMLIN_DB_HOST = "YOU_AZURE_GERMLIN_DB_HOST";
const string YOU_AZURE_GERMLIN_DB_USERNAME = "YOU_AZURE_GERMLIN_DB_USERNAME";
[FunctionName("AzureGermlin")]
public static async Task<AzureResult> AzureGermlin(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
try
{
var tdate = req.Headers["x-ms-date"].FirstOrDefault();
var reqDate = Convert.ToDateTime(tdate);
if (reqDate < DateTime.UtcNow.AddMinutes(-10))
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
var auth = req.Headers["Authorization"].FirstOrDefault();
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var data = JsonConvert.DeserializeObject<GraphQuery>(requestBody);
var he = $"POST{data.Parameters.Count}AzureGermlin{tdate}";
string password = $"{YOUR_AZURE_GERMLIN_DB_PASSWORD}";
var hmacSha256 = new System.Security.Cryptography.HMACSHA256
{
Key = Convert.FromBase64String(password)
};
byte[] hashPayLoad = hmacSha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(he));
string signature = Convert.ToBase64String(hashPayLoad);
if (signature != auth)
{
throw new HttpResponseException(HttpStatusCode.Unauthorized);
}
data.Parameters.ForEach(t =>
{
data.Query = data.Query.Replace(t.Name, t.Value);
});
var gremlinServer = new GremlinServer(YOU_AZURE_GERMLIN_DB_HOST, 443, enableSsl: true,
username: YOU_AZURE_GERMLIN_DB_USERNAME,
password: password);
using (var gremlinClient = new GremlinClient(gremlinServer,
new GraphSON2Reader(), new GraphSON2Writer(), GremlinClient.GraphSON2MimeType))
{
var resultSet = await gremlinClient.SubmitAsync<dynamic>(data.Query);
return new AzureResult() { status = 200, result = resultSet };
}
}
catch (Exception ex)
{
return new AzureResult() { status = 500, result = ex.Message };
}
}
}
}