Class: PusherPlatform::Authenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/pusher-platform/authenticator.rb

Instance Method Summary collapse

Constructor Details

#initialize(instance_id, key_id, key_secret) ⇒ Authenticator

Returns a new instance of Authenticator.



11
12
13
14
15
16
17
# File 'lib/pusher-platform/authenticator.rb', line 11

def initialize(instance_id, key_id, key_secret)
  @instance_id = instance_id
  @key_id = key_id
  @key_secret = key_secret
  # see https://github.com/rack/rack/blob/5559676e7b5a3107d39552285ce8b714b672bde6/lib/rack/utils.rb#L27
  @query_parser = QueryParser.make_default(65536, 100)
end

Instance Method Details

#authenticate(auth_payload, options) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pusher-platform/authenticator.rb', line 19

def authenticate(auth_payload, options)
  grant_type = auth_payload['grant_type'] || auth_payload[:grant_type]

  unless grant_type == "client_credentials"
    return AuthenticationResponse.new({
      status: 422,
      body: {
        error: 'token_provider/invalid_grant_type',
        error_description: "The grant_type provided, #{grant_type}, is unsupported"
      }
    })
  end

  authenticate_using_client_credentials(options)
end

#authenticate_with_refresh_token(auth_payload, options) ⇒ Object



40
41
42
# File 'lib/pusher-platform/authenticator.rb', line 40

def authenticate_with_refresh_token(auth_payload, options)
  authenticate_based_on_grant_type(auth_payload, options)
end

#authenticate_with_refresh_token_and_request(request, options) ⇒ Object



44
45
46
47
# File 'lib/pusher-platform/authenticator.rb', line 44

def authenticate_with_refresh_token_and_request(request, options)
  auth_data = @query_parser.parse_nested_query request.body.read
  authenticate_based_on_grant_type(auth_data, options)
end

#authenticate_with_request(request, options) ⇒ Object



35
36
37
38
# File 'lib/pusher-platform/authenticator.rb', line 35

def authenticate_with_request(request, options)
  auth_data = @query_parser.parse_nested_query request.body.read
  authenticate(auth_data, options)
end

#generate_access_token(options) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pusher-platform/authenticator.rb', line 49

def generate_access_token(options)
  now = Time.now.utc.to_i

  claims = {
    instance: @instance_id,
    iss: "api_keys/#{@key_id}",
    iat: now,
    exp: now + TOKEN_EXPIRY
  }

  claims.merge!({ sub: options[:user_id] }) unless options[:user_id].nil?
  claims.merge!({ su: true }) if options[:su]
  claims.merge!(options[:service_claims]) if options[:service_claims]

  {
    token: JWT.encode(claims, @key_secret, 'HS256'),
    expires_in: TOKEN_EXPIRY
  }
end