Add RemoteStorageAuthorization model
This commit is contained in:
@@ -77,14 +77,13 @@ class Rs::OauthController < ApplicationController
|
||||
|
||||
client_id.gsub!(/http(s)?:\/\//, "")
|
||||
|
||||
rs = RemoteStorage.new
|
||||
auth = rs.create_authorization(current_user, {
|
||||
auth = current_user.remote_storage_authorizations.create!(
|
||||
permissions: permissions,
|
||||
client_id: client_id,
|
||||
redirect_uri: redirect_uri,
|
||||
app_name: client_id, #TODO use user-defined name
|
||||
expire_at: expire_at
|
||||
})
|
||||
)
|
||||
|
||||
redirect_to url_with_state("#{redirect_uri}#access_token=#{auth.token}", state), allow_other_host: true
|
||||
end
|
||||
|
||||
32
app/models/remote_storage_authorization.rb
Normal file
32
app/models/remote_storage_authorization.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
class RemoteStorageAuthorization < ApplicationRecord
|
||||
belongs_to :user
|
||||
|
||||
serialize :permissions
|
||||
|
||||
validates_presence_of :permissions
|
||||
validates_presence_of :client_id
|
||||
|
||||
scope :valid, -> { where(expire_at: nil).or(where(expire_at: (DateTime.now)..)) }
|
||||
scope :expired, -> { where(expire_at: ..(DateTime.now)) }
|
||||
|
||||
after_initialize do |a|
|
||||
a.permisisons = [] if a.permissions == nil
|
||||
end
|
||||
|
||||
before_create :generate_token
|
||||
|
||||
def url
|
||||
if self.redirect_uri
|
||||
uri = URI.parse self.redirect_uri
|
||||
"#{uri.scheme}://#{client_id}"
|
||||
else
|
||||
"http://#{client_id}"
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def generate_token(length=16)
|
||||
self.token = SecureRandom.hex(length) if self.token.blank?
|
||||
end
|
||||
end
|
||||
@@ -14,6 +14,8 @@ class User < ApplicationRecord
|
||||
|
||||
has_many :accounts, through: :lndhub_user
|
||||
|
||||
has_many :remote_storage_authorizations
|
||||
|
||||
validates_uniqueness_of :cn
|
||||
validates_length_of :cn, :minimum => 3
|
||||
validates_format_of :cn, with: /\A([a-z0-9\-])*\z/,
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
require 'ostruct'
|
||||
|
||||
class RemoteStorage
|
||||
|
||||
def initialize
|
||||
end
|
||||
|
||||
def create_authorization(user, auth_data)
|
||||
|
||||
return OpenStruct.new(token: "SOME-FANCY-TOKEN")
|
||||
# permissions: permissions,
|
||||
# client_id: client_id,
|
||||
# redirect_uri: redirect_uri,
|
||||
# app_name: client_id, #TODO use user-defined name
|
||||
# expire_at: expire_at
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user