Skip to content

Commit 2b94d6e

Browse files
committed
hack on auth, add service accts
1 parent 02ba3f6 commit 2b94d6e

File tree

11 files changed

+797
-21
lines changed

11 files changed

+797
-21
lines changed

app/controllers/api/v1/ignore_lists_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module Api
22
module V1
3-
class IgnoreListsController < ApplicationController
3+
class IgnoreListsController < BaseController
44
before_action :authenticate_user!
5-
before_action :require_admin!, except: [:index]
5+
before_action :require_admin!
66
before_action :set_ignore_list, only: [:show, :update, :destroy]
77

88
# GET /api/v1/ignore_lists
Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
11
module Api
22
module V1
3-
class SessionsController < Devise::SessionsController
4-
skip_before_action :verify_authenticity_token
5-
respond_to :json
3+
class SessionsController < BaseController
4+
skip_before_action :authenticate_user!, only: [:create, :destroy]
65

7-
private
8-
9-
def respond_with(resource, _opts = {})
10-
render json: {
11-
status: { code: 200, message: 'Logged in successfully.' },
12-
data: UserSerializer.new(resource).serializable_hash[:data][:attributes]
13-
}, status: :ok
14-
end
15-
16-
def respond_to_on_destroy
17-
if current_user
6+
def create
7+
user = User.find_by(email: params.dig(:user, :email))
8+
9+
if user&.valid_password?(params.dig(:user, :password))
10+
# Generate JWT token using our custom payload method
11+
token_payload = user.jwt_payload
12+
token = JWT.encode(token_payload, Rails.application.secret_key_base, 'HS256')
13+
14+
serializer = UserSerializer.new(user)
15+
user_data = serializer.serializable_hash
16+
1817
render json: {
19-
status: 200,
20-
message: "Logged out successfully."
18+
status: { code: 200, message: 'Logged in successfully.' },
19+
user: user_data[:data] ? user_data[:data][:attributes] : user_data,
20+
token: token
2121
}, status: :ok
2222
else
2323
render json: {
24-
status: 401,
25-
message: "Couldn't find an active session."
24+
error: 'Invalid email or password'
2625
}, status: :unauthorized
2726
end
2827
end
28+
29+
def destroy
30+
# For JWT, logout is typically handled client-side by discarding the token
31+
render json: {
32+
status: 200,
33+
message: "Logged out successfully."
34+
}, status: :ok
35+
end
2936
end
3037
end
3138
end

app/models/user.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ def flipper_id
3434
admin: "admin",
3535
}, default: "default"
3636

37+
# Service account flag
38+
scope :service_accounts, -> { where(is_service_account: true) }
39+
scope :regular_users, -> { where(is_service_account: false) }
40+
3741
# Validations (Devise handles email and password validations)
3842
validates :role, inclusion: { in: roles.keys }
3943

@@ -74,6 +78,28 @@ def premium?
7478
# Could check subscription status, role, etc.
7579
admin?
7680
end
81+
82+
# Custom JWT payload for Devise JWT
83+
def jwt_payload
84+
# Service accounts get longer token expiration (30 days)
85+
# Regular users get standard expiration (1 day)
86+
expiration = is_service_account? ? 30.days.from_now : 1.day.from_now
87+
88+
{
89+
'sub' => id.to_s,
90+
'scp' => 'user',
91+
'aud' => nil,
92+
'iat' => Time.current.to_i,
93+
'exp' => expiration.to_i,
94+
'jti' => SecureRandom.uuid,
95+
'service_account' => is_service_account?
96+
}
97+
end
98+
99+
# Helper method to identify service accounts
100+
def service_account?
101+
is_service_account?
102+
end
77103

78104
# Flipper actor
79105
def flipper_id

config/routes.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@
109109

110110
root to: "streams#index"
111111
end
112+
113+
# Redirect /admin to admin dashboard
114+
get "/admin", to: redirect("/admin/streams")
112115

113116
# Mount Flipper UI without authentication - we'll handle it via Rack middleware
114117
mount Flipper::UI.app(Flipper) => "/admin/flipper"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class AddServiceAccountToUsers < ActiveRecord::Migration[8.0]
2+
def change
3+
add_column :users, :is_service_account, :boolean, default: false, null: false
4+
add_index :users, :is_service_account
5+
end
6+
end

db/schema.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[8.0].define(version: 2025_07_10_200215) do
13+
ActiveRecord::Schema[8.0].define(version: 2025_07_10_203831) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "pg_catalog.plpgsql"
1616

@@ -168,7 +168,9 @@
168168
t.string "reset_password_token"
169169
t.datetime "reset_password_sent_at"
170170
t.datetime "remember_created_at"
171+
t.boolean "is_service_account", default: false, null: false
171172
t.index ["email"], name: "index_users_on_email", unique: true
173+
t.index ["is_service_account"], name: "index_users_on_is_service_account"
172174
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
173175
t.index ["role"], name: "index_users_on_role"
174176
end

db/seeds/feature_flags.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@
2424
Flipper.disable(ApplicationConstants::Features::MAINTENANCE_MODE)
2525
Rails.logger.debug "Disabled MAINTENANCE_MODE"
2626

27+
# Disable location validation by default
28+
Flipper.disable(ApplicationConstants::Features::LOCATION_VALIDATION)
29+
Rails.logger.debug "Disabled LOCATION_VALIDATION"
30+
2731
Rails.logger.debug "Feature flags setup complete!"

0 commit comments

Comments
 (0)