Class: ChalkRuby::Grpc::AuthInterceptor
- Inherits:
-
GRPC::ClientInterceptor
- Object
- GRPC::ClientInterceptor
- ChalkRuby::Grpc::AuthInterceptor
- Defined in:
- lib/chalk_ruby/grpc/auth_interceptor.rb
Instance Method Summary collapse
-
#initialize(auth_stub, client_id, client_secret, environment_id) ⇒ AuthInterceptor
constructor
A new instance of AuthInterceptor.
- #request_response(request:, call:, method:, metadata:) ⇒ Object
Constructor Details
#initialize(auth_stub, client_id, client_secret, environment_id) ⇒ AuthInterceptor
Returns a new instance of AuthInterceptor.
8 9 10 11 12 13 14 15 |
# File 'lib/chalk_ruby/grpc/auth_interceptor.rb', line 8 def initialize(auth_stub, client_id, client_secret, environment_id) @auth_stub = auth_stub @client_id = client_id @client_secret = client_secret @environment_id = environment_id @token = nil @token_expiry = nil end |
Instance Method Details
#request_response(request:, call:, method:, metadata:) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/chalk_ruby/grpc/auth_interceptor.rb', line 17 def request_response(request:, call:, method:, metadata:) # Leave 1 minute buffer before refreshing auth token if @token.nil? || @token_expiry.nil? || Time.now >= (@token_expiry - 60) response = @auth_stub.get_token( Chalk::Server::V1::GetTokenRequest.new( client_id: @client_id, client_secret: @client_secret ) ) @token = response.access_token # expires_in assumes to be 'seconds' @token_expiry = Time.now + response.expires_in end # Add the token to the request's metadata ["authorization"] = "Bearer #{@token}" ["x-chalk-deployment-type"] = "engine-grpc" ["x-chalk-env-id"] = @environment_id # Proceed with the original call yield end |