Class: Rack::Blinkbox::Zuul::TokenDecoder

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/blinkbox/zuul/tokens/token_decoder.rb

Overview

Rack middleware for decoding blinkbox Zuul authentication tokens.

Instance Method Summary collapse

Constructor Details

#initialize(app, key_finder = nil) ⇒ TokenDecoder

Initialises a new token decoder.

Parameters:

  • app (??)

    The Rack application.

  • key_finder (#key_with_id) (defaults to: nil)

    The class that is used to find



16
17
18
19
# File 'lib/rack/blinkbox/zuul/tokens/token_decoder.rb', line 16

def initialize(app, key_finder = nil)
  @app = app
  @key_finder = key_finder || FileKeyFinder.new("./keys")
end

Instance Method Details

#call(env) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rack/blinkbox/zuul/tokens/token_decoder.rb', line 21

def call(env)
  access_token = extract_bearer_token(env)
  if access_token
    env["zuul.access_token"] = access_token
    begin
      env["zuul.claims"] = claims = decode_access_token(access_token)
      
      user_guid = claims["sub"]
      env["zuul.user_guid"] = user_guid
      env["zuul.user_id"] = user_guid.match(/\Aurn:blinkbox:zuul:user:(\d+)\Z/)[1]
      env["zuul.user_roles"] = claims["bb/rol"] || []

      client_guid = claims["bb/cid"]
      if client_guid
        env["zuul.client_guid"] = client_guid
        env["zuul.client_id"] = client_guid.match(/\Aurn:blinkbox:zuul:client:(\d+)\Z/)[1]
      end
    rescue => error
      env["zuul.error"] = error
    end
  end
  @app.call(env)   
end