-
Notifications
You must be signed in to change notification settings - Fork 19
/
function.rb
48 lines (42 loc) · 1.33 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
# frozen_string_literal: true
require 'json'
require 'jwt'
require 'pp'
def main(event:, context:)
# You shouldn't need to use context, but its fields are explained here:
# https://docs.aws.amazon.com/lambda/latest/dg/ruby-context.html
response(body: event, status: 200)
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' => '/'
})
end