Skip to content

Commit

Permalink
clean up comments
Browse files Browse the repository at this point in the history
  • Loading branch information
GrantBirki committed Oct 17, 2024
1 parent 1e1cd31 commit 55a2a0c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/kemal-hmac/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ require "./token"

module Kemal::Hmac
class Client
# :param client: the client name which will be sending HTTP requests to the server using HMAC auth (String)
# :param secret: the secret used to generate the HMAC token in relation to the client (String)
# # :param algorithm: the algorithm used to generate the HMAC token (String) - defaults to SHA256
# `client` the client name which will be sending HTTP requests to the server using HMAC auth (String)
# `secret` the secret used to generate the HMAC token in relation to the client (String)
# `algorithm` the algorithm used to generate the HMAC token (String) - defaults to SHA256
def initialize(client : String, secret : String, algorithm : String? = "SHA256")
@client = client
@secret = secret
Expand All @@ -18,8 +18,8 @@ module Kemal::Hmac

# A public helper method to generate the HMAC headers for a given path
# Use this method to get pre-filled headers to send with your request to the server
# :param path: the path (HTTP path) to generate the headers for (String) - e.g. "/api/path"
# :return: a Hash of the HMAC headers
# `path` the path (HTTP path) to generate the headers for (String) - e.g. "/api/path"
# returns a Hash of the HMAC headers
def generate_headers(path : String) : Hash(String, String)
timestamp = Time::Format::ISO_8601_DATE_TIME.format(Time.utc)
hmac_token = Kemal::Hmac::Token.new(@client, path.split("?").first, timestamp, @algorithm).hexdigest(@secret)
Expand Down
12 changes: 6 additions & 6 deletions src/kemal-hmac/handler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ module Kemal::Hmac
end

# Check the request token by building our own with our known metadata and secret
# :param request_token: token provided in request
# :param secret: secret used to build token
# :param client: client name used to build token
# :param path: path used to build token
# :param timestamp: timestamp used to build token
# :return: True if token matches, False otherwise
# `request_token` token provided in request
# `secret` secret used to build token
# `client` client name used to build token
# `path` path used to build token
# `timestamp` timestamp used to build token
# returns True if token matches, False otherwise
def valid_token?(request_token, secret, client, path, timestamp)
token = Kemal::Hmac::Token.new(client, path, timestamp, @hmac_algorithm)
Crypto::Subtle.constant_time_compare(token.hexdigest(secret), request_token)
Expand Down
10 changes: 5 additions & 5 deletions src/kemal-hmac/token.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ require "openssl/hmac"

module Kemal::Hmac
class Token
# :param subject: the subject of the token (String)
# :param resource: the resource of the token (String)
# :param timestamp: the timestamp of the token (String)
# `subject` the subject of the token (String)
# `resource` the resource of the token (String)
# `timestamp` the timestamp of the token (String)
def initialize(subject : String, resource : String, timestamp : String, algorithm : OpenSSL::Algorithm? = nil)
@subject = subject
@resource = resource
Expand All @@ -14,8 +14,8 @@ module Kemal::Hmac
end

# Build an HMAC token with the given secret
# :param secret: the secret used to build token
# :return: HMAC token (hexdigest)
# `secret` the secret used to build token
# returns HMAC token (hexdigest)
def hexdigest(secret : String) : String
OpenSSL::HMAC.hexdigest(@algorithm, secret, message)
end
Expand Down

0 comments on commit 55a2a0c

Please sign in to comment.