Class: Keyless::RsaPublicKey

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRsaPublicKey

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

#cacheObject

Setup all the getters and setters.



15
16
17
# File 'lib/keyless/rsa_public_key.rb', line 15

def cache
  @cache
end

#cachingBoolean

This getter passes back the caching flag. You can change this flag with the help of the same named setter.

Returns:

  • (Boolean)

    Whenever we should cache or not



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

#expirationInteger

This getter passes back the default public key cache expiration time. You can change this time with the help of the same named setter.

Returns:

  • (Integer)

    The configured cache expiration time



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

#urlString

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.

Returns:

  • (String)

    The configured public key location



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

.fetchOpenSSL::PKey::RSA

Just a simple shortcut class method to access the fetch method without specifying the singleton instance.

Returns:

  • (OpenSSL::PKey::RSA)


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.

Returns:

  • (Boolean)


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.

Yields:

  • (_self)

Yield Parameters:



34
35
36
# File 'lib/keyless/rsa_public_key.rb', line 34

def configure
  yield(self)
end

#fetchOpenSSL::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.

Returns:

  • (OpenSSL::PKey::RSA)


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_keyString

Fetch the encoded (DER, or PEM) public key from a remote or local location.

Returns:

  • (String)

    The encoded public key

Raises:

  • (ArgumentError)


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_httpString

Fetch the encoded (DER, or PEM) public key from a remote location via HTTP/HTTPS.

Returns:

  • (String)

    The encoded public key



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.

Returns:

  • (Boolean)


91
92
93
# File 'lib/keyless/rsa_public_key.rb', line 91

def remote?
  !(url =~ /^https?/).nil?
end