generated from GrantBirki/crystal-base-template
-
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
1 parent
13f7ff0
commit 3b8fd10
Showing
4 changed files
with
32 additions
and
2 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
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
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,27 @@ | ||
require "./token" | ||
|
||
module Kemal::Hmac | ||
class Client | ||
def initialize(client : String, secret : String, algorithm : String? = "SHA256") | ||
@client = client.upcase | ||
@secret = secret | ||
algo = (algorithm || ENV.fetch("HMAC_ALGORITHM", "SHA256")).upcase | ||
@algorithm = Kemal::Hmac.algorithm(algo) | ||
|
||
unless KEY_VALIDATION_REGEX.match(@client) | ||
raise InvalidSecretError.new("client name must only contain letters, numbers, -, or _") | ||
end | ||
end | ||
|
||
def generate_headers(path : String) | ||
timestamp = Time::Format::ISO_8601_DATE_TIME.format(Time.utc) | ||
hmac_token = Kemal::Hmac::Token.new(@client, path, timestamp).hexdigest(@secret) | ||
|
||
return { | ||
"hmac-client" => @client, | ||
"hmac-timestamp" => timestamp, | ||
"hmac-token" => hmac_token, | ||
} | ||
end | ||
end | ||
end |
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