-
Notifications
You must be signed in to change notification settings - Fork 372
/
hmac.rb
57 lines (44 loc) · 1.25 KB
/
hmac.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true
module JWT
module JWK
class HMAC < KeyAbstract
KTY = 'oct'.freeze
def initialize(keypair, kid = nil)
raise ArgumentError, 'keypair must be of type String' unless keypair.is_a?(String)
super
@kid = kid || generate_kid(@keypair)
end
def private?
true
end
def public_key
nil
end
# See https://tools.ietf.org/html/rfc7517#appendix-A.3
def export(options = {})
ret = {
kty: KTY,
kid: kid
}
return ret unless private? && options[:include_private] == true
ret.merge(
k: keypair
)
end
private
def generate_kid(hmac_key)
sequence = OpenSSL::ASN1::Sequence([OpenSSL::ASN1::UTF8String.new(hmac_key),
OpenSSL::ASN1::UTF8String.new(KTY)])
OpenSSL::Digest::SHA256.hexdigest(sequence.to_der)
end
class << self
def import(jwk_data)
jwk_k = jwk_data[:k] || jwk_data['k']
jwk_kid = jwk_data[:kid] || jwk_data['kid']
raise JWT::JWKError, 'Key format is invalid for HMAC' unless jwk_k
self.new(jwk_k, jwk_kid)
end
end
end
end
end