-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1936 lines (1348 loc) · 103 KB
/
index.html
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
<meta charset="utf-8">
<title>$ man jerome_</title>
<meta name="author" content="jerome basa">
<meta name="description" content="in this post i will cover the security related stuff for rails which i learned
over the last couple of years. security is hard. rails is pretty …">
<!-- http://t.co/dKP3o1e -->
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="canonical" href="http://jldbasa.github.io">
<link href="/favicon.png" rel="icon">
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
<link href="/atom.xml" rel="alternate" title="$ man jerome_" type="application/atom+xml">
<script src="/javascripts/modernizr-2.0.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="./javascripts/libs/jquery.min.js"%3E%3C/script%3E'))</script>
<script src="/javascripts/octopress.js" type="text/javascript"></script>
<!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
<link href="http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
</head>
<body >
<header role="banner"><hgroup>
<h1><a href="/">$ man jerome_</a></h1>
<h2>...</h2>
</hgroup>
</header>
<nav role="navigation"><ul class="subscription" data-subscription="rss">
<li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
</ul>
<form action="http://google.com/search" method="get">
<fieldset role="search">
<input type="hidden" name="q" value="site:jldbasa.github.io" />
<input class="search" type="text" name="q" results="0" placeholder="Search"/>
</fieldset>
</form>
<ul class="main-navigation">
<li><a href="/">Blog</a></li>
<li><a href="/blog/archives">Archives</a></li>
</ul>
</nav>
<div id="main">
<div id="content">
<div class="blog-index">
<article>
<header>
<h1 class="entry-title"><a href="/blog/2014/03/06/rails-security-pitfalls/">rails security pitfalls</a></h1>
<p class="meta">
<time datetime="2014-03-06T06:00:10-08:00" pubdate data-updated="true">Mar 6<span>th</span>, 2014</time>
| <a href="/blog/2014/03/06/rails-security-pitfalls/#disqus_thread"
data-disqus-identifier="http://jldbasa.github.io/blog/2014/03/06/rails-security-pitfalls/">Comments</a>
</p>
</header>
<div class="entry-content"><p>in this post i will cover the security related stuff for rails which i learned
over the last couple of years.</p>
<p>security is <strong>hard</strong>. rails is pretty secure by default (from hackers? maybe not),
but a simple mistake will lead to a catastrophe. as much as possible, try to
avoid the common pitfalls. as the saying goes <strong>prevention is better than cure</strong></p>
<h1>Common Attacks</h1>
<h2>SQL Injection</h2>
<p><img src="http://imgs.xkcd.com/comics/exploits_of_a_mom.png" alt="Exploits of a Mom" /></p>
<h3>simple case</h3>
<p>building your own conditions is vulnerable to sql injection. the code below is
not safe.</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">User</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="s2">"username LIKE '%</span><span class="si">#{</span><span class="n">params</span><span class="o">[</span><span class="ss">:username</span><span class="o">]</span><span class="si">}</span><span class="s2">%'"</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="c1"># query</span>
</span><span class='line'><span class="no">SELECT</span>
</span><span class='line'> <span class="sb">`users`</span><span class="o">.</span><span class="n">*</span>
</span><span class='line'><span class="no">FROM</span>
</span><span class='line'> <span class="sb">`users`</span>
</span><span class='line'><span class="no">WHERE</span> <span class="p">(</span><span class="n">username</span> <span class="no">LIKE</span> <span class="s1">'%jerome%'</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<p>let say the attacker sets the value of <code>params[:username]</code> e.g.</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">params</span><span class="o">[</span><span class="ss">:username</span><span class="o">]</span> <span class="o">=</span> <span class="s2">"') UNION SELECT username, password,1,1,1 FROM users --"</span>
</span><span class='line'>
</span><span class='line'><span class="no">User</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="s2">"username LIKE '%</span><span class="si">#{</span><span class="n">params</span><span class="o">[</span><span class="ss">:username</span><span class="o">]</span><span class="si">}</span><span class="s2">%'"</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="c1"># query</span>
</span><span class='line'><span class="no">SELECT</span>
</span><span class='line'> <span class="sb">`users`</span><span class="o">.</span><span class="n">*</span>
</span><span class='line'><span class="no">FROM</span>
</span><span class='line'> <span class="sb">`users`</span>
</span><span class='line'><span class="no">WHERE</span> <span class="p">(</span><span class="n">username</span> <span class="no">LIKE</span> <span class="s1">'%'</span><span class="p">)</span> <span class="no">UNION</span> <span class="no">SELECT</span> <span class="n">username</span><span class="p">,</span> <span class="n">password</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span> <span class="no">FROM</span> <span class="n">users</span> <span class="o">--%</span><span class="err">'</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<p>anything after <code>--</code> will become a comment.</p>
<h3>countermeasure</h3>
<p>instead of passing the string directly to the condition option, you can pass an array to
sanitize the string.</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">User</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="s2">"username LIKE ?"</span><span class="p">,</span> <span class="s2">"%</span><span class="si">#{</span><span class="n">params</span><span class="o">[</span><span class="ss">:username</span><span class="o">]</span><span class="si">}</span><span class="s2">%"</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<h2>XSS</h2>
<p>the rule of the thumb is <strong>never trust any data that comes from a user</strong></p>
<h3>simple case</h3>
<p>let say we have this code:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o"><</span><span class="n">span</span><span class="o">></span>
</span><span class='line'> <span class="o"><%=</span> <span class="n">raw</span> <span class="n">user</span><span class="o">.</span><span class="n">biography</span> <span class="sx">%></span>
</span><span class='line'><span class="sx"></span></span>
</span></code></pre></td></tr></table></div></figure>
<p>and the attacker sets the value of <code>user.biography</code> to <code><script>alert('hello');</script></code>
the rendered page will have this.</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o"><</span><span class="n">span</span><span class="o">></span>
</span><span class='line'> <span class="o"><</span><span class="n">script</span><span class="o">></span><span class="n">alert</span><span class="p">(</span><span class="s1">'hello'</span><span class="p">);</span><span class="o"><</span><span class="sr">/script></span>
</span><span class='line'><span class="sr"></s</span><span class="n">pan</span><span class="o">></span>
</span></code></pre></td></tr></table></div></figure>
<p>our page will now execute this javascript code!</p>
<h3>countermeasure</h3>
<p>user input must be sanitized, you can use various Rails methods such as <code>sanitize</code></p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o"><</span><span class="n">span</span><span class="o">></span>
</span><span class='line'> <span class="o"><%=</span> <span class="n">sanitize</span> <span class="p">(</span><span class="n">user</span><span class="o">.</span><span class="n">biography</span><span class="p">,</span> <span class="ss">tags</span><span class="p">:</span> <span class="sx">%w(a)</span><span class="p">,</span> <span class="ss">attributes</span><span class="p">:</span> <span class="sx">%w(href)</span><span class="p">)</span> <span class="sx">%></span>
</span><span class='line'><span class="sx"></span></span>
</span></code></pre></td></tr></table></div></figure>
<h2>CSRF – Cross-Site Request Forgery</h2>
<h3>simple case</h3>
<ul>
<li>attacker sends requests on victim’s behalf</li>
<li>doesn’t depend on XSS</li>
</ul>
<h3>countermeasure</h3>
<ul>
<li>use <code>GET</code> and <code>POST</code> methods appropriately</li>
<li>use Rails default CSRF protection</li>
</ul>
<h1>Rails Specific Attacks</h1>
<h2>Mass Assignment</h2>
<h3>problem</h3>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">create</span>
</span><span class='line'> <span class="vi">@user</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">params</span><span class="o">[</span><span class="ss">:user</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>if you have <code>User.admin</code> attribute; and the attacker sends
<code>params[:user][:admin] = 1</code>, then the <code>admin</code> attribute will be set.</p>
<h3>solution</h3>
<ul>
<li>blacklist attributes using <code>attr_protected</code></li>
</ul>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">User</span> <span class="o"><</span> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span>
</span><span class='line'> <span class="n">attr_protected</span> <span class="ss">:admin</span>
</span><span class='line'> <span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<ul>
<li>whitelist attributes using <code>attr_accessible</code></li>
</ul>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">User</span> <span class="o"><</span> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span>
</span><span class='line'> <span class="n">attr_accessible</span> <span class="ss">:username</span><span class="p">,</span> <span class="ss">:email</span>
</span><span class='line'> <span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<ul>
<li>use strong parameters</li>
</ul>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">create</span>
</span><span class='line'> <span class="vi">@user</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">params_user</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="kp">private</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">params_user</span>
</span><span class='line'> <span class="n">params</span><span class="o">.</span><span class="n">require</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span><span class="o">.</span><span class="n">permit</span><span class="p">(</span>
</span><span class='line'> <span class="ss">:username</span><span class="p">,</span>
</span><span class='line'> <span class="ss">:email</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span></code></pre></td></tr></table></div></figure>
<h2>Secret Token</h2>
<p>this token is used to sign cookies that the application sets. you can generate
a token by running <code>$ rake secret</code></p>
<h3>problem</h3>
<p>by default the token is stored in the file. most of the time this file is in
version control</p>
<figure class='code'><figcaption><span>config/initializers/secret_token.rb </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="ss">MyApp</span><span class="p">:</span><span class="ss">:Application</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">secret_token</span> <span class="o">=</span> <span class="s1">'c38d07e4b543baeea5ae70f5dd670828ec67eb3c4f585020d02667804cb39a3142704028055540b2270c5a8253cea780b16fa353b86f94c17c923ac484a20856'</span>
</span></code></pre></td></tr></table></div></figure>
<h2>solution</h2>
<p>keep it out of version control. store it in <code>ENV</code>. you can use <code>figaro</code> gem to
store data in <code>ENV</code></p>
<figure class='code'><figcaption><span>config/initializers/secret_token.rb </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="ss">MyApp</span><span class="p">:</span><span class="ss">:Application</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">secret_token</span> <span class="o">=</span> <span class="no">ENV</span><span class="o">[</span><span class="s1">'SECRET_TOKEN'</span><span class="o">]</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong>note:</strong> in Rails 4, it is called <code>secret_key_base</code>.</p>
<h2>Logging Parameters</h2>
<p>by default Rails filter any parameter that matches <code>/password/</code> from being
logged and replacing it with <code>[FILTERED]</code>. you should filter sensitive data from logs.</p>
<figure class='code'><figcaption><span>config/initializers/filter_parameter_logging.rb </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">Rails</span><span class="o">.</span><span class="n">application</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">filter_parameters</span> <span class="o">+=</span> <span class="o">[</span><span class="ss">:password</span><span class="p">,</span> <span class="ss">:ssn</span><span class="p">,</span> <span class="ss">:token</span><span class="o">]</span>
</span></code></pre></td></tr></table></div></figure>
<h2>“match” in Routing</h2>
<h3>problem</h3>
<p>in Rails 3, there’s even an example in <code>config/routes.rb</code></p>
<figure class='code'><figcaption><span>config/routes.rb </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># Example in config/routes.rb</span>
</span><span class='line'><span class="n">match</span> <span class="s1">':controller(/:action(/:id))(.:format)'</span>
</span><span class='line'>
</span><span class='line'><span class="n">match</span> <span class="s1">'/posts/delete/:id'</span><span class="p">,</span> <span class="ss">:to</span> <span class="o">=></span> <span class="s2">"posts#destroy"</span> <span class="ss">:as</span> <span class="o">=></span> <span class="s2">"delete_post"</span>
</span></code></pre></td></tr></table></div></figure>
<ul>
<li><code>match</code> matches all HTTP verb and Rails CSRF protection doesn’t apply to
<code>GET</code> requests.</li>
<li>the second route will allow <code>GET</code> method to delete posts</li>
</ul>
<h3>solution</h3>
<ul>
<li>use the correct HTTP verb e.g: <code>:get, :post, :delete</code></li>
<li>use <code>:via</code> e.g. <code>match '/posts/delete/:id', :to => "posts#destroy" :as =>
"delete_post", :via => :delete</code></li>
</ul>
<h2>Scopes</h2>
<ul>
<li>let say we have <code>user</code> and <code>post</code> models, when you you retrieve <code>post</code> for
<code>edit</code>, <code>update</code> or <code>destroy</code>, make sure you get it through authorize user.
in the code below; <code>Example 2</code> is the preferred way</li>
</ul>
<figure class='code'><figcaption><span>app/controllers/posts_controller.rb </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span><span class='line'><span class="c1"># Example 1 (UNSAFE)</span>
</span><span class='line'><span class="k">def</span> <span class="nf">edit</span>
</span><span class='line'> <span class="vi">@post</span> <span class="o">=</span> <span class="no">Post</span><span class="o">.</span><span class="n">find_by</span> <span class="nb">id</span><span class="p">:</span> <span class="n">params</span><span class="o">[</span><span class="ss">:id</span><span class="o">]</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="c1"># Example 2 (SAFE)</span>
</span><span class='line'><span class="k">def</span> <span class="nf">edit</span>
</span><span class='line'> <span class="vi">@post</span> <span class="o">=</span> <span class="n">current_user</span><span class="o">.</span><span class="n">posts</span><span class="o">.</span><span class="n">find_by</span> <span class="nb">id</span><span class="p">:</span> <span class="n">params</span><span class="o">[</span><span class="ss">:id</span><span class="o">]</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="o">.</span><span class="n">.</span><span class="o">.</span>
</span></code></pre></td></tr></table></div></figure>
<ul>
<li>use authorization gem such as <code>cancan</code></li>
</ul>
<h2>Admin</h2>
<p>most of the time, admin URLs can be found in: <code>http://example.com/admin</code>, if you
can, please consider the following:</p>
<ul>
<li>maybe use sub-domain e.g. <code>http://some-url.example.com</code></li>
<li>whitelist IP address</li>
<li>VPN or intranet access only</li>
<li>separate application</li>
</ul>
<h1>Conclusion</h1>
<p>use <code>brakeman</code> to scan your app for vulnerabilities.</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'><span class="nv">$ </span>brakeman . -o report.html
</span></code></pre></td></tr></table></div></figure>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>+----------------------+-------+
</span><span class='line'>| Warning Type | Total |
</span><span class='line'>+----------------------+-------+
</span><span class='line'>| Cross Site Scripting | 1 |
</span><span class='line'>| SQL Injection | 1 |
</span><span class='line'>| Session Setting | 1 |
</span><span class='line'>+----------------------+-------+
</span><span class='line'>
</span><span class='line'>
</span><span class='line'>+SECURITY WARNINGS+
</span><span class='line'>
</span><span class='line'>+------------+-----------------+----------+-----------------+-------------------------------------------------------------------------+
</span><span class='line'>| Confidence | Class | Method | Warning Type | Message |
</span><span class='line'>+------------+-----------------+----------+-----------------+-------------------------------------------------------------------------+
</span><span class='line'>| High | PostsController | set_post | SQL Injection | Possible SQL injection near line 68: Post.where("id =#{+params[:id]+}") |
</span><span class='line'>| High | | | Session Setting | Session secret should not be included in version control near line 12 |
</span><span class='line'>+------------+-----------------+----------+-----------------+-------------------------------------------------------------------------+
</span><span class='line'>
</span><span class='line'>View Warnings:
</span><span class='line'>
</span><span class='line'>+------------+-------------------------------------+----------------------+-----------------------------------------------------------------------+
</span><span class='line'>| Confidence | Template | Warning Type | Message |
</span><span class='line'>+------------+-------------------------------------+----------------------+-----------------------------------------------------------------------+
</span><span class='line'>| High | posts/show (PostsController#create) | Cross Site Scripting | Unescaped model attribute near line 10: Post.new(post_params).content |
</span><span class='line'>+------------+-------------------------------------+----------------------+-----------------------------------------------------------------------+</span></code></pre></td></tr></table></div></figure>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/2014/02/14/mysql-user-minimum-required-privileges-for-rails/">mysql user minimum required privileges for rails</a></h1>
<p class="meta">
<time datetime="2014-02-14T09:51:00-08:00" pubdate data-updated="true">Feb 14<span>th</span>, 2014</time>
| <a href="/blog/2014/02/14/mysql-user-minimum-required-privileges-for-rails/#disqus_thread"
data-disqus-identifier="http://jldbasa.github.io/blog/2014/02/14/mysql-user-minimum-required-privileges-for-rails/">Comments</a>
</p>
</header>
<div class="entry-content"><p>if your application uses <code>root</code> as your database username; you’re doing it wrong.
a good practice is you should have two users for your application. let say
i have a database called <code>blog_production</code>, i will have two users in my
database: <code>blog</code> and <code>blog_admin</code>. the former will be use for regular stuff,
and the latter for database administration. each db user have diffrent set of privileges.</p>
<ul>
<li><code>blog</code> user privileges
<ul>
<li>Select</li>
<li>Insert</li>
<li>Update</li>
<li>Delete</li>
<li>Lock</li>
</ul>
</li>
<li><code>blog_admin</code> user privileges
<ul>
<li>Select</li>
<li>Insert</li>
<li>Update</li>
<li>Delete</li>
<li>Lock</li>
<li>Create</li>
<li>Drop</li>
<li>Index</li>
<li>Alter</li>
</ul>
</li>
</ul>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='mysql'><span class='line'><span class="k">CREATE</span> <span class="k">DATABASE</span> <span class="n">blog_production</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">CREATE</span> <span class="n">USER</span> <span class="s1">'blog'</span><span class="o">@</span><span class="s1">'localhost'</span> <span class="n">IDENTIFIED</span> <span class="k">BY</span> <span class="s1">'db_password'</span><span class="p">;</span>
</span><span class='line'><span class="k">GRANT</span> <span class="k">Select</span><span class="p">,</span><span class="k">Insert</span><span class="p">,</span><span class="k">Update</span><span class="p">,</span><span class="k">Delete</span><span class="p">,</span><span class="k">Lock</span> <span class="kp">Tables</span> <span class="k">ON</span> <span class="n">blog_production</span><span class="p">.</span><span class="o">*</span> <span class="k">TO</span> <span class="s1">'blog'</span><span class="o">@</span><span class="s1">'localhost'</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">CREATE</span> <span class="n">USER</span> <span class="s1">'blog_admin'</span><span class="o">@</span><span class="s1">'localhost'</span> <span class="n">IDENTIFIED</span> <span class="k">BY</span> <span class="s1">'db_password'</span><span class="p">;</span>
</span><span class='line'><span class="k">GRANT</span> <span class="k">Select</span><span class="p">,</span><span class="k">Insert</span><span class="p">,</span><span class="k">Update</span><span class="p">,</span><span class="k">Delete</span><span class="p">,</span><span class="k">Create</span><span class="p">,</span><span class="k">Drop</span><span class="p">,</span><span class="k">Index</span><span class="p">,</span><span class="k">Alter</span><span class="p">,</span><span class="k">Lock</span> <span class="kp">Tables</span> <span class="k">ON</span> <span class="n">blog_production</span><span class="p">.</span><span class="o">*</span> <span class="k">TO</span> <span class="s1">'blog_admin'</span><span class="o">@</span><span class="s1">'localhost'</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="n">FLUSH</span> <span class="n">PRIVILEGES</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>
<p>now, how do you use this two users in your rails app? in <code>databases.yml</code>, add a check for <code>DB_ADMIN</code> env var.</p>
<figure class='code'><figcaption><span>config/databases.yml </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o"><%</span>
</span><span class='line'> <span class="k">if</span> <span class="no">ENV</span><span class="o">[</span><span class="s1">'DB_ADMIN'</span><span class="o">]</span>
</span><span class='line'> <span class="n">username</span> <span class="o">=</span> <span class="s2">"blog_admin"</span>
</span><span class='line'> <span class="n">password</span> <span class="o">=</span> <span class="s2">"11111111"</span>
</span><span class='line'> <span class="k">else</span>
</span><span class='line'> <span class="n">username</span> <span class="o">=</span> <span class="s2">"blog"</span>
</span><span class='line'> <span class="n">password</span> <span class="o">=</span> <span class="s2">"22222222"</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="sx">%></span>
</span><span class='line'><span class="sx">production:</span>
</span><span class='line'><span class="sx"> adapter: mysql2</span>
</span><span class='line'><span class="sx"> encoding: utf8</span>
</span><span class='line'><span class="sx"> reconnect: false</span>
</span><span class='line'><span class="sx"> database: blog_production</span>
</span><span class='line'><span class="sx"> pool: 5</span>
</span><span class='line'><span class="sx"> username: <%= username %></span>
</span><span class='line'> <span class="ss">password</span><span class="p">:</span> <span class="o"><%=</span> <span class="n">password</span> <span class="o">%></span>
</span></code></pre></td></tr></table></div></figure>
<p>by default it uses <code>blog</code> db username unless you pass <code>DB_ADMIN=true</code> env var.
the reasone we have this env is because we need to pass it when we run
migration tasks e.g:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ DB_ADMIN</span><span class="o">=</span><span class="nb">true </span>bundle <span class="nb">exec </span>rails db:migrate
</span></code></pre></td></tr></table></div></figure>
<p>if you use <strong>capistrano</strong> for deployment, you need to set <code>migrate_env</code> var.</p>
<figure class='code'><figcaption><span>config/deploy.rb </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">set</span> <span class="ss">:migrate_env</span><span class="p">,</span> <span class="s2">"DB_ADMIN=true"</span>
</span></code></pre></td></tr></table></div></figure>
<p>if you have better method, hit the comments.</p>
<p>and that’s all there is to it. hth.</p>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/2014/01/25/rails-4-paypal-express-checkout-integration/">rails 4: paypal express checkout integration using activemerchant</a></h1>
<p class="meta">
<time datetime="2014-01-25T05:35:00-08:00" pubdate data-updated="true">Jan 25<span>th</span>, 2014</time>
| <a href="/blog/2014/01/25/rails-4-paypal-express-checkout-integration/#disqus_thread"
data-disqus-identifier="http://jldbasa.github.io/blog/2014/01/25/rails-4-paypal-express-checkout-integration/">Comments</a>
</p>
</header>
<div class="entry-content"><p>in this post, i will share on how i integrated Paypal’s Express checkout in a rails 4 application using <code>activemerchant</code> gem. some of the code are based on <a href="http://railscasts.com/episodes/146-paypal-express-checkout">Railscasts Ep. 146</a>.</p>
<p><strong>before proceeding, make sure you have or did the following:</strong></p>
<ul>
<li><a href="http://developer.paypal.com">Paypal developer account</a></li>
<li>create Paypal sandbox accounts: merchant and buyer(go to Applications » Sandbox Accounts)</li>
</ul>
<p><strong>Gems to use</strong></p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">activemerchant</span> <span class="p">(</span><span class="mi">1</span><span class="o">.</span><span class="mi">42</span><span class="o">.</span><span class="mi">4</span><span class="p">)</span>
</span><span class='line'><span class="n">rails</span> <span class="p">(</span><span class="mi">4</span><span class="o">.</span><span class="mi">0</span><span class="o">.</span><span class="mi">2</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<h2>Setup Activemerchant</h2>
<figure class='code'><figcaption><span>config/environments/developer.rb </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">config</span><span class="o">.</span><span class="n">after_initialize</span> <span class="k">do</span>
</span><span class='line'> <span class="ss">ActiveMerchant</span><span class="p">:</span><span class="ss">:Billing</span><span class="o">::</span><span class="no">Base</span><span class="o">.</span><span class="n">mode</span> <span class="o">=</span> <span class="ss">:test</span>
</span><span class='line'> <span class="n">paypal_options</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'> <span class="ss">login</span><span class="p">:</span> <span class="s2">"API_USERNAME_HERE"</span><span class="p">,</span>
</span><span class='line'> <span class="ss">password</span><span class="p">:</span> <span class="s2">"API_PASSWORD_HERE"</span><span class="p">,</span>
</span><span class='line'> <span class="ss">signature</span><span class="p">:</span> <span class="s2">"API_SIGNATURE_HERE"</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="o">::</span><span class="no">EXPRESS_GATEWAY</span> <span class="o">=</span> <span class="ss">ActiveMerchant</span><span class="p">:</span><span class="ss">:Billing</span><span class="o">::</span><span class="no">PaypalExpressGateway</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">paypal_options</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<figure class='code'><figcaption><span>config/environments/test.rb </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">config</span><span class="o">.</span><span class="n">after_initialize</span> <span class="k">do</span>
</span><span class='line'> <span class="ss">ActiveMerchant</span><span class="p">:</span><span class="ss">:Billing</span><span class="o">::</span><span class="no">Base</span><span class="o">.</span><span class="n">mode</span> <span class="o">=</span> <span class="ss">:test</span>
</span><span class='line'> <span class="o">::</span><span class="no">EXPRESS_GATEWAY</span> <span class="o">=</span> <span class="ss">ActiveMerchant</span><span class="p">:</span><span class="ss">:Billing</span><span class="o">::</span><span class="no">BogusGateway</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<h2>Model changes</h2>
<p>this really depends on your app. maybe you have a <code>carts</code> and <code>orders</code> tables. or <code>bookings</code> and <code>reservations</code> tables. assuming you have <code>carts</code> and <code>orders</code> tables, you’ll have the following fields.</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>carts
</span><span class='line'> - id
</span><span class='line'> - total_amount_cents
</span><span class='line'> - purchased_at
</span><span class='line'> - created_at
</span><span class='line'> - updated_at
</span><span class='line'>
</span><span class='line'>orders
</span><span class='line'> - id
</span><span class='line'> - cart_id
</span><span class='line'> - ip
</span><span class='line'> - express_token
</span><span class='line'> - express_payer_id</span></code></pre></td></tr></table></div></figure>
<h2>View Changes</h2>
<p>after you add the <code>express_checkout</code> to your routes file; in your cart page, put the express paypal checkout button</p>
<figure class='code'><figcaption><span>app/views/carts/index.html.erb </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">link_to</span><span class="p">(</span><span class="n">image_tag</span><span class="p">(</span><span class="s2">"https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif"</span><span class="p">),</span> <span class="n">express_checkout_path</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong>IMPORTANT:</strong> Paypal wants you to use their buttons, so use them! LOL</p>
<h2>Controller Changes</h2>
<p>add the following actions in your <code>orders</code> controller.</p>
<ul>
<li><code>express_checkout</code> – this will setup your purchase and redirect to paypal.</li>
<li><code>new</code> – a.k.a. Paypal’s return URL. i usually add a “Confirm Order” button in this action. it’s a good practice not to process the order right away.</li>
<li><code>create</code> – create and purchase the order</li>
</ul>
<figure class='code'><figcaption><span>app/controllers/orders_controller.rb </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">express_checkout</span>
</span><span class='line'> <span class="n">response</span> <span class="o">=</span> <span class="no">EXPRESS_GATEWAY</span><span class="o">.</span><span class="n">setup_purchase</span><span class="p">(</span><span class="no">YOUR_TOTAL_AMOUNT_IN_CENTS</span><span class="p">,</span>
</span><span class='line'> <span class="ss">ip</span><span class="p">:</span> <span class="n">request</span><span class="o">.</span><span class="n">remote_ip</span><span class="p">,</span>
</span><span class='line'> <span class="n">return_url</span><span class="p">:</span> <span class="no">YOUR_RETURN_URL_</span><span class="p">,</span>
</span><span class='line'> <span class="n">cancel_return_url</span><span class="p">:</span> <span class="no">YOUR_CANCEL_RETURL_URL</span><span class="p">,</span>
</span><span class='line'> <span class="ss">currency</span><span class="p">:</span> <span class="s2">"USD"</span><span class="p">,</span>
</span><span class='line'> <span class="n">allow_guest_checkout</span><span class="p">:</span> <span class="kp">true</span><span class="p">,</span>
</span><span class='line'> <span class="ss">items</span><span class="p">:</span> <span class="o">[</span><span class="p">{</span><span class="nb">name</span><span class="p">:</span> <span class="s2">"Order"</span><span class="p">,</span> <span class="ss">description</span><span class="p">:</span> <span class="s2">"Order description"</span><span class="p">,</span> <span class="ss">quantity</span><span class="p">:</span> <span class="s2">"1"</span><span class="p">,</span> <span class="ss">amount</span><span class="p">:</span> <span class="no">AMOUNT_IN_CENTS</span><span class="p">}</span><span class="o">]</span>
</span><span class='line'> <span class="p">)</span>
</span><span class='line'> <span class="n">redirect_to</span> <span class="no">EXPRESS_GATEWAY</span><span class="o">.</span><span class="n">redirect_url_for</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">token</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">new</span>
</span><span class='line'> <span class="vi">@order</span> <span class="o">=</span> <span class="no">Order</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">:express_token</span> <span class="o">=></span> <span class="n">params</span><span class="o">[</span><span class="ss">:token</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">def</span> <span class="nf">create</span>
</span><span class='line'> <span class="vi">@order</span> <span class="o">=</span> <span class="vi">@cart</span><span class="o">.</span><span class="n">build_order</span><span class="p">(</span><span class="n">order_params</span><span class="p">)</span>
</span><span class='line'> <span class="vi">@order</span><span class="o">.</span><span class="n">ip</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">remote_ip</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="vi">@order</span><span class="o">.</span><span class="n">save</span>
</span><span class='line'> <span class="k">if</span> <span class="vi">@order</span><span class="o">.</span><span class="n">purchase</span> <span class="c1"># this is where we purchase the order. refer to the model method below</span>
</span><span class='line'> <span class="n">redirect_to</span> <span class="n">order_url</span><span class="p">(</span><span class="vi">@order</span><span class="p">)</span>
</span><span class='line'> <span class="k">else</span>
</span><span class='line'> <span class="n">render</span> <span class="ss">:action</span> <span class="o">=></span> <span class="s2">"failure"</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">else</span>
</span><span class='line'> <span class="n">render</span> <span class="ss">:action</span> <span class="o">=></span> <span class="s1">'new'</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong>IMPORTANT:</strong> the total_amount must be equal to the total amount of <code>items</code> in the array of hashes or else you will get errors.</p>
<h2>Model Changes</h2>
<figure class='code'><figcaption><span>app/models/order.rb </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Order</span> <span class="o"><</span> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span>
</span><span class='line'> <span class="n">belongs_to</span> <span class="ss">:cart</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">purchase</span>
</span><span class='line'> <span class="n">response</span> <span class="o">=</span> <span class="no">EXPRESS_GATEWAY</span><span class="o">.</span><span class="n">purchase</span><span class="p">(</span><span class="n">order</span><span class="o">.</span><span class="n">total_amount_cents</span><span class="p">,</span> <span class="n">express_purchase_options</span><span class="p">)</span>
</span><span class='line'> <span class="n">cart</span><span class="o">.</span><span class="n">update_attribute</span><span class="p">(</span><span class="ss">:purchased_at</span><span class="p">,</span> <span class="no">Time</span><span class="o">.</span><span class="n">now</span><span class="p">)</span> <span class="k">if</span> <span class="n">response</span><span class="o">.</span><span class="n">success?</span>
</span><span class='line'> <span class="n">response</span><span class="o">.</span><span class="n">success?</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">express_token</span><span class="o">=</span><span class="p">(</span><span class="n">token</span><span class="p">)</span>
</span><span class='line'> <span class="nb">self</span><span class="o">[</span><span class="ss">:express_token</span><span class="o">]</span> <span class="o">=</span> <span class="n">token</span>
</span><span class='line'> <span class="k">if</span> <span class="n">new_record?</span> <span class="o">&&</span> <span class="o">!</span><span class="n">token</span><span class="o">.</span><span class="n">blank?</span>
</span><span class='line'> <span class="c1"># you can dump details var if you need more info from buyer</span>
</span><span class='line'> <span class="n">details</span> <span class="o">=</span> <span class="no">EXPRESS_GATEWAY</span><span class="o">.</span><span class="n">details_for</span><span class="p">(</span><span class="n">token</span><span class="p">)</span>
</span><span class='line'> <span class="nb">self</span><span class="o">.</span><span class="n">express_payer_id</span> <span class="o">=</span> <span class="n">details</span><span class="o">.</span><span class="n">payer_id</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="kp">private</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">express_purchase_options</span>
</span><span class='line'> <span class="p">{</span>
</span><span class='line'> <span class="ss">:ip</span> <span class="o">=></span> <span class="n">ip</span><span class="p">,</span>
</span><span class='line'> <span class="ss">:token</span> <span class="o">=></span> <span class="n">express_token</span><span class="p">,</span>
</span><span class='line'> <span class="ss">:payer_id</span> <span class="o">=></span> <span class="n">express_payer_id</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>and that’s all there is to it. hth.</p>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/2014/01/12/rails-4-login-with-devise-and-facebook/">rails 4: login with devise and facebook</a></h1>
<p class="meta">
<time datetime="2014-01-12T08:43:00-08:00" pubdate data-updated="true">Jan 12<span>th</span>, 2014</time>
| <a href="/blog/2014/01/12/rails-4-login-with-devise-and-facebook/#disqus_thread"
data-disqus-identifier="http://jldbasa.github.io/blog/2014/01/12/rails-4-login-with-devise-and-facebook/">Comments</a>
</p>
</header>
<div class="entry-content"><p>i recently created an application where you can login with its own
authentication system and third party sites like facebook, twitter, linkedin, etc.
for this task, i used <code>devise</code>, <code>omniauth</code> and <code>omniauth-facebook</code> gems.</p>
<p>make sure that you setup your facebook app. we’ll use the fb app id and secret
later. let’s get started.</p>
<p><strong>Gems to use</strong></p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">devise</span> <span class="p">(</span><span class="mi">3</span><span class="o">.</span><span class="mi">2</span><span class="o">.</span><span class="mi">2</span><span class="p">)</span>