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

[refactor] Remove authorize store #4110

Merged
merged 3 commits into from
Feb 12, 2023
Merged
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
66 changes: 0 additions & 66 deletions spec/renderer/integration/store/Login.spec.ts

This file was deleted.

28 changes: 0 additions & 28 deletions spec/renderer/unit/store/Login.spec.ts

This file was deleted.

33 changes: 20 additions & 13 deletions src/main/db/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const insertAccount = (
clientSecret: string,
accessToken: string,
refreshToken: string | null,
server: LocalServer
serverId: number
): Promise<LocalAccount> => {
return new Promise((resolve, reject) => {
db.serialize(() => {
Expand All @@ -34,13 +34,12 @@ export const insertAccount = (
}
const id = this.lastID

db.run('UPDATE servers SET account_id = ? WHERE id = ?', [id, server.id], err => {
db.run('UPDATE servers SET account_id = ? WHERE id = ?', [id, serverId], err => {
if (err) {
reject(err)
}

db.run('COMMIT')

resolve({
id,
username,
Expand Down Expand Up @@ -164,22 +163,30 @@ FROM accounts INNER JOIN servers ON servers.account_id = accounts.id WHERE accou

export const removeAccount = (db: sqlite3.Database, id: number): Promise<null> => {
return new Promise((resolve, reject) => {
db.run('DELETE FROM accounts WHERE id = ?', id, err => {
if (err) {
reject(err)
}
resolve(null)
db.serialize(() => {
db.run('PRAGMA foreign_keys = ON')

db.run('DELETE FROM accounts WHERE id = ?', id, err => {
if (err) {
reject(err)
}
resolve(null)
})
})
})
}

export const removeAllAccounts = (db: sqlite3.Database): Promise<null> => {
return new Promise((resolve, reject) => {
db.run('DELETE FROM accounts', err => {
if (err) {
reject(err)
}
resolve(null)
db.serialize(() => {
db.run('PRAGMA foreign_keys = ON')

db.run('DELETE FROM accounts', err => {
if (err) {
reject(err)
}
resolve(null)
})
})
})
}
Expand Down
14 changes: 9 additions & 5 deletions src/main/db/hashtags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ export const insertTag = (db: sqlite3.Database, accountId: number, tag: string):

export const removeTag = (db: sqlite3.Database, tag: LocalTag): Promise<null> => {
return new Promise((resolve, reject) => {
db.run('DELETE FROM hashtags WHERE id = ?', tag.id, err => {
if (err) {
reject(err)
}
resolve(null)
db.serialize(() => {
db.run('PRAGMA foreign_keys = ON')

db.run('DELETE FROM hashtags WHERE id = ?', tag.id, err => {
if (err) {
reject(err)
}
resolve(null)
})
})
})
}
24 changes: 13 additions & 11 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import path from 'path'
import ContextMenu from 'electron-context-menu'
import { initSplashScreen, Config } from '@trodi/electron-splashscreen'
import openAboutWindow from 'about-window'
import generator, { detector, OAuth, NotificationType, Entity } from 'megalodon'
import generator, { detector, NotificationType, Entity } from 'megalodon'
import AutoLaunch from 'auto-launch'
import minimist from 'minimist'
import sanitizeHtml from 'sanitize-html'
Expand Down Expand Up @@ -463,38 +463,40 @@ ipcMain.handle('add-app', async (_: IpcMainInvokeEvent, url: string) => {
})

type AuthorizeRequest = {
server: LocalServer
appData: OAuth.AppData
serverID: number
baseURL: string
clientID: string
clientSecret: string
code: string
}

ipcMain.handle('authorize', async (_: IpcMainInvokeEvent, req: AuthorizeRequest) => {
const proxy = await proxyConfiguration.forMastodon()
const sns = await detector(req.server.baseURL, proxy)
const client = generator(sns, req.server.baseURL, null, 'Whalebird', proxy)
const tokenData = await client.fetchAccessToken(req.appData.client_id, req.appData.client_secret, req.code, 'urn:ietf:wg:oauth:2.0:oob')
const sns = await detector(req.baseURL, proxy)
const client = generator(sns, req.baseURL, null, 'Whalebird', proxy)
const tokenData = await client.fetchAccessToken(req.clientID, req.clientSecret, req.code, 'urn:ietf:wg:oauth:2.0:oob')
let accessToken = tokenData.access_token
if (sns === 'misskey') {
// In misskey, access token is sha256(userToken + clientSecret)
accessToken = crypto
.createHash('sha256')
.update(tokenData.access_token + req.appData.client_secret, 'utf8')
.update(tokenData.access_token + req.clientSecret, 'utf8')
.digest('hex')
}

const authorizedClient = generator(sns, req.server.baseURL, accessToken, 'Whalebird', proxy)
const authorizedClient = generator(sns, req.baseURL, accessToken, 'Whalebird', proxy)
const credentials = await authorizedClient.verifyAccountCredentials()

const account = await insertAccount(
db,
credentials.data.username,
credentials.data.id,
credentials.data.avatar,
req.appData.client_id,
req.appData.client_secret,
req.clientID,
req.clientSecret,
accessToken,
tokenData.refresh_token,
req.server
req.serverID
)
return account
})
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/components/GlobalHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<FailoverImg :src="`${server.baseURL}/favicon.ico`" :failoverSrc="`${server.baseURL}/favicon.png`" class="instance-icon" />
<span>{{ server.domain }}</span>
</el-menu-item>
<el-menu-item index="/login" :title="$t('global_header.add_new_account')" role="menuitem" class="add-new-account">
<el-menu-item index="/login/form" :title="$t('global_header.add_new_account')" role="menuitem" class="add-new-account">
<font-awesome-icon icon="plus" />
<span>New</span>
</el-menu-item>
Expand Down Expand Up @@ -71,7 +71,7 @@ export default defineComponent({
}
})
.catch(_ => {
return router.push({ path: '/login' })
return router.push({ path: '/login/form' })
})
}

Expand Down
18 changes: 3 additions & 15 deletions src/renderer/components/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,35 @@
</el-row>
</el-header>
<el-container>
<login-form v-if="appData === null" />
<authorize v-else />
<router-view />
</el-container>
</el-container>
</template>

<script lang="ts">
import { defineComponent, computed } from 'vue'
import { defineComponent } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from '@/store'
import { useMagicKeys, whenever } from '@vueuse/core'
import LoginForm from './Login/LoginForm.vue'
import Authorize from './Login/Authorize.vue'
import { ACTION_TYPES } from '@/store/Login'

export default defineComponent({
name: 'login',
components: { LoginForm, Authorize },
setup() {
const space = 'Login'
const store = useStore()
const router = useRouter()
const { escape } = useMagicKeys()

const appData = computed(() => store.state.Login.appData)

whenever(escape, () => {
close()
})

const close = () => {
store.dispatch(`${space}/${ACTION_TYPES.PAGE_BACK}`)
return router.push({
path: '/',
query: { redirect: 'home' }
})
}

return {
close,
appData
close
}
}
})
Expand Down
Loading