-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
module JWT | ||
module KMS | ||
# Represent a AWS HMAC key | ||
# https://docs.aws.amazon.com/kms/latest/developerguide/hmac.html | ||
class HmacKey | ||
include JWT::JWA::SigningAlgorithm | ||
|
||
MAPPINGS = { | ||
"HMAC_256" => { alg: "HS256", mac_algorithm: "HMAC_SHA_256" }, | ||
"HMAC_384" => { alg: "HS384", mac_algorithm: "HMAC_SHA_384" }, | ||
"HMAC_512" => { alg: "HS512", mac_algorithm: "HMAC_SHA_512" } | ||
}.freeze | ||
|
||
def initialize(key_id:, key_spec: nil) | ||
@key_id = key_id | ||
@key_spec = key_spec | ||
end | ||
|
||
def alg | ||
MAPPINGS.dig(key_spec, :alg) | ||
end | ||
|
||
def sign(data:, **) | ||
KMS.client.generate_mac(key_id: key_id, mac_algorithm: mac_algorithm, message: data).mac | ||
end | ||
|
||
def verify(data:, signature:, **) | ||
KMS.client.verify_mac(key_id: key_id, mac_algorithm: mac_algorithm, message: data, mac: signature).mac_valid | ||
end | ||
|
||
private | ||
|
||
attr_reader :key_id | ||
|
||
def key_spec | ||
@key_spec ||= description.key_spec | ||
end | ||
|
||
def mac_algorithm | ||
MAPPINGS.dig(key_spec, :mac_algorithm) | ||
end | ||
|
||
def description | ||
@description ||= KMS.client.describe_key(key_id: key_id) | ||
end | ||
end | ||
end | ||
end |