Skip to content

Commit

Permalink
Token Service
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavrajs committed Dec 15, 2019
1 parent 91cb92d commit 512f569
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 42 deletions.
10 changes: 2 additions & 8 deletions app/controllers/api/v1/widget/base_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Api::V1::Widget::BaseController < ActionController::Base
class Api::V1::Widget::BaseController < ApplicationController
private

def conversation
Expand All @@ -9,19 +9,13 @@ def conversation
end

def auth_token_params
@auth_token_params ||= JWT.decode(
request.headers[header_name], secret_key, true, algorithm: 'HS256'
).first.symbolize_keys
@auth_token_params ||= ::Widget::TokenService.new(token: request.headers[header_name]).decode_token
end

def header_name
'X-Auth-Token'
end

def secret_key
Rails.application.secrets.secret_key_base
end

def set_web_widget
@web_widget = ::Channel::WebWidget.find_by!(website_token: permitted_params[:website_token])
end
Expand Down
44 changes: 10 additions & 34 deletions app/controllers/widgets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ class WidgetsController < ActionController::Base
private

def set_contact
return if cookie_params[:source_id].nil?
return if @auth_token_params[:contact_id].nil?

contact_inbox = ::ContactInbox.find_by(
inbox_id: @web_widget.inbox.id,
source_id: cookie_params[:source_id]
)

@contact = contact_inbox ? contact_inbox.contact : nil
@contact = @web_widget.inbox.contacts.find(@auth_token_params[:contact_id])
end

def set_token
@token = conversation_token
@token = permitted_params[:cw_conversation]
@auth_token_params = if @token.present?
::Widget::TokenService.new(token: @token).decode_token
else
{}
end
end

def set_web_widget
Expand All @@ -31,32 +31,8 @@ def build_contact
contact_inbox = @web_widget.create_contact_inbox
@contact = contact_inbox.contact

payload = {
source_id: contact_inbox.source_id,
contact_id: @contact.id,
inbox_id: @web_widget.inbox.id
}
@token = JWT.encode payload, secret_key, 'HS256'
end

def cookie_params
return @cookie_params if @cookie_params.present?

if conversation_token.present?
begin
@cookie_params = JWT.decode(
conversation_token, secret_key, true, algorithm: 'HS256'
).first.symbolize_keys
rescue StandardError
@cookie_params = {}
end
return @cookie_params
end
{}
end

def conversation_token
permitted_params[:cw_conversation]
payload = { contact_id: @contact.id, inbox_id: @web_widget.inbox.id }
@token = ::Widget::TokenService.new(payload: payload).generate_token
end

def permitted_params
Expand Down
Empty file.
21 changes: 21 additions & 0 deletions app/services/widget/token_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Widget::TokenService
pattr_initialize [:payload, :token]

def generate_token
JWT.encode payload, secret_key, 'HS256'
end

def decode_token
JWT.decode(
token, secret_key, true, algorithm: 'HS256'
).first.symbolize_keys
rescue StandardError
{}
end

private

def secret_key
Rails.application.secrets.secret_key_base
end
end

0 comments on commit 512f569

Please sign in to comment.