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.



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

def initialize(instance_id, key_id, key_secret)
  @instance_id = instance_id
  @key_id = key_id
  @key_secret = key_secret
end

Instance Method Details

#authenticate(auth_payload, options) ⇒ Object



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

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



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

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



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

def authenticate_with_refresh_token_and_request(request, options)
  auth_data = Rack::Utils.parse_nested_query request.body.read
  authenticate_based_on_grant_type(auth_data, options)
end

#authenticate_with_request(request, options) ⇒ Object



32
33
34
35
# File 'lib/pusher-platform/authenticator.rb', line 32

def authenticate_with_request(request, options)
  auth_data = Rack::Utils.parse_nested_query request.body.read
  authenticate(auth_data, options)
end

#generate_access_token(options) ⇒ Object



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

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