Class: ShotgridApiRuby::Auth

Inherits:
Faraday::Middleware
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/shotgrid_api_ruby/auth.rb

Overview

Faraday middleware responsible for authentication with the shotgrid site

Defined Under Namespace

Modules: Validator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, options = { auth: nil, site_url: nil }) ⇒ Auth

Returns a new instance of Auth.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/shotgrid_api_ruby/auth.rb', line 50

def initialize(app = nil, options = { auth: nil, site_url: nil })
  raise 'missing auth' unless options[:auth]
  raise 'missing site_url' unless options[:site_url]
  unless Validator.valid?(**options[:auth]&.transform_keys(&:to_sym))
    raise 'Auth not valid'
  end

  @site_url = T.let(options[:site_url], String)
  @client_id = T.let(options[:auth][:client_id], T.nilable(String))
  @client_secret = T.let(options[:auth][:client_secret], T.nilable(String))
  @username = T.let(options[:auth][:username], T.nilable(String))
  @password = T.let(options[:auth][:password], T.nilable(String))
  @session_token = T.let(options[:auth][:session_token], T.nilable(String))
  @refresh_token = T.let(options[:auth][:refresh_token], T.nilable(String))
  @app =
    T.let(
      nil,
      T.nilable(T.any(Faraday::Middleware, VCR::Middleware::Faraday)),
    )
  @auth_type = T.let(nil, T.nilable(String))
  @auth_params = T.let(nil, T.nilable(String))
  @auth_url = T.let(nil, T.nilable(String))
  @access_token = T.let(nil, T.nilable(String))
  @token_expiry = T.let(nil, T.nilable(Time))

  super(app)
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



79
80
81
# File 'lib/shotgrid_api_ruby/auth.rb', line 79

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



82
83
84
# File 'lib/shotgrid_api_ruby/auth.rb', line 82

def client_secret
  @client_secret
end

#passwordObject (readonly)

Returns the value of attribute password.



91
92
93
# File 'lib/shotgrid_api_ruby/auth.rb', line 91

def password
  @password
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



97
98
99
# File 'lib/shotgrid_api_ruby/auth.rb', line 97

def refresh_token
  @refresh_token
end

#session_tokenObject (readonly)

Returns the value of attribute session_token.



94
95
96
# File 'lib/shotgrid_api_ruby/auth.rb', line 94

def session_token
  @session_token
end

#site_urlObject (readonly)

Returns the value of attribute site_url.



85
86
87
# File 'lib/shotgrid_api_ruby/auth.rb', line 85

def site_url
  @site_url
end

#usernameObject (readonly)

Returns the value of attribute username.



88
89
90
# File 'lib/shotgrid_api_ruby/auth.rb', line 88

def username
  @username
end

Instance Method Details

#auth_typeObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/shotgrid_api_ruby/auth.rb', line 100

def auth_type
  @auth_type ||=
    if refresh_token
      'refresh_token'
    elsif client_id
      'client_credentials'
    elsif username
      'password'
    elsif session_token
      'session_token'
    else
      ''
    end
end

#call(request_env) ⇒ Object



116
117
118
119
120
# File 'lib/shotgrid_api_ruby/auth.rb', line 116

def call(request_env)
  request_env[:request_headers].merge!(std_headers)

  @app&.call(request_env)
end