Support redirect to url scheme for authorize

This commit is contained in:
kPherox 2019-04-29 23:56:30 +09:00
parent a2cac44a81
commit 51826ecaab
No known key found for this signature in database
GPG Key ID: C04751C2BFA2F62D

View File

@ -17,20 +17,26 @@ interface AccountDoc {
} }
export default class Auth { export default class Auth {
private static redirectUri: string = 'thedesk://login'
public static ready() { public static ready() {
ipcMain.on("new-account-setup", (event: Event, instance: string) => this.setup(event, instance)) ipcMain.on("new-account-setup", (event: Event, instance: string, useURLScheme: boolean = false) => this.setup(event, instance, useURLScheme))
} }
private static async setup(event: Event, instance: string) { private static async setup(event: Event, instance: string, useURLScheme: boolean) {
let appData: OAuth.AppData let appData: OAuth.AppData
try { try {
const SCOPES: string = "read write follow" let options: Partial<{ scopes: string, redirect_uris: string, website: string }> = {
scopes: "read write follow",
website: "https://thedesk.top",
}
if (useURLScheme) {
options.redirect_uris = this.redirectUri
}
appData = await Mastodon.registerApp( appData = await Mastodon.registerApp(
"TheDesk", "TheDesk",
{ options,
scopes: SCOPES
},
"https://" + instance "https://" + instance
) )
} catch (err) { } catch (err) {
@ -63,14 +69,15 @@ export default class Auth {
) )
ipcMain.once("new-account-auth", (event: Event, code: string, instance: string) => { ipcMain.once("new-account-auth", (event: Event, code: string, instance: string) => {
this.auth(event, code, instance, appData.clientId, appData.clientSecret) let redirectUri = useURLScheme ? this.redirectUri : undefined
this.auth(event, code, instance, appData.clientId, appData.clientSecret, redirectUri)
}) })
} }
private static async auth(event: Event, code: string, instance: string, clientId: string, clientSecret: string) { private static async auth(event: Event, code: string, instance: string, clientId: string, clientSecret: string, redirectUri?: string) {
let tokenData: Partial<{ accessToken: string }> let tokenData: Partial<{ accessToken: string }>
try { try {
tokenData = await Mastodon.fetchAccessToken(clientId, clientSecret, code, "https://" + instance) tokenData = await Mastodon.fetchAccessToken(clientId, clientSecret, code, "https://" + instance, redirectUri)
} catch (err) { } catch (err) {
let error: Error = err let error: Error = err
event.sender.send(`error`, { event.sender.send(`error`, {