Class: ForgeAuth::Tokenizer

Inherits:
Object
  • Object
show all
Defined in:
lib/forge_auth/tokenizer.rb

Instance Method Summary collapse

Constructor Details

#initialize(auth_host:, auth_path:, client_id:, client_secret:, grant_type: "client_credentials", cache: nil, scope: nil, logger: nil) ⇒ Tokenizer

Returns a new instance of Tokenizer.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/forge_auth/tokenizer.rb', line 7

def initialize(auth_host:, auth_path:, client_id:, client_secret:, grant_type: "client_credentials", cache: nil, scope: nil, logger: nil)
  @auth_host = auth_host
  @auth_path = auth_path
  @cache = cache
  @logger = logger
  @params = { client_id: client_id,
              client_secret: client_secret,
              scope: scope,
              grant_type: grant_type }

  @params.delete(:scope) unless scope
end

Instance Method Details

#get_tokenObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/forge_auth/tokenizer.rb', line 20

def get_token
  if with_cache?
    token = @cache.fetch(cache_key)
    return token if token

    token = get_token_from_remote
    @cache.write(cache_key, token, expires_in: token.short_expiration)
    token
  else
    get_token_from_remote
  end
rescue => exc
  handle_error(exc)
end

#refresh_tokenObject



35
36
37
38
39
40
41
# File 'lib/forge_auth/tokenizer.rb', line 35

def refresh_token
  if with_cache?
    @cache.write(cache_key, nil)
  end

  get_token
end