-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.rb
146 lines (115 loc) · 3.51 KB
/
function.rb
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
# frozen_string_literal: true
require 'json'
require 'jwt'
require 'pp'
def main(event:, context:)
keys = event["headers"].keys
for item in keys
if item.casecmp?("content-type")
event["headers"]["content-type"] = event["headers"][item]
end
if item.casecmp?("authorization")
event["headers"]["authorization"] = event["headers"][item]
end
end
print "event: "
print event
print "\n\n"
case event["httpMethod"]
when "GET"
begin
# print "event[path] : "
# print event["path"]
# print "\n\n"
if event["path"] == "/token"
return response(body: nil, status: 405)
end
if event["path"] != "/"
return response(body: nil, status: 404)
end
if( event["headers"]["authorization"].split(" ")[0] != "Bearer" )
return response(body: nil, status: 403)
end
token = event["headers"]["authorization"].split(" ")[1]
payload = JWT.decode(token, "ITSASECRET")
rescue JWT::ImmatureSignature, JWT::ExpiredSignature => e
return response(body:nil, status: 401)
rescue JWT::DecodeError => e
return response(body: e, status: 403 )
rescue
return response(body: nil, status: 403)
else
# print "payload: "
# print payload
# print "\n\n\n"
# print "expiry: "
# print payload[0]["exp"]
# print "\n\n\n"
# if Time.now.to_i > payload[0]["exp"]
# return response(body: nil, status: 401)
# end
return response(body: payload[0]["data"], status: 200)
end
when "POST"
if event["path"] == "/"
return response(body: nil, status: 405)
end
if event["headers"]["content-type"] != "application/json"
return response(body: nil, status: 415)
end
begin
JSON.parse(event["body"])
rescue
return response(body: event, status: 422)
else
payload = {
data: JSON.parse(event["body"]),
exp: Time.now.to_i + 5,
nbf: Time.now.to_i + 2
}
token = JWT.encode payload, "ITSASECRET", 'HS256'
return response(body: {"token" => token}, status: 201)
end
else
return response(body: nil, status: 405)
end
end
def response(body: nil, status: 200)
{
body: body ? body.to_json + "\n" : '',
statusCode: status
}
end
if $PROGRAM_NAME == __FILE__
# If you run this file directly via `ruby function.rb` the following code
# will execute. You can use the code below to help you test your functions
# without needing to deploy first.
ENV['JWT_SECRET'] = 'NOTASECRET'
# # Call /token
# PP.pp main(context: {}, event: {
# 'body' => '{"name": "bboe"}',
# 'headers' => { 'Content-Type' => 'application/json' },
# 'httpMethod' => 'POST',
# 'path' => '/token'
# })
# Generate a token
payload = {
data: { user_id: 128 },
exp: Time.now.to_i + 1,
nbf: Time.now.to_i
}
token = JWT.encode payload, ENV['JWT_SECRET'], 'HS256'
# Call /
# PP.pp main(context: {}, event: {
# 'headers' => { 'AuthoRIZation' => "Bearer #{token}",
# 'CONtent-Type' => 'application/json' },
# 'httpMethod' => 'GET',
# 'path' => '/'
# })
PP.pp main(context: {}, event: {
'headers' => { 'AuthoRIZation' => nil,
'CONtent-Type' => 'application/json' },
'httpMethod' => 'GET',
'path' => '/'
})
end