module Doorkeeper module ApplicationMixin extend ActiveSupport::Concern include OAuth::Helpers include Models::Scopes include ActiveModel::MassAssignmentSecurity if defined?(::ProtectedAttributes) included do has_many :access_grants, dependent: :delete_all, class_name: 'Doorkeeper::AccessGrant' has_many :access_tokens, dependent: :delete_all, class_name: 'Doorkeeper::AccessToken' validates :name, :secret, :uid, presence: true validates :uid, uniqueness: true validates :redirect_uri, redirect_uri: true before_validation :generate_uid, :generate_secret, on: :create end module ClassMethods # Returns an instance of the Doorkeeper::Application with # specific UID and secret. # # @param uid [#to_s] UID (any object that responds to `#to_s`) # @param secret [#to_s] secret (any object that responds to `#to_s`) # # @return [Doorkeeper::Application, nil] Application instance or nil # if there is no record with such credentials # def by_uid_and_secret(uid, secret) find_by(uid: uid.to_s, secret: secret.to_s) end # Returns an instance of the Doorkeeper::Application with specific UID. # # @param uid [#to_s] UID (any object that responds to `#to_s`) # # @return [Doorkeeper::Application, nil] Application instance or nil # if there is no record with such UID # def by_uid(uid) find_by(uid: uid.to_s) end end # Set an application's valid redirect URIs. # # @param uris [String, Array] Newline-separated string or array the URI(s) # # @return [String] The redirect URI(s) seperated by newlines. def redirect_uri=(uris) super(uris.is_a?(Array) ? uris.join("\n") : uris) end private def has_scopes? Doorkeeper.configuration.orm != :active_record || Doorkeeper::Application.column_names.include?("scopes") end def generate_uid self.uid = UniqueToken.generate if uid.blank? end def generate_secret self.secret = UniqueToken.generate if secret.blank? end end end