-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevisejwt.sh
executable file
·360 lines (251 loc) · 7.85 KB
/
devisejwt.sh
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env bash
echo ~----------~----------Startingd $HOSTNAME, pwd: `pwd`, dlr0: "$0", bashsource0: "${BASH_SOURCE[0]}", $(date +"%Y-%m-%d_%H.%M.%S")
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# read settings..
read appn sfil sfil2 mpwd parm0</tmp/"_brvar1202_${USER}".txt
echo $appn $sfil $sfil2 $mpwd $parm0
# timeout1=5 ; read -t "${timeout1}" -p "Press ENTER or wait $timeout1 seconds..." || true ; echo ;
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo "gem 'jwt'" >> Gemfile
echo "gem 'rack-cors'" >> Gemfile
bundle
git add -A # Add all files and commit them
git commit -m "jwt1"
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# add new lines of text after patrn...
filetarg='config/initializers/devise.rb'
r1tmp="/tmp/_temprubyrunner_${USER}.rb"
cat << 'HEREDOC' > $r1tmp
repl2 = %Q{
config.warden do |manager|
# Registering your new Strategy
manager.strategies.add(:jwt, Devise::Strategies::JsonWebToken)
# Adding the new JWT Strategy to the top of Warden's list,
# Scoped by what Devise would scope (typically :user)
manager.default_strategies(scope: :user).unshift :jwt
end
}
ARGF.each do |line|
puts line
puts repl2 if line =~ /Devise\.setup/
end
HEREDOC
ruby $r1tmp $filetarg > $filetarg.tmp
cp $filetarg.tmp $filetarg; rm $filetarg.tmp
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Copy files over..
path1='config/initializers/core/extensions/devise/strategies'
mkdir -p $path1/
rm -r $path1/
# rsync.. -a - rltpgoD preserve almost all. -u update don't copy older source files.
rsync -av --ignore-times $sfil2/$path1/ $path1/
# copy secrets.yml
# first, backup secrets..
mkdir -p backup
file1='secrets.yml'
if [ -f config/$file1 ] ; then
cp -a config/$file1 backup/$file1$(date +"__%Y.%m.%d_%H.%M.%S").bak.txt
fi
#
path1='config/secrets.yml'
cp -a $sfil2/$path1 $path1
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# edit secrets.yml rather than copying it like the next commented out stanza would..
# add new lines of text after patrn...
filetarg='config/secrets.yml'
r1tmp="/tmp/_temprubyrunner_${USER}.rb"
cat << 'HEREDOC' > $r1tmp
repl2 = %Q{
# jwt token auth
# Add somewhere to your secrets yml
# "config/secrets.yml"
jwt_secret: '38af0483c50d39ebd0b58ea81a3fc366c1995c4da5bsuperrandomkey'
jwt_expiration_hours: 18
}
ARGF.each do |line|
puts line
puts repl2 if line =~ /development\:/
end
HEREDOC
ruby $r1tmp $filetarg > $filetarg.tmp
cp $filetarg.tmp $filetarg; rm $filetarg.tmp
sleep 1
# add new lines of text after patrn...
filetarg='config/secrets.yml'
r1tmp="/tmp/_temprubyrunner_${USER}.rb"
cat << 'HEREDOC' > $r1tmp
repl2 = %Q{
# jwt token auth
# Add somewhere to your secrets yml
# "config/secrets.yml"
jwt_secret: '62d2f8f20c9852a2da46b9332c8ec62ba95d82509a7c758evenmoresuperrandomkey'
jwt_expiration_hours: 18
}
ARGF.each do |line|
puts line
puts repl2 if line =~ /production\:/
end
HEREDOC
ruby $r1tmp $filetarg > $filetarg.tmp
cp $filetarg.tmp $filetarg; rm $filetarg.tmp
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function blockcomment21() {
: <<'BLOCKCOMMENT'
# careful with this one.. You may want to edit manually to preserve it's contents..
# copy secrets.yml
# first, backup secrets..
mkdir -p backup
file1='secrets.yml'
cp -a config/$file1 backup/$file1$(date +"__%Y.%m.%d_%H.%M.%S").bak.txt
path1='config'
mkdir -p $path1/
# rm -r $path1/
# rsync.. -a - rltpgoD preserve almost all. -u update don't copy older source files.
rsync -av --ignore-times $sfil2/$path1/ $path1/
BLOCKCOMMENT
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# add to home index..
cat << 'HEREDOC' >> app/views/home/index.html.erb
<br><br><br>
<hr>
JWT
<br>
# The example below is how to make a curl HTTP request with the proper authentication headers.
<br>
# Be sure to actually use a working route and not "http://localhost:3001/products.json"
<br>
<br>
<% if user_signed_in? %>
curl -i -X GET --header 'Authorization: Bearer <%= JWTWrapper.encode({ user_id: current_user.id }) %>' 'http://localhost:3001/products.json'
<% end %>
<br><br>
<hr>
HEREDOC
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# add new lines of text after patrn...
#
filetarg='app/controllers/application_controller.rb'
r1tmp="/tmp/_temprubyrunner_${USER}.rb"
cat << 'HEREDOC' > $r1tmp
repl2 = %Q{
# Be sure to enable JSON.
respond_to :html, :json
}
ARGF.each do |line|
puts line
puts repl2 if line =~ /before_action/
end
HEREDOC
ruby $r1tmp $filetarg > $filetarg.tmp
cp $filetarg.tmp $filetarg; rm $filetarg.tmp
sleep 1
# csrf handling cors..
# comment out line if matches patrn and insert repl2 before that line...
filetarg='app/controllers/application_controller.rb'
r1tmp="/tmp/_temprubyrunner_${USER}.rb"
cat << 'HEREDOC' > $r1tmp
repl2 = %Q{
protect_from_forgery with: :null_session
}
ARGF.each do |line|
if line =~ /protect_from_forgery with/ then
puts repl2
print ' #'
puts line
else
puts line
end
end
HEREDOC
ruby $r1tmp $filetarg > $filetarg.tmp
cp $filetarg.tmp $filetarg; rm $filetarg.tmp
# If patrn `forgery` not found, then add it..
pattern1='protect_from_forgery'
filetarg='app/controllers/application_controller.rb'
if ! grep -q "${pattern1}" $filetarg ; then
(
# add new lines of text after patrn...
#
filetarg='app/controllers/application_controller.rb'
r1tmp="/tmp/_temprubyrunner_${USER}.rb"
cat <<-'HEREDOC' > $r1tmp
patrn='class ApplicationController'
repl2 = %Q{
protect_from_forgery with: :null_session
}
ARGF.each do |line|
puts line
puts repl2 if line =~ /#{Regexp.escape(patrn)}/
end
HEREDOC
ruby $r1tmp $filetarg > $filetarg.tmp ; cat $filetarg.tmp ; cp $filetarg.tmp $filetarg; rm $filetarg.tmp
)
else
echo L.272 else
fi
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
git add -A # Add all files and commit them
git commit -m "jwt token auth2"
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# copy devise controllers
path1='app/controllers/users'
mkdir -p $path1/
rm -r $path1/
# rsync.. -a - rltpgoD preserve almost all. -u update don't copy older source files.
rsync -av --ignore-times $sfil2/$path1/ $path1/
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# sub lines of text when matching patrn...
filetarg='config/routes.rb'
cat << 'HEREDOC' > $r1tmp
repl2 = %Q{
devise_for :users, controllers: { sessions: 'users/sessions' }
}
ARGF.each do |line|
if line =~ /devise_for/ then
puts repl2
else
puts line
end
end
HEREDOC
r1tmp="/tmp/_temprubyrunner_${USER}.rb"
ruby $r1tmp $filetarg > $filetarg.tmp
cp $filetarg.tmp $filetarg; rm $filetarg.tmp
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
git add -A # Add all files and commit them
git commit -m "jwt token auth 3"
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# add rack-cors logic.
# If patrn `x` not found, then add it..
pattern1='config.middleware.insert'
filetarg='config/application.rb'
if ! grep -q "${pattern1}" $filetarg ; then
(
# add new lines of text after patrn...
#
filetarg='config/application.rb'
r1tmp="/tmp/_temprubyrunner_${USER}.rb"
cat <<-'HEREDOC' > $r1tmp
patrn='class Application'
repl2 = %Q{
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: [:get, :post, :options]
end
end
}
ARGF.each do |line|
puts line
puts repl2 if line =~ /#{Regexp.escape(patrn)}/
end
HEREDOC
ruby $r1tmp $filetarg > $filetarg.tmp ; cat $filetarg.tmp ; cp $filetarg.tmp $filetarg; rm $filetarg.tmp
)
else
echo L.358 was already there
fi