Skip to content

Commit 0f8afe2

Browse files
committed
Manually UTF-8 then B64 encode credentials.
btoa only supports 8-bit characters.
1 parent 7e20721 commit 0f8afe2

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"react-scripts": "0.9.5"
77
},
88
"dependencies": {
9+
"base64-js": "^1.2.1",
910
"jwt-decode": "^2.2.0",
1011
"react": "^15.5.4",
1112
"react-dom": "^15.5.4",
@@ -14,6 +15,7 @@
1415
"redux": "^3.6.0",
1516
"redux-form": "^6.6.3",
1617
"redux-thunk": "^2.2.0",
18+
"text-encoding": "^0.6.4",
1719
"validator": "^7.0.0"
1820
},
1921
"scripts": {

src/actions/auth.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import jwtDecode from 'jwt-decode';
22
import {SubmissionError} from 'redux-form';
3+
import {fromByteArray} from 'base64-js';
4+
import {TextEncoder} from 'text-encoding';
35

46
import {API_BASE_URL} from '../config';
57
import {normalizeResponseErrors} from './utils';
@@ -17,6 +19,13 @@ export const setCurrentUser = currentUser => ({
1719
currentUser
1820
});
1921

22+
// Encode a JS string as Base-64 encoded UTF-8
23+
const base64EncodingUTF8 = str => {
24+
const encoded = new TextEncoder('utf-8').encode(str);
25+
const b64Encoded = fromByteArray(encoded);
26+
return b64Encoded;
27+
};
28+
2029
// Stores the auth token in state and localStorage, and decodes and stores
2130
// the user data stored in the token
2231
const storeAuthInfo = (authToken, dispatch) => {
@@ -29,7 +38,7 @@ const storeAuthInfo = (authToken, dispatch) => {
2938
export const login = (username, password) => dispatch => {
3039
// Base64 encode the string username:password, used in the basic
3140
// auth field
32-
const token = btoa(`${username}:${password}`);
41+
const token = base64EncodingUTF8(`${username}:${password}`);
3342
return (
3443
fetch(`${API_BASE_URL}/auth/login`, {
3544
method: 'POST',

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,10 @@ base64-js@^1.0.2:
889889
version "1.2.0"
890890
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.2.0.tgz#a39992d723584811982be5e290bb6a53d86700f1"
891891

892+
base64-js@^1.2.1:
893+
version "1.2.1"
894+
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.2.1.tgz#a91947da1f4a516ea38e5b4ec0ec3773675e0886"
895+
892896
893897
version "0.5.3"
894898
resolved "https://registry.yarnpkg.com/batch/-/batch-0.5.3.tgz#3f3414f380321743bfc1042f9a83ff1d5824d464"
@@ -4958,6 +4962,10 @@ test-exclude@^3.3.0:
49584962
read-pkg-up "^1.0.1"
49594963
require-main-filename "^1.0.1"
49604964

4965+
text-encoding@^0.6.4:
4966+
version "0.6.4"
4967+
resolved "https://registry.yarnpkg.com/text-encoding/-/text-encoding-0.6.4.tgz#e399a982257a276dae428bb92845cb71bdc26d19"
4968+
49614969
text-table@~0.2.0:
49624970
version "0.2.0"
49634971
resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4"

0 commit comments

Comments
 (0)