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

broadcast fix #2357

Merged
merged 12 commits into from
Sep 26, 2023
6 changes: 4 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "app",
"version": "2.2.5",
"version": "2.2.9",
"private": true,
"scripts": {
"serve": "npm run locale && vue-cli-service serve",
Expand Down
1 change: 1 addition & 0 deletions src/handlers/Popup/PopupWithBcHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class PopupWithBcHandler extends PopupHandler {
if (successExtraFn) await successExtraFn.call(this, data)
resolve(data)
} catch (error) {
log.error('message error', error)
reject(error)
} finally {
this.bc.close()
Expand Down
5 changes: 3 additions & 2 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '@ethereumjs/util'
import { concatSig, normalize } from '@metamask/eth-sig-util'
import { keccak256 } from '@toruslabs/metadata-helpers'
import { safeatob } from '@toruslabs/openlogin-utils'
import assert from 'assert'
import BigNumber from 'bignumber.js'
import BN from 'bn.js'
Expand Down Expand Up @@ -872,10 +873,10 @@ export const handleRedirectParameters = (hash, queryParameters) => {
let error = ''
if (!queryParameters.preopenInstanceId) {
if (Object.keys(hashParameters).length > 0 && hashParameters.state) {
instanceParameters = JSON.parse(atob(decodeURIComponent(decodeURIComponent(hashParameters.state)))) || {}
instanceParameters = JSON.parse(safeatob(decodeURIComponent(decodeURIComponent(hashParameters.state)))) || {}
error = hashParameters.error_description || hashParameters.error || error
} else if (Object.keys(queryParameters).length > 0 && queryParameters.state) {
instanceParameters = JSON.parse(atob(decodeURIComponent(decodeURIComponent(queryParameters.state)))) || {}
instanceParameters = JSON.parse(safeatob(decodeURIComponent(decodeURIComponent(queryParameters.state)))) || {}
if (queryParameters.error) error = queryParameters.error
}
}
Expand Down
37 changes: 27 additions & 10 deletions src/views/End/End.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

<script>
import { BroadcastChannel } from '@toruslabs/broadcast-channel'
import { safeatob } from '@toruslabs/openlogin-utils'
import { safeatob, storageAvailable } from '@toruslabs/openlogin-utils'
import log from 'loglevel'

import BoxLoader from '../../components/helpers/BoxLoader'
Expand All @@ -71,13 +71,25 @@ export default {
try {
const { hash } = this.$route
const hashUrl = new URL(`${window.location.origin}?${hash.slice(1)}`)
const error = hashUrl.searchParams.get('error')
const error = hashUrl.searchParams.get('error') || ''

if (error) {
if (storageAvailable('localStorage')) {
this.channelId = localStorage.getItem('broadcast_channel_id')
}
this.broadcastData = {
type: POPUP_RESULT,
error,
}
await this.continueToApp()
return
}

const paramSessionNamespace = hashUrl.searchParams.get('sessionNamespace') || ''

const openLoginHandler = await OpenLoginHandler.getInstance({}, {}, paramSessionNamespace)
const { appState } = openLoginHandler.state.userInfo
const parsedAppState = JSON.parse(safeatob(decodeURIComponent(decodeURIComponent(appState || ''))))
const { appState } = openLoginHandler.state.userInfo || {}
const parsedAppState = JSON.parse(safeatob(decodeURIComponent(decodeURIComponent(appState || ''))) || '{}')
this.whiteLabel = parsedAppState.whiteLabel || {}
openLoginHandler.whiteLabel = this.whiteLabel
const loginConfig = parsedAppState.loginConfig || {}
Expand Down Expand Up @@ -136,18 +148,23 @@ export default {
this.loading = true
try {
// move selected key to the first position of keys
const { keys } = this.broadcastData
const id = keys.findIndex((k) => k.ethAddress === this.selectedAccount)
if (id > -1) {
const selectedKey = keys[id]
keys.splice(id, 1)
keys.unshift(selectedKey)
const { keys = [], error } = this.broadcastData
if (!error) {
const id = keys.findIndex((k) => k.ethAddress === this.selectedAccount)
if (id > -1) {
const selectedKey = keys[id]
keys.splice(id, 1)
keys.unshift(selectedKey)
}
}

const bc = new BroadcastChannel(`redirect_openlogin_channel_${this.channelId}`, broadcastChannelOptions)
await bc.postMessage({ data: this.broadcastData })
bc.close()
log.info('posted info')
if (storageAvailable('localStorage')) {
localStorage.removeItem('broadcast_channel_id')
}
// wait for 100ms before closing window
// this is mostly in case of facebook/line logins on mobile devices.
setTimeout(() => {
Expand Down
7 changes: 5 additions & 2 deletions src/views/Start/Start.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</template>

<script>
import { safeatob } from '@toruslabs/openlogin-utils'
import { safeatob, storageAvailable } from '@toruslabs/openlogin-utils'
import log from 'loglevel'

import BoxLoader from '../../components/helpers/BoxLoader'
Expand Down Expand Up @@ -49,7 +49,10 @@ export default {
const { loginProvider, state, mfaLevel, sessionNamespace, ...rest } = this.$route.query
const stateParams = JSON.parse(safeatob(state))
log.info('logging in with', loginProvider, state, rest, mfaLevel)
const { whiteLabel = {}, loginConfig = {}, origin } = stateParams
const { whiteLabel = {}, loginConfig = {}, origin, instanceId } = stateParams
if (storageAvailable('localStorage')) {
localStorage.setItem('broadcast_channel_id', instanceId)
}
this.iframeOrigin = origin
this.isCustomVerifier = Object.keys(loginConfig).length > 0

Expand Down
Loading