parent
d46d7b64d9
commit
45cb1146e4
@ -1,30 +0,0 @@
|
||||
class AccessToken < Carto::OauthToken
|
||||
|
||||
before_create :set_authorized_at
|
||||
after_create :store_api_credentials
|
||||
after_destroy :clear_api_credentials
|
||||
|
||||
private
|
||||
|
||||
def metadata_key
|
||||
"rails:oauth_access_tokens:#{token}"
|
||||
end
|
||||
|
||||
def set_authorized_at
|
||||
self.authorized_at = Time.now
|
||||
end
|
||||
|
||||
def store_api_credentials
|
||||
$api_credentials.hset metadata_key, "consumer_key", client_application.key
|
||||
$api_credentials.hset metadata_key, "consumer_secret", client_application.secret
|
||||
$api_credentials.hset metadata_key, "access_token_token", token
|
||||
$api_credentials.hset metadata_key, "access_token_secret", secret
|
||||
$api_credentials.hset metadata_key, "user_id", user_id
|
||||
$api_credentials.hset metadata_key, "time", authorized_at
|
||||
end
|
||||
|
||||
def clear_api_credentials
|
||||
$api_credentials.del metadata_key
|
||||
end
|
||||
|
||||
end
|
@ -0,0 +1,32 @@
|
||||
module Carto
|
||||
class AccessToken < OauthToken
|
||||
|
||||
before_create :set_authorized_at
|
||||
after_create :store_api_credentials
|
||||
after_destroy :clear_api_credentials
|
||||
|
||||
private
|
||||
|
||||
def metadata_key
|
||||
"rails:oauth_access_tokens:#{token}"
|
||||
end
|
||||
|
||||
def set_authorized_at
|
||||
self.authorized_at = Time.now
|
||||
end
|
||||
|
||||
def store_api_credentials
|
||||
$api_credentials.hset metadata_key, "consumer_key", client_application.key
|
||||
$api_credentials.hset metadata_key, "consumer_secret", client_application.secret
|
||||
$api_credentials.hset metadata_key, "access_token_token", token
|
||||
$api_credentials.hset metadata_key, "access_token_secret", secret
|
||||
$api_credentials.hset metadata_key, "user_id", user_id
|
||||
$api_credentials.hset metadata_key, "time", authorized_at
|
||||
end
|
||||
|
||||
def clear_api_credentials
|
||||
$api_credentials.del metadata_key
|
||||
end
|
||||
|
||||
end
|
||||
end
|
@ -0,0 +1,39 @@
|
||||
module Carto
|
||||
class RequestToken < OauthToken
|
||||
|
||||
attr_accessor :provided_oauth_verifier
|
||||
|
||||
def authorize!(user)
|
||||
return false if authorized?
|
||||
|
||||
new_attributes = { user: user, authorized_at: Time.now }
|
||||
new_attributes[:verifier] = OAuth::Helper.generate_key(20)[0,20] unless oauth10?
|
||||
|
||||
update!(new_attributes)
|
||||
end
|
||||
|
||||
def exchange!
|
||||
return false unless authorized?
|
||||
return false unless oauth10? || verifier == provided_oauth_verifier
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
access_token = Carto::AccessToken.create!(user: user, client_application: client_application)
|
||||
invalidate!
|
||||
access_token
|
||||
end
|
||||
end
|
||||
|
||||
def to_query
|
||||
oauth10? ? super : "#{super}&oauth_callback_confirmed=true"
|
||||
end
|
||||
|
||||
def oob?
|
||||
callback_url == 'oob'
|
||||
end
|
||||
|
||||
def oauth10?
|
||||
(defined? OAUTH_10_SUPPORT) && OAUTH_10_SUPPORT && callback_url.blank?
|
||||
end
|
||||
|
||||
end
|
||||
end
|
@ -1,37 +0,0 @@
|
||||
class RequestToken < Carto::OauthToken
|
||||
|
||||
attr_accessor :provided_oauth_verifier
|
||||
|
||||
def authorize!(user)
|
||||
return false if authorized?
|
||||
|
||||
new_attributes = { user: user, authorized_at: Time.now }
|
||||
new_attributes[:verifier] = OAuth::Helper.generate_key(20)[0,20] unless oauth10?
|
||||
|
||||
update!(new_attributes)
|
||||
end
|
||||
|
||||
def exchange!
|
||||
return false unless authorized?
|
||||
return false unless oauth10? || verifier == provided_oauth_verifier
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
access_token = AccessToken.create!(user: user, client_application: client_application)
|
||||
invalidate!
|
||||
access_token
|
||||
end
|
||||
end
|
||||
|
||||
def to_query
|
||||
oauth10? ? super : "#{super}&oauth_callback_confirmed=true"
|
||||
end
|
||||
|
||||
def oob?
|
||||
callback_url == 'oob'
|
||||
end
|
||||
|
||||
def oauth10?
|
||||
(defined? OAUTH_10_SUPPORT) && OAUTH_10_SUPPORT && callback_url.blank?
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in new issue