-
-
Notifications
You must be signed in to change notification settings - Fork 941
/
Copy pathapi_keys_controller.rb
111 lines (91 loc) · 3.03 KB
/
api_keys_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
class ApiKeysController < ApplicationController
before_action :disable_cache, only: :index
before_action :set_page, only: :index
include ApiKeyable
include SessionVerifiable
verify_session_before
def index
@api_key = session.delete(:api_key)
@api_keys_pagy, @api_keys = pagy(current_user.api_keys.unexpired.not_oidc.preload(ownership: :rubygem))
redirect_to new_profile_api_key_path if @api_keys.empty?
end
def new
@api_key = current_user.api_keys.build
end
def edit
@api_key = current_user.api_keys.find(params.permit(:id).require(:id))
return unless @api_key.soft_deleted?
flash[:error] = t(".invalid_key")
redirect_to profile_api_keys_path
end
def create
key = generate_unique_rubygems_key
build_params = { owner: current_user, hashed_key: hashed_key(key), **api_key_create_params }
@api_key = ApiKey.new(build_params)
if @api_key.errors.present?
flash.now[:error] = @api_key.errors.full_messages.to_sentence
@api_key = current_user.api_keys.build(api_key_create_params.merge(rubygem_id: nil))
return render :new
end
if @api_key.save
Mailer.api_key_created(@api_key.id).deliver_later
session[:api_key] = key
redirect_to profile_api_keys_path, flash: { notice: t(".success") }
else
flash.now[:error] = @api_key.errors.full_messages.to_sentence
render :new
end
end
def update
@api_key = current_user.api_keys.find(params.permit(:id).require(:id))
@api_key.assign_attributes(api_key_update_params(@api_key))
if @api_key.errors.present?
flash.now[:error] = @api_key.errors.full_messages.to_sentence
return render :edit
end
if @api_key.save
redirect_to profile_api_keys_path, flash: { notice: t(".success") }
else
flash.now[:error] = @api_key.errors.full_messages.to_sentence
render :edit
end
end
def destroy
api_key = current_user.api_keys.find(params.permit(:id).require(:id))
if api_key.expire!
flash[:notice] = t(".success", name: api_key.name)
else
flash[:error] = api_key.errors.full_messages.to_sentence
end
redirect_to profile_api_keys_path
end
def reset
if current_user.api_keys.expire_all!
flash[:notice] = t(".success")
else
flash[:error] = t("try_again")
end
redirect_to profile_api_keys_path
end
private
def verify_session_redirect_path
case action_name
when "reset", "destroy"
profile_api_keys_path
when "create"
new_profile_api_key_path
when "update"
edit_profile_api_key_path(params.permit(:id).require(:id))
else
super
end
end
def api_key_create_params
ApiKeysHelper.api_key_params(params.permit(api_key: [:name, *ApiKey::API_SCOPES, :mfa, :rubygem_id, :expires_at]).require(:api_key))
end
def api_key_update_params(existing_api_key = nil)
ApiKeysHelper.api_key_params(
params.permit(api_key: [*ApiKey::API_SCOPES, :mfa, :rubygem_id, { scopes: [ApiKey::API_SCOPES] }]).require(:api_key), existing_api_key
)
end
end