Permalink
Browse files

Add btoa implementation for tests. Closes #135.

  • Loading branch information...
1 parent 598e62b commit a48b48001a0dc3839fb47432895ecc9b034b2dad @nickstenning nickstenning committed May 11, 2012
Showing with 43 additions and 4 deletions.
  1. +43 −4 test/spec/plugin/auth_spec.coffee
@@ -1,11 +1,51 @@
Date::toISO8601String = DateToISO8601String
+B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
+
+base64Encode = (data) ->
+ if btoa?
+ # Gecko and Webkit provide native code for this
+ btoa(data)
+ else
+ # Adapted from MIT/BSD licensed code at http://phpjs.org/functions/base64_encode
+ # version 1109.2015
+ i = 0
+ ac = 0
+ enc = ""
+ tmp_arr = []
+
+ if not data
+ return data
+
+ data += ''
+
+ while i < data.length
+ # pack three octets into four hexets
+ o1 = data.charCodeAt(i++)
+ o2 = data.charCodeAt(i++)
+ o3 = data.charCodeAt(i++)
+
+ bits = o1 << 16 | o2 << 8 | o3
+
+ h1 = bits >> 18 & 0x3f
+ h2 = bits >> 12 & 0x3f
+ h3 = bits >> 6 & 0x3f
+ h4 = bits & 0x3f
+
+ # use hexets to index into b64, and append result to encoded string
+ tmp_arr[ac++] = B64.charAt(h1) + B64.charAt(h2) + B64.charAt(h3) + B64.charAt(h4)
+
+ enc = tmp_arr.join('')
+
+ r = data.length % 3
+ return (if r then enc.slice(0, r - 3) else enc) + '==='.slice(r or 3)
+
base64UrlEncode = (data) ->
- data = btoa(data)
+ data = base64Encode(data)
+ chop = data.indexOf('=')
+ data = data[...chop] if chop isnt -1
data = data.replace(/\+/g, '-')
data = data.replace(/\//g, '_')
- while data[-1..] is '='
- data = data[..-2]
data
makeToken = () ->
@@ -20,7 +60,6 @@ makeToken = () ->
encodedToken: 'header.' + base64UrlEncode(JSON.stringify(rawToken)) + '.signature'
}
-
describe 'Annotator.Plugin.Auth', ->
mock = null
rawToken = null

0 comments on commit a48b480

Please sign in to comment.