Skip to content

Commit

Permalink
Merge pull request #3 from candanedo/feature/initializer_for_paragon_…
Browse files Browse the repository at this point in the history
…credentials

[FEATURE] Include useparagon/connect javascript assets
  • Loading branch information
candanedo authored Feb 16, 2024
2 parents c4011ef + 5a7426c commit a22c02a
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 20 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ Find your project ID in the Overview tab of any Integration.

Once this information is configured in your Rails project, you can start using the gem as needed.

## Javascript assets (Paragon SDK)

The UseParagon gem includes the JavaScript assets necessary for you to utilize the Paragon SDK. To use window.paragon.authenticate(project, token) or window.connect(integration_name, options) in your JavaScript, you need to require the assets.

#### Using Importmaps
If you're using importmaps, add the following line to your config/importmap.rb file:

```ruby
pin "useparagon/connect" # 3.1.2
```
This will ensure that the necessary JavaScript asset is included in your application.

If you're using Stimulus controllers, you can require the asset in your desired controllers:

```js
import "useparagon/connect"
```

### Workflow triggers
#### Request trigger

Expand Down
8 changes: 8 additions & 0 deletions lib/use_paragon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@
module UseParagon
# Configuration from initializer
class << self
def load!
register_rails_engine if rails?
end

def configuration
@configuration ||= Configuration.new
end

def configure
yield(configuration)
end

def register_rails_engine
require "use_paragon/engine"
end
end

class Error < StandardError; end
Expand Down
26 changes: 10 additions & 16 deletions lib/use_paragon/base.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# frozen_string_literal: true

require "jwt"
require "faraday"
require "use_paragon/configuration"
require "jwt"

module UseParagon
# Basic logic for interacting with UseParagon platform
# Handles authentication and requests to Paragon API base on:
# https://docs.useparagon.com/getting-started/installing-the-connect-sdk#setup-with-your-own-authentication-backend
class Base
attr_reader :token, :user_id

Expand All @@ -15,6 +16,7 @@ def initialize(user_id)
end

def generate_token
# Paragon User Token for each of your authenticated user
validate_user_id!

rsa_private = OpenSSL::PKey::RSA.new(config.private_key)
Expand All @@ -33,27 +35,19 @@ def generate_token

def connection
@connection ||= Faraday.new do |conn|
conn.request :authorization, "Bearer", generate_token
conn.response :logger, Rails.logger, { errors: true, bodies: true }

conn.url_prefix = config.base_url
conn.request :authorization, "Bearer", generate_token
conn.request :json

if config.logger_enabled
conn.response :logger, config.logger, { errors: true, bodies: true }
end

conn.response :json, content_type: "application/json"
conn.response :raise_error
end
end

def logger(conn)
conn.response :logger, Rails.logger, { errors: true, bodies: true } do |logger|
logger.filter(/(Authorization:)([^&]+)/, '\1[REMOVED]')
logger.filter(/("code":)([^&]+)/, '\1[REMOVED]')
end
end

def logger_enabled?
ENV.fetch("USEPARAGON_API_LOGGER", "false") == "true"
end

def path(endpoint)
"/projects/#{config.project_id}/#{endpoint}"
end
Expand Down
6 changes: 5 additions & 1 deletion lib/use_paragon/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# frozen_string_literal: true

require "logger"

module UseParagon
# Allows configuration using an initializer
class Configuration
attr_accessor :private_key, :project_id, :base_url
attr_accessor :private_key, :project_id, :base_url, :logger, :logger_enabled

def initialize
@base_url = "https://zeus.useparagon.com"
@logger = Logger.new(STDOUT)
@logger_enabled = true
end
end
end
10 changes: 10 additions & 0 deletions lib/use_paragon/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module UseParagon
# Engine for Rails to include and precompile vendor assets
class Engine < ::Rails::Engine
initializer "use_paragon.assets.precompile" do |app|
app.config.assets.precompile += %w[useparagon/connect.js]
end
end
end
3 changes: 0 additions & 3 deletions use_paragon.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,4 @@ Gem::Specification.new do |spec|

spec.add_dependency "faraday"
spec.add_dependency "jwt"

# For more information and examples about making a new gem, checkout our
# guide at: https://bundler.io/guides/creating_gem.html
end
2 changes: 2 additions & 0 deletions vendor/assets/javascripts/useparagon/connect.js

Large diffs are not rendered by default.

0 comments on commit a22c02a

Please sign in to comment.