Skip to content

Commit

Permalink
Rotate keys and dotenv working
Browse files Browse the repository at this point in the history
  • Loading branch information
hack3rvaillant committed Oct 30, 2024
1 parent 9cad4b3 commit cfd64d4
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 19 deletions.
38 changes: 38 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
JWT_SIGNING_PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA8PYdftBuWoq/9GeNHO8bA8/gHYBe5wB0W+F2jPfQwuIm5vlO
EKFUTt2boAF1RI+gLrBzOEWDHu9/Pen8/XUxeXG9/w0KaawK1Nc/jlMaUs9jZceE
40mBcNypOPcPITRNuClrE05ACawd0MvXSwDrIIkF4RVxmigsuVwBy0BPTGaq0XkJ
IF372wow0y5YaJVcF5Lo013ILY7U7hziNIXx9hpZEuT65d+HRXnmVYt09HY2PMwm
QJc4foPZXE57yDjeJ8kIiGvWamcEEr1spa82uuykoAEOmw42JOgMME8tIf+9jjnd
ubYMkiAo6MFSVflHd7wYZ6WeBIOzBegD/40MHQIDAQABAoIBAADKrZRPGLxOcEm/
P7VXaUt3EJqhwlIyGHgJPA1tFdOh1qxBI8BOSRJIQ+kIFmNt/5rTR7S7bKPoBI8p
vJBkmJPdjJ8kb1WvwmtiLEhHMrQIQflPDDMC4hljpcPSD8vJD4omiOGikzVF9bvc
apCJg/ghEQ+pLQ/OYyngdVw+ewCbeP0A7xWag33Kg2OKY6v/N0dZsRzTndL+qonk
qfKBA2bFUO476IjSZRNLiaGBk1Vx36ygJ/cI3BEpmDzDkSMJ6HMIH2JKnKk6rq/t
rA28y7Ot4aCEXJJpE4isDoxIgwjwLuuPDPWnL4UHeBvogFHL87wqihJ9lG1feb6q
jXiCUdkCgYEA/TgkV05ipwkFzg5hT+JJmIX6tNzcc8+M51mjBAtpNDf85T+8+Ja2
HMujMrnf6E+T7rnAKD9zBQQ1hKi1eSeCU/osgtaLIhATa/H1EQw+EVnJ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
9hpZEuT65d+HRXnmVYt09HY2PMwmQJc4foPZXE57yDjeJ8kIiGvWamcEEr1spa82
uuykoAEOmw42JOgMME8tIf+9jjndubYMkiAo6MFSVflHd7wYZ6WeBIOzBegD/40M
HQIDAQAB
-----END PUBLIC KEY-----
"
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ gem install sinatra jwt

### Configuration

- Set the `SECRET_KEY` environment variable for JWT verification:
```ruby
# .env
SECRET_KEY = 'your-secret-key'
Use the script to rotate the keys
```bash
./rotate_keys
```
This will add the keys to the .env file

### Starting the Server

Run the following command to start the server on port 4567:
```bash
ruby proxy_server.rb
rackup config.ru
```

### Making Requests
Expand Down
2 changes: 1 addition & 1 deletion config.ru
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# config.ru

require "./proxy_server" # Adjust the path if necessary
require "./proxy_server"
run ProxyServer
7 changes: 5 additions & 2 deletions proxy_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
require "json"
require "jwt"
require "debug"
require "dotenv/load"
require "openssl/pkey"

class ProxyServer < Sinatra::Base
set :port, 4567

# Secret key for JWT verification
SECRET_KEY = "your-secret-key"
PUBLIC_KEY = ENV.fetch("JWT_SIGNING_PUBLIC_KEY").gsub("\\n", "\n")

# Handle CORS headers
before do
Expand All @@ -30,7 +32,8 @@ class ProxyServer < Sinatra::Base

# Verify JWT token
begin
JWT.decode(token, SECRET_KEY, true, {algorithm: "HS256"})
public_key = OpenSSL::PKey.read(PUBLIC_KEY)
JWT.decode(token, public_key, true, {algorithm: "RS512"})
rescue JWT::DecodeError
halt 401, {error: "Invalid token"}.to_json
end
Expand Down
24 changes: 24 additions & 0 deletions rotake_keys.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env ruby
require 'openssl'
require 'debug'

# Define the RSA key size
key_size = 2048

# Generate a new RSA key pair
rsa_key = OpenSSL::PKey::RSA.new(key_size)

# Display the private key in PEM format
puts "Private Key:"
private_key = rsa_key.to_pem
puts private_key
`sed -i '' '/JWT_SIGNING_PRIVATE_KEY/d' ./.env`
`echo 'JWT_SIGNING_PRIVATE_KEY="#{private_key}"' >> ./.env`

# Display the public key in PEM format
puts "\nPublic Key:"
public_key = rsa_key.public_key.to_pem
puts public_key
`sed -i '' '/JWT_SIGNING_PUBLIC_KEY/d' ./.env`
`echo 'JWT_SIGNING_PUBLIC_KEY="#{public_key}"' >> ./.env`

32 changes: 21 additions & 11 deletions spec/proxy_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
require "rack/test"
require "webmock/rspec"
require_relative "../proxy_server"
require "dotenv/load"
require "jwt"

SECRET_KEY = "your-secret-key"
PRIVATE_KEY = ENV.fetch("JWT_SIGNING_PRIVATE_KEY").gsub("\\n", "\n")

describe "ProxyServer" do
include Rack::Test::Methods
Expand All @@ -17,7 +18,10 @@ def expect_header(k, v)
expect(last_response.headers[k]).to eq v
end

let(:valid_token) { JWT.encode({data: "test"}, SECRET_KEY, "HS256") }
let(:valid_token) do
private_key = OpenSSL::PKey::RSA.new(PRIVATE_KEY)
JWT.encode({data: "test"}, private_key, "RS512")
end
let(:invalid_token) { "invalid.token.here" }
let(:target_url) { "https://jsonplaceholder.typicode.com/posts" }

Expand All @@ -37,21 +41,27 @@ def expect_header(k, v)
before(:each) do
options "/?url=#{target_url}"
end

it "returns CORS headers" do
expect_header("access-control-allow-origin", "*")
end
end

context "when x-bump-jwt-token is present" do
before(:each) do
header "x-bump-jwt-token", valid_token
header "x-foo", "bar"
get "/?url=#{target_url}"
end

it "returns 200 for a valid token" do
expect(last_response.status).to eq(200)
expect_header("access-control-allow-origin", "*")
context "and is valid" do
before(:each) do
header "x-bump-jwt-token", valid_token
header "x-foo", "bar"
get "/?url=#{target_url}"
end

it "returns 200" do
expect(last_response.status).to eq(200)
end

it "returns cors headers" do
expect_header("access-control-allow-origin", "*")
end
end

it "returns 401 for an invalid token" do
Expand Down

0 comments on commit cfd64d4

Please sign in to comment.