Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/fasp_base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ruby-3.3.6
ruby-version: ruby-4.0.9
bundler-cache: true
working-directory: ./fasp_base

Expand All @@ -46,7 +46,7 @@ jobs:
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ruby-3.3.6
ruby-version: ruby-4.0.9
bundler-cache: true
working-directory: ./fasp_base

Expand Down
4 changes: 2 additions & 2 deletions fasp_base/.github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ruby-3.3.6
ruby-version: ruby-4.0.9
bundler-cache: true

- name: Lint code for consistent style
Expand All @@ -40,7 +40,7 @@ jobs:
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ruby-3.3.6
ruby-version: ruby-4.0.9
bundler-cache: true

- name: Run tests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module FaspBase
class Fasp::RegistrationsController < ApplicationController
skip_authentication

before_action :registration_allowed

def create
@registration = AutomaticRegistration.new(registration_params)

if @registration.valid?
@registration.save!

render json: fasp_info, status: :created
else
render json: ProblemDetails.new(@registration),
status: :unprocessable_content,
content_type: "application/problem+json"
end

rescue ActiveRecord::RecordNotFound
render json: { manualRegistration: new_registration_url }, status: :forbidden
end

private

def fasp_info
{
serverId: @registration.server.id.to_s,
publicKey: @registration.server.fasp_public_key_base64
}
end

def registration_allowed
head :not_found if FaspBase::Setting.get("registration").closed?
end

def registration_params
params.permit(:baseUrl, :faspId, :publicKey)
end
end
end
27 changes: 27 additions & 0 deletions fasp_base/app/controllers/fasp_base/host_meta_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module FaspBase
class HostMetaController < ApplicationController
skip_authentication

def show
respond_to do |format|
format.json { render json: host_meta }
end
end

private

def host_meta
links = [ {
"href" => FaspBase.base_url,
"rel" => "https://fasp.dev/ns#baseUrl"
} ]

links << {
"href" => new_registration_url,
"rel" => "https://fasp.dev/ns#manualRegistration"
} if FaspBase::Setting.get("registration").in?(%w[manual invite_only])

{ "links" => links }
end
end
end
21 changes: 17 additions & 4 deletions fasp_base/app/controllers/fasp_base/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ class RegistrationsController < ApplicationController
before_action :registration_allowed

def new
@registration = Registration.new
session[:return_to_server] = params[:return_to_server] if params[:return_to_server].present?

@registration = ManualRegistration.new
end

def create
@registration = Registration.new(registration_params)
@registration = ManualRegistration.new(registration_params)

if @registration.valid?
@registration.save!
self.current_user = @registration.user

respond_to do |format|
format.html { redirect_to server_path(@registration.server) }
format.html { redirect_to_proper_target }
format.json { head :created }
end
else
Expand All @@ -29,13 +31,24 @@ def create

private

def redirect_to_proper_target
if session[:return_to_server].present?
query = { return_id: session.delete(:return_to_server) }

redirect_to @registration.server.url("/registration_completion?#{query.to_query}"), allow_other_host: true
else
redirect_to server_path(@registration.server)
end
end

def registration_params
params.require(:registration)
.permit(:email, :base_url, :password, :password_confirmation, :invitation_code)
end

def registration_allowed
head :not_found if FaspBase::Setting.get("registration").closed?
registration = FaspBase::Setting.get("registration")
head :not_found if registration.closed? || registration.automatic?
end
end
end
61 changes: 61 additions & 0 deletions fasp_base/app/models/fasp_base/automatic_registration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module FaspBase
class AutomaticRegistration
include ActiveModel::Model
include ActiveModel::Attributes

attr_reader :server

attribute :base_url, :string
attribute :fasp_id, :string
attribute :public_key, :string

validates :base_url,
presence: true,
"fasp_base/uri": { if: -> { Rails.env.production? } }
validates :fasp_id,
presence: true
validates :public_key,
presence: true

validate :public_key_parseable

def initialize(attributes = {})
super(
base_url: attributes[:baseUrl],
fasp_id: attributes[:faspId],
public_key: attributes[:publicKey]
)
end

def save!
validate!

@server =
if FaspBase::Setting.get("registration").manual?
Server.registration_unfinished.find_by!(base_url:)
else
Server.new(base_url:)
end

@server.public_key_pem = parsed_public_key
@server.fasp_remote_id = fasp_id

@server.save!
end

private

def public_key_parseable
parsed_public_key
rescue
errors.add(:public_key, :invalid)
end

def parsed_public_key
OpenSSL::PKey.new_raw_public_key(
"ed25519",
Base64.strict_decode64(public_key)
).public_to_pem
end
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module FaspBase
class Registration
class ManualRegistration
include ActiveModel::Model
include ActiveModel::Attributes

