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



117
118
119
120
121
122
123
# File 'lib/keyless/rsa_public_key.rb', line 117

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



105
106
107
108
109
110
111
# File 'lib/keyless/rsa_public_key.rb', line 105

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



93
94
95
96
97
98
99
# File 'lib/keyless/rsa_public_key.rb', line 93

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)


75
76
77
# File 'lib/keyless/rsa_public_key.rb', line 75

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
64
65
66
67
68
69
70
# File 'lib/keyless/rsa_public_key.rb', line 59

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.

Returns:

  • (Boolean)


84
85
86
# File 'lib/keyless/rsa_public_key.rb', line 84

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