Module: Elmas::OAuth

Included in:
API
Defined in:
lib/elmas/oauth.rb

Overview

rubocop:disable Metrics/ModuleLength

Instance Method Summary collapse

Instance Method Details

#authorize(user_name, password, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/elmas/oauth.rb', line 18

def authorize(user_name, password, options = {})
  warn "[DEPRECATION] `authorize` is deprecated. Please implement your own authorization methods instead."
  agent = Mechanize.new

  (agent, user_name, password, options)
  allow_access(agent)

  code = URI.unescape(agent.page.uri.query.split("=").last)
  OauthResponse.new(get_access_token(code))
end

#authorize_divisionObject



47
48
49
# File 'lib/elmas/oauth.rb', line 47

def authorize_division
  get("/Current/Me", no_division: true).results.first.current_division
end

#authorize_url(options = {}) ⇒ Object

Return URL for OAuth authorization



63
64
65
66
67
68
69
70
# File 'lib/elmas/oauth.rb', line 63

def authorize_url(options = {})
  options[:response_type] ||= "code"
  options[:redirect_uri] ||= redirect_uri
  params = authorization_params.merge(options)
  uri = URI("#{base_url}/api/oauth2/auth/")
  uri.query = URI.encode_www_form(params)
  uri.to_s
end

#authorized?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
# File 'lib/elmas/oauth.rb', line 39

def authorized?
  # Do a test call, return false if 401 or any error code
  get("/Current/Me", no_division: true)
rescue BadRequestException
  Elmas.error "Not yet authorized"
  return false
end

#auto_authorizeObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/elmas/oauth.rb', line 51

def auto_authorize
  warn "[DEPRECATION] `auto_authorize` is deprecated. Please implement your own authorization methods instead."
  Elmas.configure do |config|
    config.redirect_uri = ENV["REDIRECT_URI"]
    config.client_id = ENV["CLIENT_ID"]
    config.client_secret = ENV["CLIENT_SECRET"]
    config.access_token = Elmas.authorize(ENV["EXACT_USER_NAME"], ENV["EXACT_PASSWORD"]).access_token
    config.division = Elmas.authorize_division
  end
end

#get_access_token(code, _options = {}) ⇒ Object

Return an access token from authorization



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/elmas/oauth.rb', line 73

def get_access_token(code, _options = {})
  conn = Faraday.new(url: base_url) do |faraday|
    faraday.request :url_encoded
    faraday.adapter Faraday.default_adapter
  end
  params = access_token_params(code)
  conn.post do |req|
    req.url "/api/oauth2/token"
    req.body = params
    req.headers["Accept"] = "application/json"
  end
end

#get_refresh_token(refresh_token) ⇒ Object

Return an access token from authorization via refresh token



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/elmas/oauth.rb', line 87

def get_refresh_token(refresh_token)
  conn = Faraday.new(url: config[:base_url]) do |faraday|
    faraday.request :url_encoded
    faraday.adapter Faraday.default_adapter
  end

  params = refresh_access_token_params(refresh_token)

  conn.post do |req|
    req.url "/api/oauth2/token"
    req.body = params
    req.headers["Accept"] = "application/json"
    req.headers["Content-Type"] = "application/x-www-form-urlencoded"
  end
end

#refresh_authorizationObject



29
30
31
32
33
34
35
36
37
# File 'lib/elmas/oauth.rb', line 29

def refresh_authorization
  warn "[DEPRECATION] `refresh_authorization` is deprecated. Please implement your own authorization methods instead."
  OauthResponse.new(get_refresh_token(refresh_token)).tap do |response|
    Elmas.configure do |config|
      config.access_token = response.access_token
      config.refresh_token = response.refresh_token
    end
  end
end