Class: Aspera::OAuth::Factory
- Inherits:
-
Object
- Object
- Aspera::OAuth::Factory
- Includes:
- Singleton
- Defined in:
- lib/aspera/oauth/factory.rb
Overview
Factory to create tokens and manage their cache
Constant Summary collapse
- TOKEN_FIELD =
'access_token'
Instance Attribute Summary collapse
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
Class Method Summary collapse
-
.bearer_auth?(authorization) ⇒ Boolean
True if the authorization contains a bearer token , i.e.
-
.bearer_authorization(token) ⇒ String
Value suitable for Authorization header.
-
.bearer_token(authorization) ⇒ Object
Extract only token from Authorization (remove scheme).
-
.cache_id(url, creator_class, *params) ⇒ Object
A unique cache identifier.
-
.class_to_id(creator_class) ⇒ Object
Snake version of class name.
Instance Method Summary collapse
-
#create(**parameters) ⇒ Object
One of the registered creators for the given create type.
-
#decode_token(token) ⇒ Object
decode token using all registered decoders.
-
#flush_tokens ⇒ Object
delete all existing tokens.
-
#get_token_info(id) ⇒ Hash
Get token information from cache.
- #persist_mgr ⇒ Object
- #persist_mgr=(manager) ⇒ Object
- #persisted_tokens ⇒ Object
-
#register_decoder(method) ⇒ Object
register a bearer token decoder, mainly to inspect expiry date.
-
#register_token_creator(creator_class) ⇒ Object
register a token creation method.
Instance Attribute Details
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
75 76 77 |
# File 'lib/aspera/oauth/factory.rb', line 75 def parameters @parameters end |
Class Method Details
.bearer_auth?(authorization) ⇒ Boolean
Returns true if the authorization contains a bearer token , i.e. auth scheme is bearer.
29 30 31 |
# File 'lib/aspera/oauth/factory.rb', line 29 def bearer_auth?() return .start_with?(SPACE_BEARER_AUTH_SCHEME) end |
.bearer_authorization(token) ⇒ String
Returns Value suitable for Authorization header.
24 25 26 |
# File 'lib/aspera/oauth/factory.rb', line 24 def (token) return "#{SPACE_BEARER_AUTH_SCHEME}#{token}" end |
.bearer_token(authorization) ⇒ Object
Extract only token from Authorization (remove scheme)
34 35 36 37 |
# File 'lib/aspera/oauth/factory.rb', line 34 def bearer_token() Aspera.assert(bearer_auth?()){'not a bearer token, wrong prefix scheme'} return [SPACE_BEARER_AUTH_SCHEME.length..-1] end |
.cache_id(url, creator_class, *params) ⇒ Object
Returns a unique cache identifier.
40 41 42 43 44 45 46 47 |
# File 'lib/aspera/oauth/factory.rb', line 40 def cache_id(url, creator_class, *params) return IdGenerator.from_list([ PERSIST_CATEGORY_TOKEN, url, Factory.class_to_id(creator_class) ] + params) end |
.class_to_id(creator_class) ⇒ Object
Returns snake version of class name.
50 51 52 |
# File 'lib/aspera/oauth/factory.rb', line 50 def class_to_id(creator_class) return creator_class.name.split('::').last.capital_to_snake.to_sym end |
Instance Method Details
#create(**parameters) ⇒ Object
Returns one of the registered creators for the given create type.
164 165 166 167 168 169 170 |
# File 'lib/aspera/oauth/factory.rb', line 164 def create(**parameters) Aspera.assert_type(parameters, Hash) id = parameters[:grant_method] Aspera.assert(@token_type_classes.key?(id)){"token grant method unknown: '#{id}'"} create_parameters = parameters.reject{ |k, _v| k.eql?(:grant_method)} @token_type_classes[id].new(**create_parameters) end |
#decode_token(token) ⇒ Object
decode token using all registered decoders
144 145 146 147 148 149 150 |
# File 'lib/aspera/oauth/factory.rb', line 144 def decode_token(token) @decoders.each do |decoder| result = decoder.call(token) rescue nil return result unless result.nil? end return end |
#flush_tokens ⇒ Object
delete all existing tokens
96 97 98 |
# File 'lib/aspera/oauth/factory.rb', line 96 def flush_tokens persist_mgr.garbage_collect(PERSIST_CATEGORY_TOKEN) end |
#get_token_info(id) ⇒ Hash
Get token information from cache
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/aspera/oauth/factory.rb', line 114 def get_token_info(id) token_raw_string = persist_mgr.get(id) return if token_raw_string.nil? token_data = JSON.parse(token_raw_string) Aspera.assert_type(token_data, Hash) decoded_token = decode_token(token_data[TOKEN_FIELD]) info = {data: token_data} if decoded_token.is_a?(Hash) info[:decoded] = decoded_token # TODO: move date decoding to token decoder ? expiration_date = if decoded_token['expires_at'].is_a?(String) then Time.parse(decoded_token['expires_at']).to_time elsif decoded_token['exp'].is_a?(Integer) then Time.at(decoded_token['exp']) end unless expiration_date.nil? info[:expiration] = expiration_date info[:ttl_sec] = expiration_date - Time.now info[:expired] = info[:ttl_sec] < @parameters[:token_refresh_threshold] end end Log.dump(:token_info, info) return info end |
#persist_mgr ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/aspera/oauth/factory.rb', line 83 def persist_mgr if @persist.nil? # use OAuth::Factory.instance.persist_mgr=PersistencyFolder.new) Log.log.debug('Not using persistency') # create NULL persistency class @persist = Class.new do def get(_x); nil; end; def delete(_x); nil; end; def put(_x, _y); nil; end; def garbage_collect(_x, _y); nil; end # rubocop:disable Style/Semicolon end.new end return @persist end |
#persist_mgr=(manager) ⇒ Object
77 78 79 80 81 |
# File 'lib/aspera/oauth/factory.rb', line 77 def persist_mgr=(manager) @persist = manager # cleanup expired tokens @persist.garbage_collect(PERSIST_CATEGORY_TOKEN, @parameters[:token_cache_max_age]) end |
#persisted_tokens ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/aspera/oauth/factory.rb', line 100 def persisted_tokens data = persist_mgr.current_items(PERSIST_CATEGORY_TOKEN) data.each.map do |k, v| info = {id: k} info.merge!(JSON.parse(v)) rescue nil d = decode_token(info.delete(TOKEN_FIELD)) info.merge(d) if d info end end |
#register_decoder(method) ⇒ Object
register a bearer token decoder, mainly to inspect expiry date
139 140 141 |
# File 'lib/aspera/oauth/factory.rb', line 139 def register_decoder(method) @decoders.push(method) end |
#register_token_creator(creator_class) ⇒ Object
register a token creation method
156 157 158 159 160 161 |
# File 'lib/aspera/oauth/factory.rb', line 156 def register_token_creator(creator_class) Aspera.assert_type(creator_class, Class) id = Factory.class_to_id(creator_class) Log.log.debug{"registering token creator #{id}"} @token_type_classes[id] = creator_class end |