SHARE
TWEET

F5 APM - generate_ga_code

a guest Jan 8th, 2018 33 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. when HTTP_REQUEST {
  2.   set account [URI::query [HTTP::uri] "account"]
  3.   set domain [URI::query [HTTP::uri] "domain"]
  4.   set secret [URI::query [HTTP::uri] "secret"]
  5.   set qr_code [URI::query [HTTP::uri] "qr_code"]
  6.  
  7.   if { ([HTTP::path] starts_with "/ga_secret_generator") && ($account ne "") && ($domain ne "") } {
  8.     if { [string length $secret] <= 10  } {
  9.       set secret [b64encode [md5 [expr rand()]]]
  10.     }
  11.  
  12.     set secret [string range $secret 0 9]
  13.  
  14.     array set b32_alphabet_inv {
  15.        0 A  1 B  2 C  3 D
  16.        4 E  5 F  6 G  7 H
  17.        8 I  9 J 10 K 11 L
  18.       12 M 13 N 14 O 15 P
  19.       16 Q 17 R 18 S 19 T
  20.       20 U 21 V 22 W 23 X
  21.       24 Y 25 Z 26 2 27 3
  22.       28 4 29 5 30 6 31 7
  23.     }
  24.  
  25.     set secret_b32 ""
  26.     set l [string length $secret]
  27.     set n 0
  28.     set j 0
  29.  
  30.     # encode loop is outlined in RFC 4648 (http://tools.ietf.org/html/rfc4648#page-8)
  31.     for { set i 0 } { $i < $l } { incr i } {
  32.       set n [expr $n << 8]
  33.       set n [expr $n + [scan [string index $secret $i] %c]]
  34.       set j [incr j 8]
  35.  
  36.       while { $j >= 5 } {
  37.         set j [incr j -5]
  38.         append secret_b32 $b32_alphabet_inv([expr ($n & (0x1F << $j)) >> $j])
  39.       }
  40.     }
  41.  
  42.     # pad final input group with zeros to form an integral number of 5-bit groups, then encode
  43.     if { $j > 0 } { append secret_b32 $b32_alphabet_inv([expr $n << (5 - $j) & 0x1F]) }
  44.  
  45.     # if the final quantum is not an integral multiple of 40, append "=" padding
  46.     set pad [expr 8 - [string length $secret_b32] % 8]
  47.     if { ($pad > 0) && ($pad < 8) } { append secret_b32 [string repeat = $pad] }
  48.  
  49.     set ga_qr_code_link "https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/"
  50.     append ga_qr_code_link "$account@$domain"
  51.     append ga_qr_code_link "%3Fsecret%3D"
  52.     append ga_qr_code_link $secret_b32
  53.  
  54.  
  55.     set ga_secret_http_resp {<html>
  56.   <body>
  57.     <div align="center">
  58. }
  59.  
  60.     if { $qr_code eq "yes" } {
  61.       append ga_secret_http_resp "      <img src=\"$ga_qr_code_link\" />\n"
  62.     }
  63.  
  64.     append ga_secret_http_resp "     <p>account: $account@$domain"
  65.     append ga_secret_http_resp "key (secret): $secret_b32</p>\n    </div>\n  </body>\n</html>"
  66.  
  67.     HTTP::respond 200 content $ga_secret_http_resp
  68.   } else {
  69.     HTTP::respond 200 content {<html>
  70.   <body>
  71.     <h2><a href="http://goo.gl/edmb2">Google Authenticator</a> key (shared secret) generator</h2>
  72.     <form action="/ga_secret_generator" method="GET">
  73.       <table cellspacing="1" cellpadding="1" border="0">
  74.         <tr>
  75.           <th align="left">account:</th>
  76.           <td><input name="account" type="text" size="10"> @ <input name="domain" type="text" size="20"></td>
  77.         </tr>
  78.         <tr>
  79.           <th align="left">secret:</th>
  80.           <td><input name="secret" type="text" size="10"> *optional 10 character key (additional chars truncated), random secret used if blank</td>
  81.         </tr>
  82.         <tr>
  83.           <th align="left">generate QR code?</th>
  84.           <td><input name="qr_code" type="checkbox" value="yes"> *a request will be made to Google to generate QR code</td>
  85.         </tr>
  86.       </table>
  87.       <input type="submit" value="Submit">
  88.     </form>
  89.   </body>
  90. </html>}
  91.   }
  92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top