Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow replacing default algorithms with AWS KMS versions #7

Merged
merged 2 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,25 @@ And require the gem in your code.
```ruby
require `jwt-aws-kms`
```
## Supported algorithms

The gem supports the following AWS KMS algorithms:

| Algorithm Name | Description | JWA Name |
|----------------|--------------------------------------------------|-------------------------|
| RSASSA_PKCS1_V1_5_SHA_256 | RSASSA PKCS1 v1.5 using SHA-256 | RS256 |
| RSASSA_PKCS1_V1_5_SHA_384 | RSASSA PKCS1 v1.5 using SHA-384 | RS384 |
| RSASSA_PKCS1_V1_5_SHA_512 | RSASSA PKCS1 v1.5 using SHA-512 | RS512 |
| RSASSA_PSS_SHA_256 | RSASSA PSS using SHA-256 | PS256 |
| RSASSA_PSS_SHA_384 | RSASSA PSS using SHA-384 | PS384 |
| RSASSA_PSS_SHA_512 | RSASSA PSS using SHA-512 | PS512 |
| ECDSA_SHA_256 | ECDSA using P-256 and SHA-256 | ES256 |
| ECDSA_SHA_384 | ECDSA using P-384 and SHA-384 | ES384 |
| ECDSA_SHA_512 | ECDSA using P-521 and SHA-512 | ES512 |

## Usage

### Basic usage
```ruby

# Create a key, for example with the ruby AWS SDK
Expand All @@ -28,22 +44,15 @@ algo = ::JWT::Aws::KMS.for(algorithm: "HS512")
token = JWT.encode(payload, key.key_metadata.key_id, algo)
decoded_token = JWT.decode(token, key.key_metadata.key_id, true, algorithm: algo)
```
### Replace default algorithms

## Supported algorithms
You can swap the default algorithms in the JWT gem to AWS backed ones by calling `::JWT::Aws::KMS.replace_defaults!`.

The gem supports the following AWS KMS algorithms:
```ruby
::JWT::Aws::KMS.replace_defaults! # Called in a initializer of some kind

| Algorithm Name | Description | JWA Name |
|----------------|--------------------------------------------------|-------------------------|
| RSASSA_PKCS1_V1_5_SHA_256 | RSASSA PKCS1 v1.5 using SHA-256 | RS256 |
| RSASSA_PKCS1_V1_5_SHA_384 | RSASSA PKCS1 v1.5 using SHA-384 | RS384 |
| RSASSA_PKCS1_V1_5_SHA_512 | RSASSA PKCS1 v1.5 using SHA-512 | RS512 |
| RSASSA_PSS_SHA_256 | RSASSA PSS using SHA-256 | PS256 |
| RSASSA_PSS_SHA_384 | RSASSA PSS using SHA-384 | PS384 |
| RSASSA_PSS_SHA_512 | RSASSA PSS using SHA-512 | PS512 |
| ECDSA_SHA_256 | ECDSA using P-256 and SHA-256 | ES256 |
| ECDSA_SHA_384 | ECDSA using P-384 and SHA-384 | ES384 |
| ECDSA_SHA_512 | ECDSA using P-521 and SHA-512 | ES512 |
token = JWT.encode(payload, "e25c502b-a383-44ac-a778-0d97e8688cb7", "HS512") # Encode payload with KMS key e25c502b-a383-44ac-a778-0d97e8688cb7
```

## Development

Expand Down
8 changes: 8 additions & 0 deletions lib/jwt/aws/kms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ def self.for(algorithm:)
raise ArgumentError, "Algorithm #{algorithm} not supported"
end.new(algorithm: algorithm)
end

def self.replace_defaults!
[HmacKey, SignVerifyKey].each do |type|
type::MAPPINGS.each_key do |algorithm|
type.register_algorithm(type.new(algorithm: algorithm))
end
end
end
end
end
end
17 changes: 17 additions & 0 deletions spec/jwt/aws/kms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,21 @@
expect(decoded_token).to eq([{ "pay" => "load" }, { "alg" => "HS512" }])
end
end

describe ".replace_defaults!" do
before do
described_class.replace_defaults!
end

it "replaces the default algorithms with AWS KMS backed ones" do
expect(JWT::JWA.resolve("RS512")).to be_a(JWT::Aws::KMS::SignVerifyKey)
end

it "allows utilizing the AWS KMS key using the algo name" do
key = Aws::KMS::Client.new.create_key(key_spec: "HMAC_512", key_usage: "GENERATE_VERIFY_MAC")
token = JWT.encode(payload, key.key_metadata.key_id, "HS512")
decoded_token = JWT.decode(token, key.key_metadata.key_id, true, algorithms: "HS512")
expect(decoded_token).to eq([{ "pay" => "load" }, { "alg" => "HS512" }])
end
end
end