File tree Expand file tree Collapse file tree
controllers/api/internal/users Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ module Api
4+ module Internal
5+ module Users
6+ class DeletionsController < ApplicationController
7+ protect_from_forgery with : :exception
8+ before_action :authenticate_request!
9+
10+ def create
11+ UserCleanupJob . perform_later (
12+ params [ :user_id ]
13+ )
14+
15+ head :ok
16+ end
17+
18+ private
19+
20+ def authenticate_request!
21+ verify_token!
22+ verify_signature!
23+ end
24+
25+ def verify_token!
26+ token = request . headers [ 'Authorization' ] &.remove ( 'Bearer ' )
27+
28+ unless ActiveSupport ::SecurityUtils . secure_compare (
29+ token . to_s ,
30+ ENV . fetch ( 'OPENHPI_API_TOKEN' )
31+ )
32+ head :unauthorized
33+ return
34+ end
35+ end
36+
37+ def verify_signature!
38+ expected = OpenSSL ::HMAC . hexdigest (
39+ 'SHA256' ,
40+ ENV . fetch ( 'OPENHPI_WEBHOOK_SECRET' ) ,
41+ request . raw_post
42+ )
43+
44+ provided = request . headers [ 'X-Signature' ]
45+
46+ unless ActiveSupport ::SecurityUtils . secure_compare ( expected , provided . to_s )
47+ head :unauthorized
48+ return
49+ end
50+ end
51+ end
52+ end
53+ end
54+ end
Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ class UserCleanupJob < ApplicationJob
4+ queue_as :default
5+
6+ retry_on StandardError , wait : :exponentially_longer , attempts : 10
7+
8+ def perform ( user_id )
9+ cleanup_user_data ( user_id )
10+ end
11+
12+ private
13+
14+ def cleanup_user_data ( user_id )
15+ user = ExternalUser . find_by ( external_id : user_id , consumer_id : 1 ) # Consumer with ID 1 is openHPI.
16+
17+ if user . present?
18+ user . update ( name : 'Deleted User' , email : nil )
19+ end
20+ end
21+ end
Original file line number Diff line number Diff line change 193193 mount ActionCable . server => '/cable'
194194 mount RailsAdmin ::Engine => '/rails_admin' , as : 'rails_admin'
195195
196+ namespace :api do
197+ namespace :internal do
198+ namespace :users do
199+ post 'deleted' , to : 'deletions#create'
200+ end
201+ end
202+ end
203+
196204 # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
197205 # Can be used by load balancers and uptime monitors to verify that the app is live.
198206 get 'up' , to : 'rails/health#show' , as : :rails_health_check
You can’t perform that action at this time.
0 commit comments