-
Notifications
You must be signed in to change notification settings - Fork 653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate and delete ClientApplication Sequel model #15886
Changes from all commits
954f557
befb631
dc286a3
56ed134
bbd210c
3694622
1b78dd2
d5b26e3
b82b758
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,49 @@ | ||
require 'active_record' | ||
require 'oauth' | ||
require_dependency 'cartodb_config_utils' | ||
require_dependency 'carto/domain_patcher_request_proxy' | ||
|
||
module Carto | ||
class ClientApplication < ActiveRecord::Base | ||
|
||
extend CartoDB::ConfigUtils | ||
|
||
belongs_to :user, class_name: Carto::User | ||
has_many :oauth_tokens, class_name: Carto::OauthToken, dependent: :destroy | ||
has_many :access_tokens, -> { where(type: 'AccessToken') }, class_name: Carto::OauthToken, dependent: :destroy | ||
|
||
before_create :initialize_entity | ||
|
||
def oauth_server | ||
# check if this is used | ||
@oauth_server ||= OAuth::Server.new('http://your.site') | ||
end | ||
|
||
def self.find_token(token_key) | ||
return nil if token_key.nil? | ||
|
||
token = Carto::RequestToken.find_by(token: token_key) || Carto::AccessToken.find_by(token: token_key) | ||
token&.authorized? ? token : nil | ||
end | ||
|
||
def self.verify_request(request, options = {}, &block) | ||
value = OAuth::Signature.build(request, options, &block).verify | ||
if !value && !cartodb_com_hosted? | ||
# Validation failed, try to see if it has been signed for cartodb.com | ||
cartodb_request = Carto::DomainPatcherRequestProxy.new(request, options) | ||
value = OAuth::Signature.build(cartodb_request, options, &block).verify | ||
end | ||
value | ||
rescue OAuth::Signature::UnknownSignatureMethod | ||
false | ||
end | ||
|
||
private | ||
|
||
def initialize_entity | ||
self.key = OAuth::Helper.generate_key(40)[0, 40] | ||
self.secret = OAuth::Helper.generate_key(40)[0, 40] | ||
end | ||
|
||
end | ||
end |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,15 +87,22 @@ def build_search_tweets_from_hash_export(exported_hash) | |
end | ||
|
||
def save_imported_user(user) | ||
# Keep client_application imported timestamps | ||
if user.client_application | ||
import_app_updated_at = user.client_application.updated_at | ||
import_app_created_at = user.client_application.created_at | ||
end | ||
|
||
user.save! | ||
::User[user.id].after_save | ||
|
||
client_application = user.client_applications.first | ||
if client_application | ||
client_application.access_tokens.each do |t| | ||
t.update!(type: 'AccessToken') | ||
end | ||
return unless user.client_application | ||
|
||
user.client_application.access_tokens.each do |t| | ||
t.update!(type: 'AccessToken') | ||
end | ||
|
||
user.client_application.update_columns(created_at: import_app_created_at, updated_at: import_app_updated_at) | ||
end | ||
|
||
def save_imported_search_tweet(search_tweet, user) | ||
|
@@ -146,7 +153,7 @@ def build_user_from_hash(exported_user) | |
|
||
user.connector_configurations = build_connector_configurations_from_hash(exported_user[:connector_configurations]) | ||
|
||
user.client_applications = build_client_applications_from_hash(exported_user[:client_application]) | ||
user.client_application = build_client_application_from_hash(exported_user[:client_application]) | ||
|
||
user.oauth_app_users = build_oauth_app_users_from_hash(exported_user[:oauth_app_users]) | ||
|
||
|
@@ -222,23 +229,24 @@ def build_oauth_token_fom_hash(exported_oauth_token) | |
) | ||
end | ||
|
||
def build_client_applications_from_hash(client_app_hash) | ||
return [] unless client_app_hash | ||
def build_client_application_from_hash(client_app_hash) | ||
return unless client_app_hash | ||
|
||
client_application = Carto::ClientApplication.new( | ||
client_application = Carto::ClientApplication.create( | ||
name: client_app_hash[:name], | ||
url: client_app_hash[:url], | ||
support_url: client_app_hash[:support_url], | ||
callback_url: client_app_hash[:callback_url], | ||
key: client_app_hash[:key], | ||
secret: client_app_hash[:secret], | ||
created_at: client_app_hash[:created_at], | ||
updated_at: client_app_hash[:updated_at], | ||
oauth_tokens: client_app_hash[:oauth_tokens].map { |t| build_oauth_token_fom_hash(t) }, | ||
access_tokens: client_app_hash[:access_tokens].map { |t| build_oauth_token_fom_hash(t) } | ||
access_tokens: client_app_hash[:access_tokens].map { |t| build_oauth_token_fom_hash(t) }, | ||
user_id: client_app_hash[:user_id] | ||
) | ||
|
||
[client_application] | ||
# Overwrite fields that were created with ORM lifecycle callbacks | ||
client_application.key = client_app_hash[:key] | ||
client_application.secret = client_app_hash[:secret] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another way of achieving this could be to modify the model callbacks so those attributes are only assigned if not present yet, for example: def initialize_entity
self.key = OAuth::Helper.generate_key(40)[0, 40] unless key.present?
self.secret = OAuth::Helper.generate_key(40)[0, 40] unless secret.present?
end I don't have a strong opinion on this so choose what you think it's more intuitive to understand. Maybe |
||
client_application.created_at = client_app_hash[:created_at] | ||
client_application.updated_at = client_app_hash[:updated_at] | ||
client_application | ||
end | ||
|
||
def build_oauth_app_users_from_hash(oauth_app_users) | ||
|
@@ -371,7 +379,8 @@ def export_client_application(app) | |
created_at: app.created_at, | ||
updated_at: app.updated_at, | ||
oauth_tokens: app.oauth_tokens.reject { |t| a_t_tokens.include?(t.token) }.map { |ot| export_oauth_token(ot) }, | ||
access_tokens: app.access_tokens.map { |ot| export_oauth_token(ot) } | ||
access_tokens: app.access_tokens.map { |ot| export_oauth_token(ot) }, | ||
user_id: app.user_id | ||
} | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'oauth' | ||
|
||
module Carto | ||
class DomainPatcherRequestProxy < OAuth::RequestProxy::RackRequest | ||
|
||
def uri | ||
super.sub('carto.com', 'cartodb.com') | ||
end | ||
|
||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing this 🎉