Class: Her::Middleware::OAuthProviderHeader

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/her/middleware/o_auth_provider_header.rb

Overview

OAuthProviderHeader

Instance Method Summary collapse

Instance Method Details

#add_header(headers) ⇒ Object


5
6
7
# File 'lib/her/middleware/o_auth_provider_header.rb', line 5

def add_header(headers)
  headers.merge! Authorization: "Bearer #{refresh_token['access_token']}"
end

#call(env) ⇒ Object


45
46
47
48
# File 'lib/her/middleware/o_auth_provider_header.rb', line 45

def call(env)
  add_header(env[:request_headers])
  @app.call(env)
end

#passwordObject


39
40
41
42
43
# File 'lib/her/middleware/o_auth_provider_header.rb', line 39

def password
  uri = URI("#{token_url}?#{URI.encode_www_form(password_params)}")
  res = Net::HTTP.post_form(uri, {})
  JSON.parse(res.body)
end

#password_paramsObject


24
25
26
27
28
29
30
31
32
33
# File 'lib/her/middleware/o_auth_provider_header.rb', line 24

def password_params
  {
    client_id: Parasut.options.client_id,
    client_secret: Parasut.options.client_secret,
    username: Parasut.options.username,
    password: Parasut.options.password,
    grant_type: 'password',
    redirect_uri: 'urn:ietf:wg:oauth:2.0:oob'
  }
end

#refresh_tokenObject


9
10
11
12
13
# File 'lib/her/middleware/o_auth_provider_header.rb', line 9

def refresh_token
  uri = URI("#{token_url}?#{URI.encode_www_form(refresh_token_params)}")
  res = Net::HTTP.post_form(uri, {})
  JSON.parse(res.body)
end

#refresh_token_paramsObject


15
16
17
18
19
20
21
22
# File 'lib/her/middleware/o_auth_provider_header.rb', line 15

def refresh_token_params
  {
    client_id: Parasut.options.client_id,
    client_secret: Parasut.options.client_secret,
    grant_type: 'refresh_token',
    refresh_token: password['refresh_token']
  }
end

#token_urlObject


35
36
37
# File 'lib/her/middleware/o_auth_provider_header.rb', line 35

def token_url
  'https://api.parasut.com/oauth/token'
end