Class: Keyless::RsaPublicKey
- Inherits:
-
Object
- Object
- Keyless::RsaPublicKey
- Includes:
- Singleton
- Defined in:
- lib/keyless/rsa_public_key.rb
Overview
A common purpose RSA public key fetching/caching helper. With the help of this class you are able to retrieve the RSA public key from a remote server or a local file. This is naturally only useful if you care about JSON Web Token which are signed by the RSA algorithm.
Defined Under Namespace
Classes: FetchError
Instance Attribute Summary collapse
-
#cache ⇒ Object
Setup all the getters and setters.
-
#caching ⇒ Boolean
This getter passes back the caching flag.
-
#expiration ⇒ Integer
This getter passes back the default public key cache expiration time.
-
#url ⇒ String
This getter passes back the default RSA public key.
Class Method Summary collapse
-
.fetch ⇒ OpenSSL::PKey::RSA
Just a simple shortcut class method to access the fetch method without specifying the singleton instance.
Instance Method Summary collapse
-
#cache? ⇒ Boolean
A helper for the caching configuration.
-
#configure {|_self| ... } ⇒ Object
Configure the single instance.
-
#fetch ⇒ OpenSSL::PKey::RSA
Fetch the public key with the help of the configuration.
-
#fetch_encoded_key ⇒ String
Fetch the encoded (DER, or PEM) public key from a remote or local location.
-
#initialize ⇒ RsaPublicKey
constructor
Setup the instance.
-
#remote? ⇒ Boolean
A helper to determine if the configured URL is on a remote server or it is local on the filesystem.
Constructor Details
#initialize ⇒ RsaPublicKey
Setup the instance.
23 24 25 26 |
# File 'lib/keyless/rsa_public_key.rb', line 23 def initialize @expiration = 1.hour @cache = ActiveSupport::Cache::MemoryStore.new end |
Instance Attribute Details
#cache ⇒ Object
Setup all the getters and setters.
19 20 21 |
# File 'lib/keyless/rsa_public_key.rb', line 19 def cache @cache end |
#caching ⇒ Boolean
This getter passes back the caching flag. You can change this flag with the help of the same named setter.
121 122 123 124 125 126 127 |
# File 'lib/keyless/rsa_public_key.rb', line 121 def caching unless @caching conf = ::Keyless.configuration return conf.rsa_public_key_caching end @caching end |
#expiration ⇒ Integer
This getter passes back the default public key cache expiration time. You can change this time with the help of the same named setter.
109 110 111 112 113 114 115 |
# File 'lib/keyless/rsa_public_key.rb', line 109 def expiration unless @expiration conf = ::Keyless.configuration return conf.rsa_public_key_expiration end @expiration end |
#url ⇒ String
This getter passes back the default RSA public key. You can change this the way you like by configuring your URL with the help of the same named setter.
97 98 99 100 101 102 103 |
# File 'lib/keyless/rsa_public_key.rb', line 97 def url unless @url conf = ::Keyless.configuration return conf.rsa_public_key_url end @url end |
Class Method Details
.fetch ⇒ OpenSSL::PKey::RSA
Just a simple shortcut class method to access the fetch method without specifying the singleton instance.
32 33 34 |
# File 'lib/keyless/rsa_public_key.rb', line 32 def self.fetch instance.fetch end |
Instance Method Details
#cache? ⇒ Boolean
A helper for the caching configuration.
79 80 81 |
# File 'lib/keyless/rsa_public_key.rb', line 79 def cache? caching && true end |
#configure {|_self| ... } ⇒ Object
Configure the single instance. This is just a wrapper (like tap) to the instance itself.
38 39 40 |
# File 'lib/keyless/rsa_public_key.rb', line 38 def configure yield(self) end |
#fetch ⇒ OpenSSL::PKey::RSA
Fetch the public key with the help of the configuration. You can configure the public key location (local file, remote (HTTP/HTTPS) file), whenever we should cache and how long to cache.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/keyless/rsa_public_key.rb', line 47 def fetch encoded_key = if cache? cache.fetch('encoded_key', expires_in: expiration) do fetch_encoded_key end else fetch_encoded_key end OpenSSL::PKey::RSA.new(encoded_key) end |
#fetch_encoded_key ⇒ String
Fetch the encoded (DER, or PEM) public key from a remote or local location.
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/keyless/rsa_public_key.rb', line 63 def fetch_encoded_key raise ArgumentError, 'No URL for RsaPublicKey configured' unless url if remote? res = HTTParty.get(url) raise FetchError, res.inspect unless (200..299).cover? res.code res.body else File.read(url) end end |
#remote? ⇒ Boolean
A helper to determine if the configured URL is on a remote server or it is local on the filesystem. Whenever the configured URL specifies the HTTP/HTTPS protocol, we assume it is remote.
88 89 90 |
# File 'lib/keyless/rsa_public_key.rb', line 88 def remote? !(url =~ /^https?/).nil? end |