Class: DuoUniversalRails::Client
- Inherits:
-
Object
- Object
- DuoUniversalRails::Client
- Defined in:
- lib/duo_universal_rails/client.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#api_hostname ⇒ Object
readonly
Returns the value of attribute api_hostname.
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
readonly
Returns the value of attribute client_secret.
-
#redirect_uri ⇒ Object
readonly
Returns the value of attribute redirect_uri.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#time_now ⇒ Object
readonly
Returns the value of attribute time_now.
Class Method Summary collapse
Instance Method Summary collapse
- #auth ⇒ Object
- #auth_path ⇒ Object
- #auth_url ⇒ Object
- #connection ⇒ Object
-
#create_jwt_payload(payload:) ⇒ Object
The secret must be a string.
- #decode_jwt_token(token: nil) ⇒ Object
- #expire_in_sec ⇒ Object
- #health_check ⇒ Object
- #health_check_path ⇒ Object
- #health_check_url ⇒ Object
- #https_api_hostname ⇒ Object
-
#initialize(client_id:, client_secret:, api_hostname:, redirect_uri:, time_now: Time.now, state: Client.generate_state, adapter: Faraday.default_adapter) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ Object
- #time_now_in_sec ⇒ Object
- #token ⇒ Object
- #token_path ⇒ Object
- #token_url ⇒ Object
Constructor Details
#initialize(client_id:, client_secret:, api_hostname:, redirect_uri:, time_now: Time.now, state: Client.generate_state, adapter: Faraday.default_adapter) ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 15 16 17 18 |
# File 'lib/duo_universal_rails/client.rb', line 10 def initialize(client_id:, client_secret:, api_hostname:, redirect_uri:, time_now: Time.now, state: Client.generate_state, adapter: Faraday.default_adapter) @client_id = client_id @client_secret = client_secret @api_hostname = api_hostname @redirect_uri = redirect_uri @time_now = time_now @state = state @adapter = adapter end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
8 9 10 |
# File 'lib/duo_universal_rails/client.rb', line 8 def adapter @adapter end |
#api_hostname ⇒ Object (readonly)
Returns the value of attribute api_hostname.
8 9 10 |
# File 'lib/duo_universal_rails/client.rb', line 8 def api_hostname @api_hostname end |
#client_id ⇒ Object (readonly)
Returns the value of attribute client_id.
8 9 10 |
# File 'lib/duo_universal_rails/client.rb', line 8 def client_id @client_id end |
#client_secret ⇒ Object (readonly)
Returns the value of attribute client_secret.
8 9 10 |
# File 'lib/duo_universal_rails/client.rb', line 8 def client_secret @client_secret end |
#redirect_uri ⇒ Object (readonly)
Returns the value of attribute redirect_uri.
8 9 10 |
# File 'lib/duo_universal_rails/client.rb', line 8 def redirect_uri @redirect_uri end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
8 9 10 |
# File 'lib/duo_universal_rails/client.rb', line 8 def state @state end |
#time_now ⇒ Object (readonly)
Returns the value of attribute time_now.
8 9 10 |
# File 'lib/duo_universal_rails/client.rb', line 8 def time_now @time_now end |
Class Method Details
.generate_state ⇒ Object
21 22 23 24 |
# File 'lib/duo_universal_rails/client.rb', line 21 def generate_state # Generate JTI_LENGTH length random value SecureRandom.alphanumeric(Constant::JTI_LENGTH) end |
Instance Method Details
#auth ⇒ Object
68 69 70 |
# File 'lib/duo_universal_rails/client.rb', line 68 def auth AuthRequest.new(self) end |
#auth_path ⇒ Object
40 41 42 |
# File 'lib/duo_universal_rails/client.rb', line 40 def auth_path Constant::OAUTH_V1_AUTHORIZE_ENDPOINT end |
#auth_url ⇒ Object
44 45 46 |
# File 'lib/duo_universal_rails/client.rb', line 44 def auth_url [https_api_hostname, auth_path].join("") end |
#connection ⇒ Object
85 86 87 88 89 |
# File 'lib/duo_universal_rails/client.rb', line 85 def connection @connection ||= Faraday.new(https_api_hostname) do |f| f.adapter adapter # adds the adapter to the connection, defaults to `Faraday.default_adapter` end end |
#create_jwt_payload(payload:) ⇒ Object
The secret must be a string. A JWT::DecodeError will be raised if it isn’t provided.
77 78 79 |
# File 'lib/duo_universal_rails/client.rb', line 77 def create_jwt_payload(payload:) JWT.encode payload, client_secret, Constant::SIG_ALGORITHM end |
#decode_jwt_token(token: nil) ⇒ Object
81 82 83 |
# File 'lib/duo_universal_rails/client.rb', line 81 def decode_jwt_token(token: nil) JWT.decode token, client_secret, true, { algorithm: Constant::SIG_ALGORITHM } end |
#expire_in_sec ⇒ Object
60 61 62 |
# File 'lib/duo_universal_rails/client.rb', line 60 def expire_in_sec time_now_in_sec + Constant::FIVE_MINUTES_IN_SECONDS end |
#health_check ⇒ Object
64 65 66 |
# File 'lib/duo_universal_rails/client.rb', line 64 def health_check HealthCheckRequest.new(self) end |
#health_check_path ⇒ Object
32 33 34 |
# File 'lib/duo_universal_rails/client.rb', line 32 def health_check_path Constant::OAUTH_V1_HEALTH_CHECK_ENDPOINT end |
#health_check_url ⇒ Object
36 37 38 |
# File 'lib/duo_universal_rails/client.rb', line 36 def health_check_url [https_api_hostname, health_check_path].join("") end |
#https_api_hostname ⇒ Object
28 29 30 |
# File 'lib/duo_universal_rails/client.rb', line 28 def https_api_hostname "https://#{api_hostname}" end |
#inspect ⇒ Object
91 92 93 |
# File 'lib/duo_universal_rails/client.rb', line 91 def inspect "#<DuoUniversalRails::Client>" end |
#time_now_in_sec ⇒ Object
56 57 58 |
# File 'lib/duo_universal_rails/client.rb', line 56 def time_now_in_sec time_now.to_i end |
#token ⇒ Object
72 73 74 |
# File 'lib/duo_universal_rails/client.rb', line 72 def token TokenRequest.new(self) end |
#token_path ⇒ Object
48 49 50 |
# File 'lib/duo_universal_rails/client.rb', line 48 def token_path Constant::OAUTH_V1_TOKEN_ENDPOINT end |
#token_url ⇒ Object
52 53 54 |
# File 'lib/duo_universal_rails/client.rb', line 52 def token_url [https_api_hostname, token_path].join("") end |