Sha256: 94ef13d6177a1b1795d8609297a76a3565140e09eacf3504fe9c182b23b5c5dd

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module Nylas
  # Authenticate your application using the native interface
  # @see https://docs.nylas.com/reference#native-authentication-1
  class NativeAuthentication
    attr_accessor :api

    def initialize(api:)
      self.api = api
    end

    def authenticate(name:, email_address:, provider:, settings:, reauth_account_id: nil,
                     scopes: nil, return_full_response: false)
      scopes ||= %w[email calendar contacts]
      scopes = scopes.join(",") unless scopes.is_a?(String)
      code = retrieve_code(name: name, email_address: email_address, provider: provider,
                           settings: settings, reauth_account_id: reauth_account_id, scopes: scopes)

      exchange_code_for_access_token(code, return_full_response)
    end

    private

    def retrieve_code(name:, email_address:, provider:, settings:, reauth_account_id:, scopes:)
      payload = { client_id: api.client.app_id, name: name, email_address: email_address,
                  provider: provider, settings: settings, scopes: scopes }
      payload[:reauth_account_id] = reauth_account_id
      response = api.execute(method: :post, path: "/connect/authorize", payload: JSON.dump(payload))
      response[:code]
    end

    def exchange_code_for_access_token(code, return_full_response)
      payload = { client_id: api.client.app_id, client_secret: api.client.app_secret, code: code }
      response = api.execute(method: :post, path: "/connect/token", payload: JSON.dump(payload))
      return_full_response ? response : response[:access_token]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nylas-5.17.0 lib/nylas/native_authentication.rb