Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions back/app/services/user_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,11 @@ def update_in_tenant_template!(user, user_params = {})
user.confirm
user.save!
end

def jwt_token(user)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename this method (&/or add a comment) to make it clear what the use case is, as I expect other devs would get confused about which JWT token in used in the the 'regular' requests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, for some reason I thought this was a separate service within the admin API engine, but seems like I didn't see that right. I'll rename it or move it somewhere else. Or add a comment indeed.

payload = user.to_token_payload
payload[:exp] = 30.minutes.from_now.to_i
AuthToken::AuthToken.new(payload:).token
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def show
render json: @user
end

def jwt_token
user = User.find(params[:id])
json = { jwt_token: UserService.jwt_token(user) }
render json: json
end

def create
user = UserService.create_in_admin_api(user_params, confirm_user?)

Expand Down
1 change: 1 addition & 0 deletions back/engines/commercial/admin_api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

resources :users, only: %i[index create update show] do
get :by_email, on: :collection
get :jwt_token, on: :member
delete :bulk_delete_by_emails, on: :collection
end

Expand Down
25 changes: 25 additions & 0 deletions back/engines/commercial/admin_api/spec/acceptance/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,31 @@
end
end

get 'admin_api/users/:id/jwt_token' do
let(:id) { user.id }

example 'Get JWT token for a user' do
freeze_time = Time.zone.parse('2023-01-01 12:00:00')
travel_to(freeze_time) do
do_request
end

expect(status).to eq 200
jwt_token = json_parse(response_body)[:jwt_token]

# Test JWT structure (payloads starting with "{" will always start with "eyJ" when encoded)
expect(jwt_token).to match(/\AeyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\z/)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regex generated with Copilot.


# Test the decoded payload
payload, header = JWT.decode(jwt_token, nil, false)
expect(header['alg']).to be_present
expect(payload).to include(
'sub' => id,
'exp' => (freeze_time + 30.minutes).to_i
)
end
end

delete 'admin_api/users/bulk_delete_by_emails', active_job_inline_adapter: true do
parameter :emails, 'Array of user emails'

Expand Down