Skip to content

Commit b2de450

Browse files
authored
Merge pull request #434 from Authenticator-Extension/onedrive
OneDrive Support
2 parents f259ab6 + 2200f18 commit b2de450

File tree

16 files changed

+483
-63
lines changed

16 files changed

+483
-63
lines changed

manifest-chrome.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@
5656
"https://www.google.com/",
5757
"https://*.dropboxapi.com/*",
5858
"https://www.googleapis.com/*",
59-
"https://accounts.google.com/o/oauth2/revoke"
59+
"https://accounts.google.com/o/oauth2/revoke",
60+
"https://graph.microsoft.com/me/*",
61+
"https://login.microsoftonline.com/common/oauth2/v2.0/token"
6062
],
6163
"offline_enabled": true,
62-
"content_security_policy": "script-src 'self'; font-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; connect-src https://www.google.com/ https://*.dropboxapi.com https://www.googleapis.com/ https://accounts.google.com/o/oauth2/revoke ; default-src 'none'"
64+
"content_security_policy": "script-src 'self'; font-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; connect-src https://www.google.com/ https://*.dropboxapi.com https://www.googleapis.com/ https://accounts.google.com/o/oauth2/revoke https://login.microsoftonline.com/common/oauth2/v2.0/token https://graph.microsoft.com/; default-src 'none'"
6365
}

manifest-firefox.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@
4848
"https://www.google.com/",
4949
"https://*.dropboxapi.com/*",
5050
"https://www.googleapis.com/*",
51-
"https://accounts.google.com/o/oauth2/revoke"
51+
"https://accounts.google.com/o/oauth2/revoke",
52+
"https://graph.microsoft.com/me/*",
53+
"https://login.microsoftonline.com/common/oauth2/v2.0/token"
5254
],
53-
"content_security_policy": "script-src 'self'; font-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; connect-src https://www.google.com/ https://*.dropboxapi.com https://www.googleapis.com/ https://accounts.google.com/o/oauth2/revoke ; default-src 'none'"
55+
"content_security_policy": "script-src 'self'; font-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; connect-src https://www.google.com/ https://*.dropboxapi.com https://www.googleapis.com/ https://accounts.google.com/o/oauth2/revoke https://login.microsoftonline.com/common/oauth2/v2.0/token https://graph.microsoft.com/; default-src 'none'"
5456
}

package-lock.json

Lines changed: 11 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
PLATFORM=$1
1010
REMOTE=$(git config --get remote.origin.url)
1111
CREDS=$(cat ./src/models/credentials.ts | tr -d '\n')
12-
CREDREGEX='^.*".+".*".+".*".+".*$'
12+
CREDREGEX='^.*".+".*".+".*".+".*".+".*".+".*$'
1313
STYLEFILES="./src/* ./src/**/* ./src/**/**/* ./sass/*.scss"
1414
set -e
1515

scripts/credentials.ts.enc

211 Bytes
Binary file not shown.

src/background.ts

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import QRCode from "qrcode-reader";
44
import { getCredentials } from "./models/credentials";
55
import { Encryption } from "./models/encryption";
66
import { EntryStorage, ManagedStorage } from "./models/storage";
7-
import { Dropbox, Drive } from "./models/backup";
7+
import { Dropbox, Drive, OneDrive } from "./models/backup";
88
import * as uuid from "uuid/v4";
99

