Class: FieldView::AuthToken
- Inherits:
-
Object
- Object
- FieldView::AuthToken
- Defined in:
- lib/fieldview/auth_token.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#access_token_expiration_at ⇒ Object
Returns the value of attribute access_token_expiration_at.
-
#last_request ⇒ Object
Returns the value of attribute last_request.
-
#last_request_headers ⇒ Object
Returns the value of attribute last_request_headers.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
#refresh_token_expiration_at ⇒ Object
Returns the value of attribute refresh_token_expiration_at.
Class Method Summary collapse
- .build_token_request(url_params) ⇒ Object
-
.new_auth_token_with_code_from_redirect_code(code, redirect_uri: nil) ⇒ Object
Code will be collected from the redirect to your server.
Instance Method Summary collapse
- #access_token_expired? ⇒ Boolean
- #execute_request!(method, path, headers: {}, params: {}) ⇒ Object
-
#initialize(params) ⇒ AuthToken
constructor
A new instance of AuthToken.
- #refresh_access_token! ⇒ Object
- #refresh_token_expired? ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(params) ⇒ AuthToken
Returns a new instance of AuthToken.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/fieldview/auth_token.rb', line 6 def initialize(params) # Assume now was 5 seconds ago now = FieldView.get_now_for_auth_token - 5 # Used on every request self.access_token = params[:access_token] # When the access token expires we'll need to refresh, # can be specified as part of the object, 14399 is what is being # returned at the time of writing this (2017-04-11) if params[:access_token_expiration_at].is_a?(String) then self.access_token_expiration_at = Time.parse(params[:access_token_expiration_at]) else self.access_token_expiration_at = params[:access_token_expiration_at] || (now + (params[:expires_in]||14399)) end # Refresh token isn't required, but can be initialized with this self.refresh_token = params[:refresh_token] # Refresh token technically expires in 30 days if params[:refresh_token_expiration_at].is_a?(String) then self.refresh_token_expiration_at = Time.parse(params[:refresh_token_expiration_at]) else self.refresh_token_expiration_at = params[:refresh_token_expiration_at] || (now + (30)*24*60*60) end end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
3 4 5 |
# File 'lib/fieldview/auth_token.rb', line 3 def access_token @access_token end |
#access_token_expiration_at ⇒ Object
Returns the value of attribute access_token_expiration_at.
3 4 5 |
# File 'lib/fieldview/auth_token.rb', line 3 def access_token_expiration_at @access_token_expiration_at end |
#last_request ⇒ Object
Returns the value of attribute last_request.
4 5 6 |
# File 'lib/fieldview/auth_token.rb', line 4 def last_request @last_request end |
#last_request_headers ⇒ Object
Returns the value of attribute last_request_headers.
5 6 7 |
# File 'lib/fieldview/auth_token.rb', line 5 def last_request_headers @last_request_headers end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
3 4 5 |
# File 'lib/fieldview/auth_token.rb', line 3 def refresh_token @refresh_token end |
#refresh_token_expiration_at ⇒ Object
Returns the value of attribute refresh_token_expiration_at.
3 4 5 |
# File 'lib/fieldview/auth_token.rb', line 3 def refresh_token_expiration_at @refresh_token_expiration_at end |
Class Method Details
.build_token_request(url_params) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/fieldview/auth_token.rb', line 111 def self.build_token_request(url_params) uri = URI.parse(FieldView.oauth_token_base) new_query_ar = URI.decode_www_form(uri.query || '') url_params.each do |param| new_query_ar << param end uri.query = URI.encode_www_form(new_query_ar) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Post.new(uri.request_uri) request["Accept"] = "*/*" request["Authorization"] = "Basic #{Base64.encode64("#{FieldView.client_id}:#{FieldView.client_secret}").strip}" request["Content-Type"] = "application/x-www-form-urlencoded" return http, request end |
.new_auth_token_with_code_from_redirect_code(code, redirect_uri: nil) ⇒ Object
Code will be collected from the redirect to your server
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/fieldview/auth_token.rb', line 128 def self.new_auth_token_with_code_from_redirect_code(code, redirect_uri: nil) http, request = build_token_request([ ["grant_type", "authorization_code"], ["redirect_uri", redirect_uri || FieldView.redirect_uri], ["code", code] ]) response = http.request(request) if response.code != "200" then raise AuthenticationError.new("Was unable to get a new auth token using the code provided. " \ "See response body: #{response.body}") else json = JSON.parse(response.body, symbolize_names: true) return AuthToken.new(json) end end |
Instance Method Details
#access_token_expired? ⇒ Boolean
33 34 35 |
# File 'lib/fieldview/auth_token.rb', line 33 def access_token_expired? return !!(self.access_token.nil? || self.access_token_expiration_at <= Time.now) end |
#execute_request!(method, path, headers: {}, params: {}) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/fieldview/auth_token.rb', line 56 def execute_request!(method, path, headers: {}, params: {}) next_token_header = headers[FieldView::NEXT_TOKEN_HEADER_KEY] # Upon initial request the api no longer wants us to send anything if next_token_header.to_s == "" then headers.delete(FieldView::NEXT_TOKEN_HEADER_KEY) end self.last_request = path if access_token_expired? && refresh_token_expired? then raise AllTokensExpiredError.new("All of your tokens have expired. " \ "You'll need to re-log into FieldView.") end if access_token_expired? then refresh_access_token!() end uri = URI.parse("#{FieldView.api_base}#{FieldView.api_version}/#{path}") # TODO: Handle parameters http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = FieldView.api_base.start_with?("https") request = Net::HTTP.class_eval(method.to_s.capitalize).new(uri.request_uri) if method == :get then new_query_ar = URI.decode_www_form(uri.query || '') params.each do |key,value| new_query_ar << [key.to_s, value.to_s] end uri.query = URI.encode_www_form(new_query_ar) request = Net::HTTP.class_eval(method.to_s.capitalize).new(uri.request_uri) else if params.is_a?(Hash) request.body = params.to_json() else request.body = params end end request["Accept"] = "*/*" request["Authorization"] = "Bearer #{self.access_token}" request["X-Api-Key"] = FieldView.x_api_key request["Content-Type"] = "application/json" headers.each do |header,value| request[header] = value.to_s end self.last_request_headers = request.to_hash response = nil begin response = http.request(request) rescue Zlib::DataError => e raise BadRequestError.new("A data error has occurred with Zlib while making the request, check the headers: #{request.to_hash}", http_body: params) end FieldView.handle_response_error_codes(response) return FieldViewResponse.new(response) end |
#refresh_access_token! ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/fieldview/auth_token.rb', line 41 def refresh_access_token!() http, request = self.class.build_token_request([ ["grant_type", "refresh_token"], ["refresh_token", self.refresh_token] ]) response = http.request(request) if response.code != "200" then # An error has occurred, log to roll bar and notify them raise RefreshTokenError.new("Failed to refresh FieldView token, response body: #{response.body}") else json = JSON.parse(response.body, symbolize_names: true) initialize(json) end end |
#refresh_token_expired? ⇒ Boolean
37 38 39 |
# File 'lib/fieldview/auth_token.rb', line 37 def refresh_token_expired? return !!(self.refresh_token.nil? || self.refresh_token_expiration_at <= Time.now) end |
#to_s ⇒ Object
144 145 146 147 148 149 |
# File 'lib/fieldview/auth_token.rb', line 144 def to_s() [ "AccessToken: #{self.access_token}, Expiration: #{self.access_token_expiration_at}", "RefreshToken: #{self.refresh_token}, Expiration: #{self.refresh_token_expiration_at}" ].join('\n') end |