Class: TeamSnap::AuthMiddleware
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- TeamSnap::AuthMiddleware
- Defined in:
- lib/teamsnap/auth_middleware.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
- #client_id ⇒ Object
- #client_secret ⇒ Object
- #digest ⇒ Object
-
#initialize(app, options) ⇒ AuthMiddleware
constructor
A new instance of AuthMiddleware.
- #message(env) ⇒ Object
- #message_hash(env) ⇒ Object
- #query_string(env) ⇒ Object
- #token ⇒ Object
Constructor Details
#initialize(app, options) ⇒ AuthMiddleware
Returns a new instance of AuthMiddleware.
5 6 7 8 |
# File 'lib/teamsnap/auth_middleware.rb', line 5 def initialize(app, ) = super(app) end |
Instance Method Details
#call(env) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/teamsnap/auth_middleware.rb', line 10 def call(env) if token env[:request_headers].merge!({"Authorization" => "Bearer #{token}"}) elsif client_id && client_secret query_params = Hash[URI.decode_www_form(env.url.query || "")] .merge({ hmac_client_id: client_id, hmac_nonce: SecureRandom.uuid, hmac_timestamp: Time.now.to_i }) env.url.query = URI.encode_www_form(query_params) env.request_headers["X-Teamsnap-Hmac"] = OpenSSL::HMAC.hexdigest( digest, client_secret, (env) ) end @app.call(env) end |
#client_id ⇒ Object
34 35 36 |
# File 'lib/teamsnap/auth_middleware.rb', line 34 def client_id @client_id ||= [:client_id] end |
#client_secret ⇒ Object
38 39 40 |
# File 'lib/teamsnap/auth_middleware.rb', line 38 def client_secret @client_secret ||= [:client_secret] end |
#digest ⇒ Object
42 43 44 |
# File 'lib/teamsnap/auth_middleware.rb', line 42 def digest OpenSSL::Digest.new("sha256") end |
#message(env) ⇒ Object
56 57 58 |
# File 'lib/teamsnap/auth_middleware.rb', line 56 def (env) env.body.to_s || "" end |
#message_hash(env) ⇒ Object
46 47 48 49 50 |
# File 'lib/teamsnap/auth_middleware.rb', line 46 def (env) digest.hexdigest( query_string(env) + (env) ) end |
#query_string(env) ⇒ Object
52 53 54 |
# File 'lib/teamsnap/auth_middleware.rb', line 52 def query_string(env) "/?" + env.url.query.to_s end |
#token ⇒ Object
30 31 32 |
# File 'lib/teamsnap/auth_middleware.rb', line 30 def token @token ||= [:token] end |