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.



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

#cacheObject

Setup all the getters and setters.



19
20
21
# File 'lib/keyless/rsa_public_key.rb', line 19

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



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

#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



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

#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



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

.fetchOpenSSL::PKey::RSA

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

Returns:

  • (OpenSSL::PKey::RSA)


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.

Returns:

  • (Boolean)


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.

Yields:

  • (_self)

Yield Parameters:



38
39
40
# File 'lib/keyless/rsa_public_key.rb', line 38

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)


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_keyString

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

Returns:

  • (String)

    The encoded public key

Raises:

  • (ArgumentError)


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.

Returns:

  • (Boolean)


88
89
90
# File 'lib/keyless/rsa_public_key.rb', line 88

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