Class: Dina::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/dina/authentication/authentication.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAuthentication

Returns a new instance of Authentication.



12
13
14
15
16
# File 'lib/dina/authentication/authentication.rb', line 12

def initialize
  @token = nil
  @config = nil
  @opts = default_opts
end

Class Method Details

.instanceObject



7
8
9
# File 'lib/dina/authentication/authentication.rb', line 7

def instance
  Thread.current[:dina_authentication] ||= new
end

Instance Method Details

#configObject



18
19
20
# File 'lib/dina/authentication/authentication.rb', line 18

def config
  @config ||= OpenStruct.new(@opts)
end

#config=(opts = {}) ⇒ Object

Sets Authentication configuration Options hash as follows:

token_store_file: "file to store the token",
user: "username provided by DINA admin in Keycloak",
password: "password provided by DINA admin in Keycloak",
client_id: "provided by DINA admin in Keycloak",
endpoint_url: "DINA API URL without terminating slash",
authorization_url: "Keycloak authorization URL without terminating slash".
realm: "provided by DINA admin in Keycloak"
verify_ssl: true

Parameters:

  • options (Hash)

    the configuration options

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dina/authentication/authentication.rb', line 36

def config=(opts = {})
  raise ConfigItemMissing, "Missing token_store_file from config." unless opts[:token_store_file]
  raise ConfigItemMissing, "Missing user from config." unless opts[:user]
  raise ConfigItemMissing, "Missing password from config." unless opts[:password]
  raise ConfigItemMissing, "Missing client_id from config." unless opts[:client_id]
  raise ConfigItemMissing, "Missing endpoint_url from config." unless opts[:endpoint_url]
  raise ConfigItemMissing, "Missing authorization_url from config." unless opts[:authorization_url]
  raise ConfigItemMissing, "Missing realm from config." unless opts[:realm]

  if !opts[:token_store_file].instance_of?(String) || !::File.exist?(opts[:token_store_file])
    raise TokenStoreFileNotFound
  end

  @token = nil
  @config = nil
  @opts.merge!(opts)
  Keycloak.auth_server_url = config.authorization_url
  Keycloak.realm = config.realm

  if opts.key?(:verify_ssl) && opts[:verify_ssl] == false
    Dina::BaseModel.connection_options[:ssl] = { verify: false }
  end

  if ::File.zero?(config.token_store_file)
    save_token(hash: empty_token)
  end
end

#flushObject

Save default values in token store file



80
81
82
# File 'lib/dina/authentication/authentication.rb', line 80

def flush
  save_token(hash: empty_token)
end

#flush_configObject



84
85
86
87
88
# File 'lib/dina/authentication/authentication.rb', line 84

def flush_config
  @opts = default_opts
  @config = nil
  @token = nil
end

#headerString

Gets, sets, and renews a Bearer access token as required and produces a Bearer string

Returns:

  • (String)

    the Bearer token



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dina/authentication/authentication.rb', line 67

def header
  if access_token.nil? || refresh_token.nil?
    get_token
  end

  if Time.now >= Time.parse(auth_expiry)
    renew_token
  end

  "Bearer " + access_token
end