Class: DuoUniversalRails::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/duo_universal_rails/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#adapterObject (readonly)

Returns the value of attribute adapter.



8
9
10
# File 'lib/duo_universal_rails/client.rb', line 8

def adapter
  @adapter
end

#api_hostnameObject (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_idObject (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_secretObject (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_uriObject (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

#stateObject (readonly)

Returns the value of attribute state.



8
9
10
# File 'lib/duo_universal_rails/client.rb', line 8

def state
  @state
end

#time_nowObject (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_stateObject



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

#authObject



68
69
70
# File 'lib/duo_universal_rails/client.rb', line 68

def auth 
  AuthRequest.new(self)
end

#auth_pathObject



40
41
42
# File 'lib/duo_universal_rails/client.rb', line 40

def auth_path
  Constant::OAUTH_V1_AUTHORIZE_ENDPOINT
end

#auth_urlObject



44
45
46
# File 'lib/duo_universal_rails/client.rb', line 44

def auth_url
  [https_api_hostname, auth_path].join("")
end

#connectionObject



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_secObject



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_checkObject



64
65
66
# File 'lib/duo_universal_rails/client.rb', line 64

def health_check
  HealthCheckRequest.new(self)
end

#health_check_pathObject



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_urlObject



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_hostnameObject



28
29
30
# File 'lib/duo_universal_rails/client.rb', line 28

def https_api_hostname 
  "https://#{api_hostname}"
end

#inspectObject



91
92
93
# File 'lib/duo_universal_rails/client.rb', line 91

def inspect
  "#<DuoUniversalRails::Client>"
end

#time_now_in_secObject



56
57
58
# File 'lib/duo_universal_rails/client.rb', line 56

def time_now_in_sec 
  time_now.to_i
end

#tokenObject



72
73
74
# File 'lib/duo_universal_rails/client.rb', line 72

def token 
  TokenRequest.new(self)
end

#token_pathObject



48
49
50
# File 'lib/duo_universal_rails/client.rb', line 48

def token_path
  Constant::OAUTH_V1_TOKEN_ENDPOINT
end

#token_urlObject



52
53
54
# File 'lib/duo_universal_rails/client.rb', line 52

def token_url
  [https_api_hostname, token_path].join("")
end