Module: Dina::Authentication
- Defined in:
- lib/dina/authentication/authentication.rb
Class Attribute Summary collapse
-
.endpoint_url ⇒ Object
Returns the value of attribute endpoint_url.
Class Method Summary collapse
-
.config(options = {}) ⇒ 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”, server_name: “used locally to reference the token”, 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” }.
-
.flush ⇒ Object
Flush instance variables and save default values in token store file.
-
.header ⇒ String
Gets, sets, and renews a Bearer access token as required and produces a Header string.
Class Attribute Details
.endpoint_url ⇒ Object
Returns the value of attribute endpoint_url.
74 75 76 |
# File 'lib/dina/authentication/authentication.rb', line 74 def endpoint_url @endpoint_url end |
Class Method Details
.config(options = {}) ⇒ 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",
server_name: "used locally to reference the token",
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"
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/dina/authentication/authentication.rb', line 20 def self.config( = {}) raise ConfigItemMissing, "Missing token_store_file from config." unless [:token_store_file] raise ConfigItemMissing, "Missing user from config." unless [:user] raise ConfigItemMissing, "Missing password from config." unless [:password] raise ConfigItemMissing, "Missing server_name from config." unless [:server_name] raise ConfigItemMissing, "Missing client_id from config." unless [:client_id] raise ConfigItemMissing, "Missing endpoint_url from config." unless [:endpoint_url] raise ConfigItemMissing, "Missing authorization_url from config." unless [:authorization_url] raise ConfigItemMissing, "Missing realm from config." unless [:realm] if ![:token_store_file].instance_of?(String) || !::File.exist?([:token_store_file]) raise TokenStoreFileNotFound end @token = nil @token_store_file = [:token_store_file] @user = [:user] @password = [:password] @server_name = [:server_name] @client_id = [:client_id] @endpoint_url = [:endpoint_url] Keycloak.auth_server_url = [:authorization_url] Keycloak.realm = [:realm] if ::File.zero?(@token_store_file) write_token(data: empty_token) end end |
.flush ⇒ Object
Flush instance variables and save default values in token store file
69 70 71 |
# File 'lib/dina/authentication/authentication.rb', line 69 def self.flush write_token(data: empty_token) end |
.header ⇒ String
Gets, sets, and renews a Bearer access token as required and produces a Header string
WARNING: this is not likely to be threadsafe unless we do away with @token and load the token_store_file with every call to header
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/dina/authentication/authentication.rb', line 56 def self.header if access_token.nil? || refresh_token.nil? set_token end if Time.now >= Time.parse(auth_expiry) renew_token end "Bearer " + access_token end |