2018-10-08 14:50:39 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-14 23:23:31 +00:00
|
|
|
module JSONWebToken
|
2016-05-02 12:32:16 +00:00
|
|
|
class RSAToken < Token
|
|
|
|
attr_reader :key_file
|
|
|
|
|
|
|
|
def initialize(key_file)
|
|
|
|
super()
|
|
|
|
@key_file = key_file
|
|
|
|
end
|
|
|
|
|
|
|
|
def encoded
|
|
|
|
headers = {
|
|
|
|
kid: kid
|
|
|
|
}
|
|
|
|
JWT.encode(payload, key, 'RS256', headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def key_data
|
|
|
|
@key_data ||= File.read(key_file)
|
|
|
|
end
|
|
|
|
|
|
|
|
def key
|
|
|
|
@key ||= OpenSSL::PKey::RSA.new(key_data)
|
|
|
|
end
|
|
|
|
|
2016-05-13 21:41:30 +00:00
|
|
|
def public_key
|
|
|
|
key.public_key
|
|
|
|
end
|
|
|
|
|
2016-05-02 12:32:16 +00:00
|
|
|
def kid
|
2016-05-14 21:04:26 +00:00
|
|
|
# calculate sha256 from DER encoded ASN1
|
|
|
|
kid = Digest::SHA256.digest(public_key.to_der)
|
|
|
|
|
|
|
|
# we encode only 30 bytes with base32
|
|
|
|
kid = Base32.encode(kid[0..29])
|
|
|
|
|
|
|
|
# insert colon every 4 characters
|
|
|
|
kid.scan(/.{4}/).join(':')
|
2016-05-02 12:32:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|