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.
-
#fetch_encoded_key_via_http ⇒ String
Fetch the encoded (DER, or PEM) public key from a remote location via HTTP/HTTPS.
-
#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.
19 20 21 22 |
# File 'lib/keyless/rsa_public_key.rb', line 19 def initialize @expiration = 1.hour @cache = ActiveSupport::Cache::MemoryStore.new end |
Instance Attribute Details
#cache ⇒ Object
Setup all the getters and setters.
15 16 17 |
# File 'lib/keyless/rsa_public_key.rb', line 15 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.
124 125 126 127 128 129 130 |
# File 'lib/keyless/rsa_public_key.rb', line 124 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.
112 113 114 115 116 117 118 |
# File 'lib/keyless/rsa_public_key.rb', line 112 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.
100 101 102 103 104 105 106 |
# File 'lib/keyless/rsa_public_key.rb', line 100 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.
28 29 30 |
# File 'lib/keyless/rsa_public_key.rb', line 28 def self.fetch instance.fetch end |
Instance Method Details
#cache? ⇒ Boolean
A helper for the caching configuration.
82 83 84 |
# File 'lib/keyless/rsa_public_key.rb', line 82 def cache? caching && true end |
#configure {|_self| ... } ⇒ Object
Configure the single instance. This is just a wrapper (like tap) to the instance itself.
34 35 36 |
# File 'lib/keyless/rsa_public_key.rb', line 34 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.
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/keyless/rsa_public_key.rb', line 43 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.
59 60 61 62 63 |
# File 'lib/keyless/rsa_public_key.rb', line 59 def fetch_encoded_key raise ArgumentError, 'No URL for RsaPublicKey configured' unless url remote? ? fetch_encoded_key_via_http : File.read(url) end |
#fetch_encoded_key_via_http ⇒ String
Fetch the encoded (DER, or PEM) public key from a remote location via HTTP/HTTPS.
69 70 71 72 73 74 75 76 77 |
# File 'lib/keyless/rsa_public_key.rb', line 69 def fetch_encoded_key_via_http conf = ::Keyless.configuration with_retries(max_tries: conf.rsa_public_key_fetch_retries) do res = HTTP.get(url) raise FetchError, res.inspect unless res.status.success? res.to_s 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.
91 92 93 |
# File 'lib/keyless/rsa_public_key.rb', line 91 def remote? !(url =~ /^https?/).nil? end |