-
Notifications
You must be signed in to change notification settings - Fork 0
/
Handler.cs
83 lines (72 loc) · 3 KB
/
Handler.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
using System;
using Amazon.Lambda.Core;
using System.IO;
using System.Collections.Generic;
using System.Text.Json;
using Amazon.Lambda.ApplicationLoadBalancerEvents;
[assembly:LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
namespace Namespace
{
public class API
{
public ApplicationLoadBalancerResponse FunctionHandler(Stream input)
{
ApplicationLoadBalancerResponse response = null;
var request = new StreamReader(input).ReadToEnd();
var dict = JsonSerializer.Deserialize<Dictionary<string,object>>(request);
var requestHeaders = JsonSerializer.Deserialize<Dictionary<string,string>>(dict["headers"].ToString());
var authorization = "";
int contentLength = request.Length;
System.Console.WriteLine(request);
if (dict["httpMethod"].ToString()=="OPTIONS") {
Dictionary<string, string> corsHeaders = new Dictionary<string, string>();
corsHeaders.Add("Content-Type", "application/json");
corsHeaders.Add("Access-Control-Allow-Origin", "*");
corsHeaders.Add("Access-Control-Allow-Headers", "*");
corsHeaders.Add("Access-Control-Allow-Methods", "*");
var corsResponse = new ApplicationLoadBalancerResponse() {
IsBase64Encoded = false,
StatusCode = 200,
StatusDescription = "200 OK",
Headers = corsHeaders,
};
return corsResponse;
}
if (requestHeaders.ContainsKey("authorization"))
authorization = requestHeaders["authorization"];
// Get these from the Lambda ENV
var domain = Environment.GetEnvironmentVariable("AUTH_DOMAIN");
var audience = Environment.GetEnvironmentVariable("AUTH_AUDIENCE");
var permission = Environment.GetEnvironmentVariable("AUTH_PERMISSIONS");
var permissions = new List<string>(permission.Split(','));
// See if authorized
if (JWTAuthorizer.Authorizer.IsAuthorized(authorization, domain, audience, permissions))
{
var data = new Response { Message = "Hello, 123!", BodyLength = contentLength.ToString()};
Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Content-Type", "application/json");
headers.Add("Access-Control-Allow-Origin", "*");
response = new ApplicationLoadBalancerResponse() {
IsBase64Encoded = false,
StatusCode = 200,
StatusDescription = "200 OK",
Headers = headers,
Body = JsonSerializer.Serialize<Response>(data)
};
}
else
{
response = new ApplicationLoadBalancerResponse() {
IsBase64Encoded = false,
StatusCode = 403,
StatusDescription = "403 Unauthorized",
};
}
return response;
}
}
class Response {
public string Message { get; set; }
public string BodyLength { get; set; }
}
}