-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoauth.js
More file actions
123 lines (96 loc) · 3.13 KB
/
Copy pathoauth.js
File metadata and controls
123 lines (96 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const fs = require('fs').promises
const path = require('path')
const os = require('os')
const sqlite3 = require('sqlite3')
const sqlite = require('sqlite')
const ini = require('ini')
function getProfiles (dir, store) {
return Object.keys(store)
.filter(key => key.startsWith('Profile'))
.map(key => store[key])
.map(profile => ({
name: profile.Name,
path: path.resolve(dir, profile.Path)
}))
}
async function getFirefoxProfilesImpl (dir) {
try {
return getProfiles(dir, ini.parse(await fs.readFile(path.resolve(dir, 'profiles.ini'), 'utf8')))
} catch (err) {
if (err.code !== 'ENOENT') {
throw err
}
}
}
async function getFirefoxProfiles () {
return await getFirefoxProfilesImpl(path.resolve(os.homedir(), '.mozilla/firefox')) ||
getFirefoxProfilesImpl(path.resolve(os.homedir(), 'Library/Application Support/Firefox'))
}
async function fetchLastRedirectImpl (profile) {
const db = await sqlite.open({
filename: `file:${path.resolve(profile.path, 'places.sqlite')}?immutable=1`,
driver: sqlite3.Database,
mode: sqlite3.OPEN_READONLY | sqlite3.OPEN_URI
})
try {
const result = await db.get("SELECT url FROM moz_places WHERE url LIKE 'https://lockbox.firefox.com/fxa/android-redirect.html%' ORDER BY last_visit_date DESC LIMIT 1")
return result?.url
} finally {
await db.close()
}
}
function fetchLastRedirect (profiles) {
return Promise.all(profiles.map(profile => fetchLastRedirectImpl(profile)))
}
async function poll (profiles, initialState) {
while (true) {
await new Promise(resolve => setTimeout(resolve, 1000))
const state = await fetchLastRedirect(profiles)
for (const [i, url] of state.entries()) {
if (initialState[i] !== url) {
console.error('\n')
return url
}
}
process.stderr.write('.')
}
}
async function oauth (options) {
const { sync, verbose } = options
const profiles = await getFirefoxProfiles()
if (!profiles || profiles.length === 0) {
console.error(`Couldn't find Firefox directory.
This method relies on parsing the Firefox history to retreive the OAuth
challenge response and cannot work right now.
Consider using 'ffs auth password' as a fallback.
`)
process.exit(1)
}
if (verbose) {
for (const profile of profiles) {
console.error(`[verbose] profile ${profile.name}`)
}
}
const initialState = await fetchLastRedirect(profiles)
if (verbose) {
for (const [i, url] of initialState.entries()) {
console.error(`[verbose] state [${profiles[i].name}] ${url}`)
}
console.error()
}
const challenge = await sync.auth.oauth.challenge()
console.error(`Visit the following URL in Firefox to sign in:
${challenge.url}
`)
console.error(`We'll detect the OAuth response automatically.
If it hangs, closing Firefox can sometimes help.
Otheriwse, consider using 'ffs auth password' as a fallback.
`)
const url = await poll(profiles, initialState)
if (verbose) {
console.error(`[verbose] detected ${url}`)
}
const result = Object.fromEntries(new URL(url).searchParams)
return sync.auth.oauth.complete(challenge, result)
}
module.exports = oauth