1010
let cachedPassphrase = "";
@@ -30,7 +30,7 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
3030
setAutolock();
3131
} else if (message.action === "passphrase") {
3232
sendResponse(cachedPassphrase);
33-
} else if (["dropbox", "drive"].indexOf(message.action) > -1) {
33+
} else if (["dropbox", "drive", "onedrive"].indexOf(message.action) > -1) {
3434
getBackupToken(message.action);
3535
} else if (message.action === "lock") {
3636
cachedPassphrase = "";
@@ -270,6 +270,11 @@ function getBackupToken(service: string) {
270270
getCredentials().drive.client_id +
271271
"&scope=https%3A//www.googleapis.com/auth/drive.file&prompt=consent&redirect_uri=" +
272272
redirUrl;
273+
} else if (service === "onedrive") {
274+
redirUrl = encodeURIComponent(chrome.identity.getRedirectURL());
275+
authUrl = `https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=${
276+
getCredentials().onedrive.client_id
277+
}&response_type=code&redirect_uri=${redirUrl}&scope=https%3A%2F%2Fgraph.microsoft.com%2FFiles.ReadWrite.AppFolder%20https%3A%2F%2Fgraph.microsoft.com%2FUser.Read%20offline_access&response_mode=query&prompt=consent`;
273278
}
274279
chrome.identity.launchWebAuthFlow(
275280
{ url: authUrl, interactive: true },
@@ -280,7 +285,10 @@ function getBackupToken(service: string) {
280285
let hashMatches = url.split("#");
281286
if (service === "drive") {
282287
hashMatches = url.slice(0, -1).split("?");
288+
} else if (service === "onedrive") {
289+
hashMatches = url.split("?");
283290
}
291+
284292
if (hashMatches.length < 2) {
285293
return;
286294
}
@@ -353,6 +361,54 @@ function getBackupToken(service: string) {
353361
}
354362
);
355363
uploadBackup("drive");
364+
} else if (service === "onedrive") {
365+
const xhr = new XMLHttpRequest();
366+
// Need to trade code we got from launchWebAuthFlow for a
367+
// token & refresh token
368+
await new Promise(
369+
(
370+
resolve: (value: boolean) => void,
371+
reject: (reason: Error) => void
372+
) => {
373+
xhr.open(
374+
"POST",
375+
"https://login.microsoftonline.com/common/oauth2/v2.0/token"
376+
);
377+
xhr.setRequestHeader("Accept", "application/json");
378+
xhr.setRequestHeader(
379+
"Content-Type",
380+
"application/x-www-form-urlencoded"
381+
);
382+
xhr.onreadystatechange = () => {
383+
if (xhr.readyState === 4) {
384+
try {
385+
const res = JSON.parse(xhr.responseText);
386+
if (res.error) {
387+
console.error(res.error_description);
388+
resolve(false);
389+
} else {
390+
localStorage.oneDriveToken = res.access_token;
391+
localStorage.oneDriveRefreshToken =
392+
res.refresh_token;
393+
resolve(true);
394+
}
395+
} catch (error) {
396+
console.error(error);
397+
reject(error);
398+
}
399+
}
400+
return;
401+
};
402+
xhr.send(
403+
`client_id=${
404+
getCredentials().onedrive.client_id
405+
}&grant_type=authorization_code&scope=https%3A%2F%2Fgraph.microsoft.com%2FFiles.ReadWrite.AppFolder%20https%3A%2F%2Fgraph.microsoft.com%2FUser.Read%20offline_access&code=${value}&redirect_uri=${redirUrl}&client_secret=${encodeURIComponent(
406+
getCredentials().onedrive.client_secret
407+
)}`
408+
);
409+
}
410+
);
411+
uploadBackup("onedrive");
356412
}
357413
}
358414
}
@@ -377,6 +433,11 @@ async function uploadBackup(service: string) {
377433
await drive.upload(encryption);
378434
break;
379435

436+
case "onedrive":
437+
const onedrive = new OneDrive();
438+
await onedrive.upload(encryption);
439+
break;
440+
380441
default:
381442
break;
382443
}

src/components/Popup/MainHeader.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
v-bind:style="{
2727
left: encryption.getEncryptionStatus() ? '70px' : '45px'
2828
}"
29-
v-show="(dropboxToken || driveToken) && !style.isEditing"
29+
v-show="
30+
(dropboxToken || driveToken || oneDriveToken) && !style.isEditing
31+
"
3032
>
3133
<IconSync />
3234
</div>
@@ -76,7 +78,7 @@ import IconCheck from "../../../svg/check.svg";
7678
const computedPrototype = [
7779
mapState("style", ["style"]),
7880
mapState("accounts", ["encryption"]),
79-
mapState("backup", ["driveToken", "dropboxToken"])
81+
mapState("backup", ["driveToken", "dropboxToken", "oneDriveToken"])
8082
];
8183
8284
let computed = {};

0 commit comments

Comments
 (0)