|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'dotenv/load' |
| 4 | +require 'sinatra' |
| 5 | +require 'workos' |
| 6 | +require 'date' |
| 7 | +require_relative 'audit_log_events.rb' |
| 8 | + |
| 9 | +# Pull API key and Client ID from ENV variable |
| 10 | +WorkOS.key = ENV['WORKOS_API_KEY'] |
| 11 | +CLIENT_ID = ENV['WORKOS_CLIENT_ID'] |
| 12 | + |
| 13 | +enable :sessions |
| 14 | + |
| 15 | +use( |
| 16 | + Rack::Session::Cookie, |
| 17 | + key: 'rack.session', |
| 18 | + domain: 'localhost', |
| 19 | + path: '/', |
| 20 | + expire_after: 2_592_000, |
| 21 | + secret: SecureRandom.hex(16) |
| 22 | +) |
| 23 | + |
| 24 | +get '/' do |
| 25 | + erb :login, :layout => :layout |
| 26 | +end |
| 27 | + |
| 28 | +post '/set_org' do |
| 29 | + @organization_id = params[:org] |
| 30 | + |
| 31 | + session[:organization_id] = @organization_id |
| 32 | + |
| 33 | + organization = WorkOS::Organizations.get_organization( |
| 34 | + id: @organization_id |
| 35 | + ) |
| 36 | + |
| 37 | + @org_name = organization.name |
| 38 | + session[:organization_name] = @org_name |
| 39 | + erb :send_events, :layout => :layout |
| 40 | +end |
| 41 | + |
| 42 | +get '/set_org' do |
| 43 | + @organization_id = session[:organization_id] |
| 44 | + @org_name = session[:organization_name] |
| 45 | + erb :send_events, :layout => :layout |
| 46 | +end |
| 47 | + |
| 48 | +post '/send_event' do |
| 49 | + event_type = params[:event] |
| 50 | + @organization_id = session[:organization_id] |
| 51 | + @org_name = session[:organization_name] |
| 52 | + |
| 53 | + events = [ |
| 54 | + $user_signed_in, |
| 55 | + $user_logged_out, |
| 56 | + $user_organization_deleted, |
| 57 | + $user_connection_deleted, |
| 58 | + ] |
| 59 | + |
| 60 | + event = events[event_type.to_i] |
| 61 | + |
| 62 | + WorkOS::AuditLogs.create_event( |
| 63 | + organization: @organization_id, |
| 64 | + event: event |
| 65 | + ) |
| 66 | + |
| 67 | + erb :send_events, :layout => :layout |
| 68 | +end |
| 69 | + |
| 70 | +get '/export_events' do |
| 71 | + @organization_id = session[:organization_id] |
| 72 | + @org_name = session[:organization_name] |
| 73 | + erb :export_events, :layout => :layout |
| 74 | +end |
| 75 | + |
| 76 | +post '/get_events' do |
| 77 | + organization_id = session[:organization_id] |
| 78 | + event_type = params[:event] |
| 79 | + today = DateTime.now.to_s |
| 80 | + last_month = DateTime.now.prev_month.to_s |
| 81 | + |
| 82 | + if event_type == 'generate_csv' |
| 83 | + audit_log_export = WorkOS::AuditLogs.create_export( |
| 84 | + organization: organization_id, |
| 85 | + range_start: last_month, |
| 86 | + range_end: today |
| 87 | + ) |
| 88 | + session[:export_id] = audit_log_export.id |
| 89 | + puts audit_log_export.id |
| 90 | + redirect '/export_events' |
| 91 | + end |
| 92 | + |
| 93 | + if event_type == 'access_csv' |
| 94 | + export_id = session[:export_id].to_s |
| 95 | + puts export_id |
| 96 | + audit_log_export = WorkOS::AuditLogs.get_export( |
| 97 | + id: export_id |
| 98 | + ) |
| 99 | + url = audit_log_export.url |
| 100 | + |
| 101 | + redirect url |
| 102 | + end |
| 103 | + |
| 104 | +end |
| 105 | + |
| 106 | + |
| 107 | +# Logout a user |
| 108 | +get '/logout' do |
| 109 | + session[:organization_id] = nil |
| 110 | + session[:organization_name] = nil |
| 111 | + session[:export_id] = nil |
| 112 | + redirect '/' |
| 113 | +end |
0 commit comments