2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
module JSONWebToken
|
|
|
|
class RSAToken < Token
|
|
|
|
attr_reader :key_file
|
|
|
|
|
|
|
|
def initialize(key_file)
|
|
|
|
super()
|
|
|
|
@key_file = key_file
|
|
|
|
end
|
|
|
|
|
|
|
|
def encoded
|
|
|
|
headers = {
|
2019-02-15 15:39:39 +05:30
|
|
|
kid: kid,
|
|
|
|
typ: 'JWT'
|
2016-06-02 11:05:42 +05:30
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
|
def public_key
|
|
|
|
key.public_key
|
|
|
|
end
|
|
|
|
|
|
|
|
def kid
|
|
|
|
# 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(':')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|