forked from jwt/ruby-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsigning_algorithm.rb
More file actions
62 lines (49 loc) · 1.36 KB
/
Copy pathsigning_algorithm.rb
File metadata and controls
62 lines (49 loc) · 1.36 KB
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
58
59
60
61
62
# frozen_string_literal: true
module JWT
# JSON Web Algorithms
module JWA
# Base functionality for signing algorithms
module SigningAlgorithm
# Class methods for the SigningAlgorithm module
module ClassMethods
def register_algorithm(algo)
::JWT::JWA.register_algorithm(algo)
end
end
def self.included(klass)
klass.extend(ClassMethods)
end
attr_reader :alg
def valid_alg?(alg_to_check)
alg&.casecmp(alg_to_check)&.zero? == true
end
def header(*)
{ 'alg' => alg }
end
def sign(*)
raise_sign_error!('Algorithm implementation is missing the sign method')
end
def verify(*)
raise_verify_error!('Algorithm implementation is missing the verify method')
end
def raise_verify_error!(message)
raise(VerificationKeyError.new(message).tap { |e| e.set_backtrace(caller(1)) })
end
def raise_sign_error!(message)
raise(EncodeError.new(message).tap { |e| e.set_backtrace(caller(1)) })
end
end
class << self
def register_algorithm(algo)
algorithms[algo.alg.to_s.downcase] = algo
end
def find(algo)
algorithms.fetch(algo.to_s.downcase, Unsupported)
end
private
def algorithms
@algorithms ||= {}
end
end
end
end