- has_many :tokens, :class_name => "OauthToken"
- validates_presence_of :name, :url, :key, :secret
- validates_uniqueness_of :key
- before_validation_on_create :generate_keys
-
+ has_many :tokens, :class_name => "OauthToken", :dependent => :delete_all
+ has_many :access_tokens
+ has_many :oauth2_verifiers
+ has_many :oauth_tokens
+
+ validates :key, :presence => true, :uniqueness => true
+ validates :name, :url, :secret, :presence => true
+ validates :url, :format => /\A#{URI::DEFAULT_PARSER.make_regexp(%w[http https])}\z/
+ validates :support_url, :allow_blank => true, :format => /\A#{URI::DEFAULT_PARSER.make_regexp(%w[http https])}\z/
+ validates :callback_url, :allow_blank => true, :format => /\A#{URI::DEFAULT_PARSER.make_regexp}\z/
+
+ before_validation :generate_keys, :on => :create
+
+ attr_accessor :token_callback_url
+
+ def self.find_token(token_key)
+ token = OauthToken.includes(:client_application).find_by(:token => token_key)
+ token if token&.authorized?
+ end
+