Skip to content

Commit

Permalink
Merge pull request #2205 from owncloud/bugfix/openid-refresh-flow
Browse files Browse the repository at this point in the history
Prevent duplicate token refresh calls
  • Loading branch information
Lukas Hirt authored Oct 10, 2019
2 parents 7c60fd2 + 7dba197 commit 0f98f59
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
9 changes: 0 additions & 9 deletions src/services/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,6 @@ export function initVueAuthenticate (config) {
Log.logger = console
Log.level = openIdConfig.logLevel

mgr.events.addUserLoaded(function (user) {
console.log('New User Loaded:', arguments)
console.log('Access_token: ', user.access_token)
})

mgr.events.addSilentRenewError(function () {
console.error('Silent Renew Error:', arguments)
})

mgr.events.addUserSignedOut(function () {
console.log('UserSignedOut:', arguments)
})
Expand Down
44 changes: 24 additions & 20 deletions src/store/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const state = {

const actions = {
cleanUpLoginState (context) {
if (context.state.id === '') {
return
}
// reset user to default state
context.commit('SET_USER', state)
// reset capabilities to default state
Expand All @@ -37,10 +40,6 @@ const actions = {
})
},
initAuth (context, payload = { autoRedirect: false }) {
// if called from login, use available vue-authenticate instance; else re-init
if (!vueAuthInstance) vueAuthInstance = initVueAuthenticate(context.rootState.config)
const token = vueAuthInstance.getToken()

function init (client, token) {
const instance = context.rootState.config.server || window.location.origin
const options = {
Expand Down Expand Up @@ -87,28 +86,33 @@ const actions = {
})
}

const tokenRefresh = function (client) {
vueAuthInstance.mgr.signinSilent().then(user => {
console.log('token refreshed…')
init(client, user.access_token)
}).catch(error => {
console.warn('token refresh failed ' + error)
context.dispatch('cleanUpLoginState')
router.push({ name: 'accessDenied' })
})
}

if (token) {
// if called from login, use available vue-authenticate instance; else re-init
if (!vueAuthInstance) {
vueAuthInstance = initVueAuthenticate(context.rootState.config)
const client = this._vm.$client
vueAuthInstance.events().addAccessTokenExpired(function () {
console.log('store/user.js - AccessToken Expired:', arguments)
tokenRefresh(client)
console.log('AccessToken Expired:', arguments)
})
vueAuthInstance.mgr.events.addAccessTokenExpiring(function () {
console.log('AccessToken Expiring:', arguments)
tokenRefresh(client)
})

vueAuthInstance.events().addUserLoaded(user => {
console.log(`New User Loaded. access_token: ${user.access_token}, refresh_token: ${user.refresh_token}`)
init(client, user.access_token)
})
vueAuthInstance.events().addUserUnloaded(() => {
console.log('user unloaded…')
context.dispatch('cleanUpLoginState')
router.push({ name: 'accessDenied' })
})
vueAuthInstance.events().addSilentRenewError(error => {
console.error('Silent Renew Error:', error)
context.dispatch('cleanUpLoginState')
router.push({ name: 'accessDenied' })
})
}
const token = vueAuthInstance.getToken()
if (token) {
init(this._vm.$client, token)
}
},
Expand Down

0 comments on commit 0f98f59

Please sign in to comment.