Add support for identity-aware proxy login

This commit is contained in:
branchmispredictor 2021-05-18 13:27:43 -04:00
parent 0127ccf395
commit 8b68f453c5
2 changed files with 39 additions and 28 deletions

View File

@ -98,6 +98,10 @@ export default {
return
}
if (!localStorage.getItem('token')) {
return
}
const expiresIn = (this.userInfo !== null ? this.userInfo.exp : 0) - +new Date() / 1000
// If the token expiry is negative, it is already expired and we have no choice but to redirect

View File

@ -1,6 +1,7 @@
import {HTTPFactory} from '@/http-common'
import {ERROR_MESSAGE, LOADING} from '../mutation-types'
import UserModel from '../../models/user'
import authTypes from '../../models/authTypes'
export default {
namespaced: true,
@ -173,47 +174,49 @@ export default {
return Promise.resolve()
}
const jwt = localStorage.getItem('token')
let authenticated = false
let info = null
if (jwt) {
const base64 = jwt
.split('.')[1]
.replace('-', '+')
.replace('_', '/')
const info = new UserModel(JSON.parse(window.atob(base64)))
info = new UserModel(JSON.parse(window.atob(base64)))
const ts = Math.round((new Date()).getTime() / 1000)
authenticated = info.exp >= ts
ctx.commit('info', info)
if (authenticated) {
const HTTP = HTTPFactory()
// We're not returning the promise here to prevent blocking the initial ui render if the user is
// accessing the site with a token in local storage
HTTP.get('user', {
headers: {
Authorization: `Bearer ${jwt}`,
},
})
.then(r => {
const info = new UserModel(r.data)
info.type = ctx.state.info.type
info.email = ctx.state.info.email
info.exp = ctx.state.info.exp
ctx.commit('info', info)
ctx.commit('authenticated', authenticated)
ctx.commit('lastUserRefresh')
})
.catch(e => {
console.error('Error while refreshing user info:', e)
})
}
}
ctx.commit('authenticated', authenticated)
if (!authenticated) {
ctx.commit('info', null)
info = null;
}
ctx.commit('authenticated', authenticated)
ctx.commit('info', info)
// Always call the user endpoint, as there may be external auth
// even if a jwt is not present
const HTTP = HTTPFactory()
HTTP.get('user', {
headers: {
Authorization: `Bearer ${jwt}`,
},
})
.then(r => {
const info = new UserModel(r.data)
// Externally authed users might not have any of these set from a JWT,
// so default to sane values
info.type = ctx.state.info ? ctx.state.info.type : authTypes.USER
info.email = ctx.state.info ? ctx.state.info.email : null
info.exp = ctx.state.info ? ctx.state.info.exp : 0
ctx.commit('info', info)
ctx.commit('authenticated', true)
ctx.commit('lastUserRefresh')
})
.catch(e => {
console.error('Error while refreshing user info:', e)
})
return Promise.resolve()
},
@ -224,6 +227,10 @@ export default {
return
}
if (!localStorage.getItem('token')) {
return
}
HTTP.post('user/token', null, {
headers: {
Authorization: 'Bearer ' + localStorage.getItem('token'),