2011-02-01 09:57:29 +08:00
|
|
|
#
|
|
|
|
# Copyright (C) 2011 Instructure, Inc.
|
|
|
|
#
|
|
|
|
# This file is part of Canvas.
|
|
|
|
#
|
|
|
|
# Canvas is free software: you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
# Software Foundation, version 3 of the License.
|
|
|
|
#
|
|
|
|
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License along
|
|
|
|
# with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
class DeveloperKey < ActiveRecord::Base
|
2012-08-17 05:25:50 +08:00
|
|
|
include CustomValidations
|
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :account
|
|
|
|
has_many :page_views
|
2011-05-27 07:41:43 +08:00
|
|
|
has_many :access_tokens
|
2012-05-06 02:18:08 +08:00
|
|
|
has_many :context_external_tools, :primary_key => 'tool_id', :foreign_key => 'tool_id'
|
2011-05-27 07:41:43 +08:00
|
|
|
|
2012-05-17 13:52:32 +08:00
|
|
|
attr_accessible :api_key, :name, :user, :account, :icon_url, :redirect_uri, :tool_id, :email
|
2012-08-17 05:25:50 +08:00
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
before_create :generate_api_key
|
2012-05-17 13:52:32 +08:00
|
|
|
before_save :nullify_empty_tool_id
|
2012-08-17 05:25:50 +08:00
|
|
|
|
|
|
|
validates_as_url :redirect_uri
|
|
|
|
|
2012-05-17 13:52:32 +08:00
|
|
|
def nullify_empty_tool_id
|
|
|
|
self.tool_id = nil if tool_id.blank?
|
|
|
|
self.icon_url = nil if icon_url.blank?
|
|
|
|
end
|
2011-02-01 09:57:29 +08:00
|
|
|
|
|
|
|
def generate_api_key(overwrite=false)
|
2011-05-27 07:41:43 +08:00
|
|
|
self.api_key = AutoHandle.generate(nil, 64) if overwrite || !self.api_key
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.default
|
|
|
|
get_special_key("User-Generated")
|
|
|
|
end
|
|
|
|
|
2012-05-17 13:52:32 +08:00
|
|
|
def account_name
|
|
|
|
account.try(:name)
|
|
|
|
end
|
|
|
|
|
2011-05-27 07:41:43 +08:00
|
|
|
def self.get_special_key(default_key_name)
|
2012-03-20 06:24:27 +08:00
|
|
|
Shard.default.activate do
|
|
|
|
@special_keys ||= {}
|
2011-05-27 07:41:43 +08:00
|
|
|
|
2012-03-20 06:24:27 +08:00
|
|
|
if Rails.env.test?
|
|
|
|
# TODO: we have to do this because tests run in transactions. maybe it'd
|
|
|
|
# be good to create some sort of of memoize_if_safe method, that only
|
|
|
|
# memoizes when we're caching classes and not in test mode? I dunno. But
|
|
|
|
# this stinks.
|
|
|
|
return @special_keys[default_key_name] = DeveloperKey.find_or_create_by_name(default_key_name)
|
|
|
|
end
|
2011-05-27 07:41:43 +08:00
|
|
|
|
2012-03-20 06:24:27 +08:00
|
|
|
key = @special_keys[default_key_name]
|
|
|
|
return key if key
|
|
|
|
if (key_id = Setting.get("#{default_key_name}_developer_key_id", nil)) && key_id.present?
|
|
|
|
key = DeveloperKey.find_by_id(key_id)
|
|
|
|
end
|
|
|
|
return @special_keys[default_key_name] = key if key
|
|
|
|
key = DeveloperKey.create!(:name => default_key_name)
|
|
|
|
Setting.set("#{default_key_name}_developer_key_id", key.id)
|
|
|
|
return @special_keys[default_key_name] = key
|
2011-05-27 07:41:43 +08:00
|
|
|
end
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
2011-08-23 03:35:03 +08:00
|
|
|
|
|
|
|
# verify that the given uri has the same domain as this key's
|
|
|
|
# redirect_uri domain.
|
|
|
|
def redirect_domain_matches?(redirect_uri)
|
|
|
|
self_domain = URI.parse(self.redirect_uri).host
|
|
|
|
other_domain = URI.parse(redirect_uri).host
|
2012-08-17 05:25:50 +08:00
|
|
|
return self_domain.present? && (self_domain == other_domain || other_domain.end_with?(".#{self_domain}"))
|
2011-08-23 03:35:03 +08:00
|
|
|
rescue URI::InvalidURIError
|
|
|
|
return false
|
|
|
|
end
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|