Class: Aspera::OAuth::Factory

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/aspera/oauth/factory.rb

Overview

Factory to create tokens and manage their cache

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parametersObject (readonly)

Returns the value of attribute parameters.



61
62
63
# File 'lib/aspera/oauth/factory.rb', line 61

def parameters
  @parameters
end

Class Method Details

.bearer?(token) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/aspera/oauth/factory.rb', line 23

def bearer?(token)
  return token.start_with?(BEARER_PREFIX)
end

.bearer_build(token) ⇒ Object



19
20
21
# File 'lib/aspera/oauth/factory.rb', line 19

def bearer_build(token)
  return "#{BEARER_PREFIX}#{token}"
end

.bearer_extract(token) ⇒ Object



27
28
29
30
# File 'lib/aspera/oauth/factory.rb', line 27

def bearer_extract(token)
  Aspera.assert(bearer?(token)){'not a bearer token, wrong prefix'}
  return token[BEARER_PREFIX.length..-1]
end

.class_to_id(creator_class) ⇒ Object

snake version of class name is the identifier



37
38
39
# File 'lib/aspera/oauth/factory.rb', line 37

def class_to_id(creator_class)
  return creator_class.name.split('::').last.capital_to_snake.to_sym
end

.id(*params) ⇒ Object



32
33
34
# File 'lib/aspera/oauth/factory.rb', line 32

def id(*params)
  return [PERSIST_CATEGORY_TOKEN, *params].flatten
end

Instance Method Details

#create(**parameters) ⇒ Object

Returns one of the registered creators for the given create type.

Returns:

  • one of the registered creators for the given create type



112
113
114
115
116
117
118
# File 'lib/aspera/oauth/factory.rb', line 112

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



92
93
94
95
96
97
98
# File 'lib/aspera/oauth/factory.rb', line 92

def decode_token(token)
  @decoders.each do |decoder|
    result = decoder.call(token) rescue nil
    return result unless result.nil?
  end
  return nil
end

#flush_tokensObject

delete all existing tokens



82
83
84
# File 'lib/aspera/oauth/factory.rb', line 82

def flush_tokens
  persist_mgr.garbage_collect(PERSIST_CATEGORY_TOKEN, nil)
end

#persist_mgrObject



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/aspera/oauth/factory.rb', line 69

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 Layout/EmptyLineBetweenDefs, Style/Semicolon, Layout/LineLength
    end.new
  end
  return @persist
end

#persist_mgr=(manager) ⇒ Object



63
64
65
66
67
# File 'lib/aspera/oauth/factory.rb', line 63

def persist_mgr=(manager)
  @persist = manager
  # cleanup expired tokens
  @persist.garbage_collect(PERSIST_CATEGORY_TOKEN, @parameters[:token_cache_expiry_sec])
end

#register_decoder(method) ⇒ Object

register a bearer token decoder, mainly to inspect expiry date



87
88
89
# File 'lib/aspera/oauth/factory.rb', line 87

def register_decoder(method)
  @decoders.push(method)
end

#register_token_creator(creator_class) ⇒ Object

register a token creation method

Parameters:

  • id

    creation type from field :grant_method in constructor

  • lambda_create

    called to create token

  • id_create

    called to generate unique id for token, for cache



104
105
106
107
108
109
# File 'lib/aspera/oauth/factory.rb', line 104

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