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

Authentication fixes #92

Merged
merged 2 commits into from
Oct 20, 2021
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
5 changes: 5 additions & 0 deletions changelog/unreleased/bugfix-authentication-fixes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Bugfix: Authentication fixes

We've fixed a bug causing the file-picker becoming stuck on the loading spinner when the access token is already expired.

https://github.com/owncloud/file-picker/pull/92
28 changes: 22 additions & 6 deletions src/services/auth.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Log, User, UserManager, InMemoryWebStorage, WebStorageStateStore } from 'oidc-client'

const AUTH_STORAGE_PREFIX = 'oc_oAuth'

export function initVueAuthenticate(config) {
if (config) {
const storage = config.storage === 'memory' ? new InMemoryWebStorage() : sessionStorage
const storage = config.storage === 'memory' ? new InMemoryWebStorage() : localStorage
const store = new WebStorageStateStore({
prefix: 'oc_oAuth',
prefix: AUTH_STORAGE_PREFIX,
store: storage
})

Expand Down Expand Up @@ -67,27 +69,41 @@ export function initVueAuthenticate(config) {
console.log('UserSignedOut:', arguments)
})

mgr.events.addSilentRenewError(() => {
mgr.clearStaleState(store, 0)
})

return {
authenticate() {
mgr.clearStaleState(store, 0)
return mgr.signinPopup()
},
getToken() {
const storageString = storage.getItem('oc_oAuth' + mgr._userStoreKey)
const storageString = storage.getItem(AUTH_STORAGE_PREFIX + mgr._userStoreKey)

if (storageString) {
const user = User.fromStorageString(storageString)

if (user) {
mgr.events.load(user, false)

return user.access_token
if (user.expired) {
mgr.signinSilent().then((_, reject) => {
if (reject) {
mgr.clearStaleState(store, 0)
return null
}
return user.access_token
})
} else {
return user.access_token
}
Comment on lines +90 to +99
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, let me know if you want me to switch this to use await.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be nice 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep in mind this would require a couple more functions to become async, including initApp, are you sure the added complexity is worth it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably something for a different PR 😅

}
}

return null
},
getStoredUserObject() {
const storageString = storage.getItem('oc_oAuth' + mgr._userStoreKey)
const storageString = storage.getItem(AUTH_STORAGE_PREFIX + mgr._userStoreKey)

if (storageString) {
const user = User.fromStorageString(storageString)
Expand Down