Module: Eol::OAuth

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

Overview

rubocop:disable Metrics/ModuleLength

Instance Method Summary collapse

Instance Method Details

#authorize_divisionObject



27
28
29
# File 'lib/eol/oauth.rb', line 27

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

#authorize_url(options = {}) ⇒ Object

Return URL for OAuth authorization



32
33
34
35
36
37
38
39
# File 'lib/eol/oauth.rb', line 32

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)


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

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

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

Return an access token from authorization



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/eol/oauth.rb', line 42

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/eol/oauth.rb', line 56

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