Module: Elmas::OAuth
- Included in:
- API
- Defined in:
- lib/elmas/oauth.rb
Overview
rubocop:disable Metrics/ModuleLength
Instance Method Summary collapse
- #authorize(user_name, password, options = {}) ⇒ Object
- #authorize_division ⇒ Object
-
#authorize_url(options = {}) ⇒ Object
Return URL for OAuth authorization.
- #authorized? ⇒ Boolean
- #auto_authorize ⇒ Object
-
#get_access_token(code, _options = {}) ⇒ Object
Return an access token from authorization.
-
#get_refresh_token(refresh_token) ⇒ Object
Return an access token from authorization via refresh token.
- #refresh_authorization ⇒ Object
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 (user_name, password, = {}) warn "[DEPRECATION] `authorize` is deprecated. Please implement your own authorization methods instead." agent = Mechanize.new login(agent, user_name, password, ) allow_access(agent) code = URI.unescape(agent.page.uri.query.split("=").last) OauthResponse.new(get_access_token(code)) end |
#authorize_division ⇒ Object
47 48 49 |
# File 'lib/elmas/oauth.rb', line 47 def 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 ( = {}) [:response_type] ||= "code" [:redirect_uri] ||= redirect_uri params = .merge() uri = URI("#{base_url}/api/oauth2/auth/") uri.query = URI.encode_www_form(params) uri.to_s end |
#authorized? ⇒ Boolean
39 40 41 42 43 44 45 |
# File 'lib/elmas/oauth.rb', line 39 def # 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_authorize ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/elmas/oauth.rb', line 51 def 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.(ENV["EXACT_USER_NAME"], ENV["EXACT_PASSWORD"]).access_token config.division = Elmas. 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, = {}) 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_authorization ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/elmas/oauth.rb', line 29 def 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 |