forked from stuartcryan/lastpass-alfred-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Move auth flow to javascript Alfred object (stuartcryan#27)
* Move the auth flow to javascript and out of go.
- Loading branch information
Showing
8 changed files
with
410 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
#!/usr/bin/osascript | ||
ObjC.import('stdlib') | ||
var BW_EXEC = $.getenv('BW_EXEC'); | ||
var PATH = $.getenv('PATH'); | ||
|
||
function run(arg) { | ||
|
||
var app = Application.currentApplication() | ||
app.includeStandardAdditions = true | ||
|
||
var mode = arg[0] | ||
var modeSet = "unlock" | ||
var email = arg[1] | ||
if (mode.localeCompare(modeSet) == 0) { | ||
var text = `Unlock Bitwarden for user ${email}.\nPlease enter your password:` | ||
var response = app.displayDialog(text, { | ||
defaultAnswer: "", | ||
withIcon: "caution", | ||
buttons: ["Cancel", "OK"], | ||
defaultButton: "OK", | ||
cancelButton: "Cancel", | ||
givingUpAfter: 120, | ||
hiddenAnswer: "true" | ||
}) | ||
|
||
var password = response.textReturned | ||
return unlock(password) | ||
} else { | ||
var totpInt = arg[2] | ||
var totpStr = arg[3] | ||
|
||
var text = `Login to Bitwarden for user ${email}.\nPlease enter your password:` | ||
var response = app.displayDialog(text, { | ||
defaultAnswer: "", | ||
withIcon: "caution", | ||
buttons: ["Cancel", "OK"], | ||
defaultButton: "OK", | ||
cancelButton: "Cancel", | ||
givingUpAfter: 120, | ||
hiddenAnswer: "true" | ||
}) | ||
var password = response.textReturned | ||
|
||
if (totpStr.localeCompare(" ") != 0) { | ||
if (totpInt.localeCompare("1") != 0) { | ||
var text = `2FA authentication for Bitwarden user ${email}.\nPlease enter your 2FA code for ${totpStr}:` | ||
var response = app.displayDialog(text, { | ||
defaultAnswer: "", | ||
withIcon: "caution", | ||
buttons: ["Cancel", "OK"], | ||
defaultButton: "OK", | ||
cancelButton: "Cancel", | ||
givingUpAfter: 120, | ||
hiddenAnswer: "false" | ||
}) | ||
|
||
var totpCode = response.textReturned | ||
return login(email, password, totpCode, totpInt) | ||
} else { | ||
return login(email, password, "", totpInt) | ||
} | ||
} else { | ||
return login(email, password, "", "") | ||
} | ||
} | ||
} | ||
|
||
function login(email, password, totp, totpMode) { | ||
var app = Application.currentApplication() | ||
app.includeStandardAdditions = true | ||
|
||
if (totpMode.localeCompare("1") == 0) { | ||
var cmd = `PATH=${PATH}; ${BW_EXEC} login ${email} --method ${totpMode} "${password}"` | ||
try{ | ||
var result = app.doShellScript(cmd); | ||
}catch(e) { | ||
var res = e.toString() | ||
res = res.includes("Two-step login code") | ||
if (!res) { | ||
console.log(e.toString()) | ||
return e.toString() | ||
} | ||
} | ||
var text = `Email authentication for Bitwarden user ${email}.\nPlease enter your 2FA code sent via email:` | ||
var response = app.displayDialog(text, { | ||
defaultAnswer: "", | ||
withIcon: "caution", | ||
buttons: ["Cancel", "OK"], | ||
defaultButton: "OK", | ||
cancelButton: "Cancel", | ||
givingUpAfter: 120, | ||
hiddenAnswer: "false" | ||
}) | ||
var totpCode = response.textReturned | ||
|
||
var cmd = `PATH=${PATH}; ${BW_EXEC} login ${email} --method ${totpMode} --code ${totpCode} "${password}" --raw` | ||
try { | ||
var result = app.doShellScript(cmd); | ||
}catch(e) { | ||
console.log(e.toString()) | ||
return e.toString() | ||
} | ||
} else { | ||
if (totp.localeCompare("") != 0) { | ||
var cmd = `PATH=${PATH}; ${BW_EXEC} login ${email} --method ${totpMode} --code ${totp} "${password}" --raw` | ||
} else { | ||
var cmd = `PATH=${PATH}; ${BW_EXEC} login ${email} "${password}" --raw` | ||
} | ||
try { | ||
var result = app.doShellScript(cmd); | ||
}catch(e) { | ||
console.log(e.toString()) | ||
return e.toString() | ||
} | ||
} | ||
var res = setToken(result) | ||
if (res.localeCompare("") == 0) { | ||
return "Logged in." | ||
} | ||
} | ||
|
||
function unlock(password) { | ||
var app = Application.currentApplication() | ||
app.includeStandardAdditions = true | ||
var cmd = `PATH=${PATH}; ${BW_EXEC} unlock "${password}" --raw` | ||
try { | ||
var result = app.doShellScript(cmd); | ||
var res = setToken(result) | ||
if (res.localeCompare("") == 0) { | ||
return "Unlocked" | ||
} | ||
}catch(e) { | ||
console.log(e) | ||
return e.toString() | ||
} | ||
} | ||
|
||
function setToken(password) { | ||
var bundleId = "com.lisowski-development.alfred.bitwarden" | ||
var app = Application.currentApplication() | ||
app.includeStandardAdditions = true | ||
|
||
var cmd = `PATH=${PATH}; /usr/bin/security add-generic-password -s ${bundleId} -a token -w ${password} -U` | ||
try { | ||
app.doShellScript(cmd); | ||
return "" | ||
}catch(e) { | ||
console.log(e.toString()) | ||
return e.toString() | ||
} | ||
} |
Oops, something went wrong.