-
Notifications
You must be signed in to change notification settings - Fork 0
feat: implement device registration, admin-secured management, and rate limiting for push notifications #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,5 @@ POSTGRES_PORT= | |
|
|
||
| USER_DB= | ||
| PASSWORD_DB= | ||
|
|
||
| ADMIN_TOKEN= | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,17 @@ | ||
| class ApplicationController < ActionController::API | ||
| private | ||
|
|
||
| def authenticate_admin! | ||
| configured_token = ENV['ADMIN_TOKEN'].presence | ||
| supplied_token = request.headers['X-Admin-Token'].presence | ||
|
|
||
| return render_unauthorized unless configured_token && supplied_token | ||
| return render_unauthorized unless supplied_token.bytesize == configured_token.bytesize | ||
|
|
||
| render_unauthorized unless ActiveSupport::SecurityUtils.secure_compare(supplied_token, configured_token) | ||
| end | ||
|
|
||
| def render_unauthorized | ||
| render json: { error: 'Unauthorized' }, status: :unauthorized | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| class DevicesController < ApplicationController | ||
| before_action :authenticate_admin!, only: %i[index destroy] | ||
|
|
||
| def index | ||
| no_store | ||
| render json: { data: Device.order(:id).pluck(:fcm_token) } | ||
| end | ||
|
|
||
| def create | ||
| token = device_params[:fcm_token] | ||
| device = Device.find_or_initialize_by(fcm_token: token) | ||
| new_device = device.new_record? | ||
| device.assign_attributes(device_params) | ||
|
|
||
| if device.save | ||
| render_device_json(device, status: new_device ? :created : :ok) | ||
| else | ||
| render json: { errors: device.errors.full_messages }, status: :unprocessable_entity | ||
| end | ||
|
Euler-B marked this conversation as resolved.
|
||
| rescue ActiveRecord::RecordNotUnique | ||
| existing_device = Device.find_by!(fcm_token: token) | ||
| render_device_json(existing_device, status: :ok) | ||
| end | ||
|
|
||
| def destroy | ||
| device = Device.find_by(id: params[:id]) | ||
| return render json: { error: 'Device not found' }, status: :not_found unless device | ||
|
|
||
| device.destroy! | ||
| head :no_content | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def render_device_json(device, status:) | ||
| render json: { data: { id: device.id, type: 'device' } }, status: status | ||
| end | ||
|
|
||
| def device_params | ||
| params.permit(:fcm_token, :platform) | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| class Device < ApplicationRecord | ||
| MAX_FCM_TOKEN_LENGTH = 255 | ||
|
|
||
| validates :fcm_token, | ||
| presence: true, | ||
| uniqueness: true, | ||
| length: { maximum: MAX_FCM_TOKEN_LENGTH }, | ||
| format: { without: /\s/ } | ||
| validates :platform, inclusion: { in: %w[web] } | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,6 @@ | |
|
|
||
| resource '/v1/*', | ||
| headers: :any, | ||
| methods: %i[get post options] | ||
| methods: %i[get post delete options] | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| class CreateDevices < ActiveRecord::Migration[7.2] | ||
| def change | ||
| create_table :devices do |t| | ||
| t.string :fcm_token, null: false, limit: 255 | ||
| t.string :platform, null: false, default: 'web' | ||
| t.timestamps | ||
| end | ||
|
|
||
| add_index :devices, :fcm_token, unique: true | ||
|
Euler-B marked this conversation as resolved.
|
||
| add_check_constraint :devices, "platform = 'web'", name: 'devices_platform_is_web' | ||
| end | ||
| end | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| require 'test_helper' | ||
|
|
||
| class DevicesControllerTest < ActionDispatch::IntegrationTest | ||
| setup do | ||
| @admin_token = 'test-admin-token' | ||
| ENV['ADMIN_TOKEN'] = @admin_token | ||
| end | ||
|
|
||
| teardown do | ||
| ENV.delete('ADMIN_TOKEN') | ||
| end | ||
|
|
||
| test 'creates a device publicly and does not duplicate its token' do | ||
| assert_difference('Device.count', 1) do | ||
| post devices_url, params: { fcm_token: 'token-1' }, as: :json | ||
| end | ||
| assert_response :created | ||
|
|
||
| post devices_url, params: { fcm_token: 'token-1' }, as: :json | ||
| assert_response :success | ||
| assert_equal 1, Device.where(fcm_token: 'token-1').count | ||
| end | ||
|
|
||
| test 'rejects invalid devices' do | ||
| post devices_url, params: { fcm_token: '' }, as: :json | ||
|
|
||
| assert_response :unprocessable_entity | ||
| end | ||
|
|
||
| test 'requires the admin token to list devices' do | ||
| get devices_url | ||
| assert_response :unauthorized | ||
| assert_equal({ 'error' => 'Unauthorized' }, JSON.parse(response.body)) | ||
|
|
||
| get devices_url, headers: { 'X-Admin-Token' => @admin_token } | ||
| assert_response :success | ||
| assert_includes response.headers['Cache-Control'], 'no-store' | ||
| assert_equal({ 'data' => [] }, JSON.parse(response.body)) | ||
| end | ||
|
|
||
| test 'requires the admin token to destroy a device' do | ||
| device = Device.create!(fcm_token: 'token-1') | ||
|
|
||
| delete device_url(device) | ||
| assert_response :unauthorized | ||
| assert_equal({ 'error' => 'Unauthorized' }, JSON.parse(response.body)) | ||
|
|
||
| delete device_url(device), headers: { 'X-Admin-Token' => @admin_token } | ||
| assert_response :no_content | ||
| assert_not Device.exists?(device.id) | ||
| end | ||
|
|
||
| test 'rejects unauthorized requests with mismatched token length or invalid token' do | ||
| get devices_url, headers: { 'X-Admin-Token' => 'short' } | ||
| assert_response :unauthorized | ||
| assert_equal({ 'error' => 'Unauthorized' }, JSON.parse(response.body)) | ||
|
|
||
| get devices_url, headers: { 'X-Admin-Token' => 'wrong-admin-token' } | ||
| assert_response :unauthorized | ||
| assert_equal({ 'error' => 'Unauthorized' }, JSON.parse(response.body)) | ||
| end | ||
|
|
||
| test 'fails closed when ADMIN_TOKEN is not configured' do | ||
| ENV.delete('ADMIN_TOKEN') | ||
|
|
||
| get devices_url, headers: { 'X-Admin-Token' => 'test-admin-token' } | ||
|
|
||
| assert_response :unauthorized | ||
| assert_equal({ 'error' => 'Unauthorized' }, JSON.parse(response.body)) | ||
| end | ||
|
|
||
| test 'rescues ActiveRecord::RecordNotUnique on concurrent device creation' do | ||
| existing = Device.create!(fcm_token: 'token-concurrent') | ||
|
|
||
| begin | ||
| Device.define_method(:save) { raise ActiveRecord::RecordNotUnique } | ||
| post devices_url, params: { fcm_token: 'token-concurrent' }, as: :json | ||
| ensure | ||
| Device.remove_method(:save) | ||
| end | ||
|
|
||
| assert_response :ok | ||
| assert_equal({ 'data' => { 'id' => existing.id, 'type' => 'device' } }, JSON.parse(response.body)) | ||
| end | ||
|
|
||
| test 'supports CORS preflight for DELETE requests' do | ||
| device = Device.create!(fcm_token: 'token-cors') | ||
| origin = ENV.fetch('ALLOWED_ORIGIN', 'http://localhost:5173') | ||
|
|
||
| process :options, device_url(device), headers: { | ||
| 'Origin' => origin, | ||
| 'Access-Control-Request-Method' => 'DELETE' | ||
| } | ||
|
|
||
| assert_response :success | ||
| assert_includes response.headers['Access-Control-Allow-Methods'], 'DELETE' | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| require 'test_helper' | ||
|
|
||
| class DeviceTest < ActiveSupport::TestCase | ||
| test 'requires a unique FCM token' do | ||
| device = Device.new(fcm_token: 'token-1') | ||
| assert device.save! | ||
|
|
||
| duplicate = Device.new(fcm_token: 'token-1') | ||
| assert_not duplicate.valid? | ||
| assert_includes duplicate.errors[:fcm_token], 'has already been taken' | ||
| end | ||
|
|
||
| test 'defaults to the web platform' do | ||
| assert_equal 'web', Device.new(fcm_token: 'token-1').platform | ||
| end | ||
|
|
||
| test 'only accepts the web platform' do | ||
| device = Device.new(fcm_token: 'token-1', platform: 'ios') | ||
|
|
||
| assert_not device.valid? | ||
| assert_includes device.errors[:platform], 'is not included in the list' | ||
| end | ||
|
Euler-B marked this conversation as resolved.
|
||
|
|
||
| test 'rejects invalid FCM tokens containing whitespace, control characters, or exceeding maximum length' do | ||
| invalid_tokens = [ | ||
| 'token with space', | ||
| "token\nwith\nnewline", | ||
| "token\rwith\rreturn", | ||
| "token\twith\ttab", | ||
| 'a' * (Device::MAX_FCM_TOKEN_LENGTH + 1) | ||
| ] | ||
|
|
||
| invalid_tokens.each do |invalid_token| | ||
| device = Device.new(fcm_token: invalid_token) | ||
| assert_not device.valid?, "Expected device with token #{invalid_token.inspect} to be invalid" | ||
| assert_not_empty device.errors[:fcm_token] | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.