Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oauth state should be base64 #511

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"uuid-validate": "0.0.3"
},
"devDependencies": {
"base64-js": "^1.5.1",
"css-loader": "^5.2.6",
"favicons-webpack-plugin": "^5.0.2",
"file-loader": "^6.2.0",
Expand Down
8 changes: 7 additions & 1 deletion src/oauth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import $ from 'jquery';
import QRious from 'qrious';
import URLUtils from '../utils/url.js';
import MixinUtils from '../utils/mixin.js';
import base64 from '../utils/base64.js';

function OAuth(router, api) {
this.router = router;
Expand All @@ -18,7 +19,12 @@ OAuth.prototype = {
let scope = params.scope || "";
scope = scope.replace(/\+/g, ' ');
const codeChallenge = params.code_challenge || "";
const state = params.state || "";
let state = params.state || "";
const stateBase64 = base64.encode(base64.decode(state));
// TODO deprecated
if (state === stateBase64) {
state = stateBase64;
};
const returnTo = params.return_to || "";
self.api.authorization.connect(function (resp) {
if (resp.error) {
Expand Down
48 changes: 48 additions & 0 deletions src/utils/base64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import base64 from 'base64-js';

function padding(str) {
var mod = str.length % 4;
var pad = 4 - mod;

if (mod === 0) {
return str;
}

return str + new Array(1 + pad).join('=');
}

function stringToByteArray(str) {
var arr = new Array(str.length);
for (var a = 0; a < str.length; a++) {
arr[a] = str.charCodeAt(a);
}
return arr;
}

function byteArrayToString(array) {
var result = '';
for (var i = 0; i < array.length; i++) {
result += String.fromCharCode(array[i]);
}
return result;
}

function encode(str) {
return base64
.fromByteArray(stringToByteArray(str))
.replace(/\+/g, '-') // Convert '+' to '-'
.replace(/\//g, '_'); // Convert '/' to '_'
}

function decode(str) {
str = padding(str)
.replace(/-/g, '+') // Convert '-' to '+'
.replace(/_/g, '/'); // Convert '_' to '/'

return byteArrayToString(base64.toByteArray(str));
}

export default {
encode: encode,
decode: decode
};