Expand Down Expand Up @@ -34,47 +34,7 @@ def save!
User.transaction do
@user = User.create!(email:, password:)
@server = Server.create!(user:, base_url:)
finish_registration
end
end

private

def finish_registration
response = make_registration_request

response.raise_for_status

handle_response(response)
end

def make_registration_request
HTTPX
.post(
@server.url("/registration"),
json: {
name: FaspBase.fasp_name,
baseUrl: FaspBase.base_url,
serverId: @server.id.to_s,
publicKey: @server.fasp_public_key_base64
}
)
end

def handle_response(response)
json = response.json
@server.update!(
fasp_remote_id: json["faspId"],
public_key_pem: base64_to_pem(json["publicKey"]),
registration_completion_uri: json["registrationCompletionUri"]
)
end

def base64_to_pem(key)
OpenSSL::PKey.new_raw_public_key(
"ed25519",
Base64.strict_decode64(key)
).public_to_pem
end
end
end
7 changes: 6 additions & 1 deletion fasp_base/app/models/fasp_base/server.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
module FaspBase
class Server < ApplicationRecord
belongs_to :user
belongs_to :user, optional: true

validates :base_url, presence: true,
uniqueness: true

before_create :create_keypair

scope :registration_unfinished, -> { where(fasp_remote_id: nil) }

def public_key
@public_key ||= OpenSSL::PKey.read(public_key_pem)
end
Expand Down
2 changes: 1 addition & 1 deletion fasp_base/app/models/fasp_base/setting.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module FaspBase
class Setting < ApplicationRecord
OPTIONS = {
registration: %w[open closed invite_only].freeze
registration: %w[automatic closed invite_only manual].freeze
}.freeze

def self.get(name)
Expand Down
5 changes: 5 additions & 0 deletions fasp_base/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
resource :activation, path: "/:version/activation", only: [ :create, :destroy ]
end
resource :provider_info, only: :show
resource :registration, only: :create
end

resource :home, only: [ :show ]
Expand All @@ -26,3 +27,7 @@

resources :servers, only: [ :show ]
end

Rails.application.routes.draw do
get "/.well-known/host-meta.json", to: "fasp_base/host_meta#show"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class RenameOpenRegistrationToManual < ActiveRecord::Migration[8.1]
def up
execute "UPDATE fasp_base_settings SET value = 'manual' WHERE name = 'registration' AND value = 'open'"
end

def down
execute "UPDATE fasp_base_settings SET value = 'open' WHERE name = 'registration' AND value = 'manual'"
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MakeUserOptionalOnServers < ActiveRecord::Migration[8.1]
def change
change_column :fasp_base_servers, :user_id, :integer, null: true
end
end
1 change: 1 addition & 0 deletions fasp_base/lib/fasp_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "fasp_base/linzer/adapter/action_dispatch/response"
require "fasp_base/linzer/adapter/httpx/request"
require "fasp_base/linzer/adapter/httpx/response"
require "fasp_base/problem_details"

module FaspBase
class Error < ::StandardError ; end
Expand Down
23 changes: 23 additions & 0 deletions fasp_base/lib/fasp_base/problem_details.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module FaspBase
class ProblemDetails
def initialize(model) = @model = model

def to_json(options = nil)
{
type: "https://fasp.dev/ns#validation-error",
errors:
}
end

private

def errors
@model.errors.map do |error|
{
detail: error.full_message,
pointer: "/#{error.attribute.to_s.camelize(:lower)}"
}
end
end
end
end
2 changes: 1 addition & 1 deletion fasp_base/test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Rails.application.routes.draw do
mount FaspBase::Engine => "/fasp_base"
mount FaspBase::Engine => "/"

root to: proc { [ 200, { "Content-Type" => "text/plain" }, [ "Root path to dummy host app" ] ] }
end
4 changes: 2 additions & 2 deletions fasp_base/test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[8.0].define(version: 2025_05_08_125745) do
ActiveRecord::Schema[8.1].define(version: 2026_10_16_135108) do
create_table "fasp_base_admin_users", force: :cascade do |t|
t.string "email", null: false
t.string "password_digest"
Expand All @@ -28,7 +28,7 @@

create_table "fasp_base_servers", force: :cascade do |t|
t.string "base_url", null: false
t.integer "user_id", null: false
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "fasp_private_key_pem", null: false
Expand Down
2 changes: 1 addition & 1 deletion fasp_base/test/fixtures/fasp_base/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

registration:
name: registration
value: open
value: manual
Loading
Loading