Controllers
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import { createStore, createLogger } from 'vuex'
|
||||
import auth from './modules/auth'
|
||||
import msalAuth from './modules/msalAuth'
|
||||
|
||||
const debug = process.env.NODE_ENV !== 'production'
|
||||
|
||||
export default createStore({
|
||||
modules: {
|
||||
auth,
|
||||
msalAuth,
|
||||
},
|
||||
strict: debug,
|
||||
plugins: debug ? [createLogger()] : [],
|
||||
|
@@ -1,153 +0,0 @@
|
||||
import Cookies from 'cookies-js'
|
||||
import jwt_decode from 'jwt-decode'
|
||||
import axios from 'axios'
|
||||
|
||||
const TokenCookieName = 'ktusaktutoken'
|
||||
const ClientIdCookieName = 'ktusakacas'
|
||||
const AuthorityCookieName = 'ktusakeksas'
|
||||
const TenantCookieName = 'ktusalaimis'
|
||||
const NonceCookieName = 'ktusakumpikas'
|
||||
|
||||
const Scope = 'openid email'
|
||||
|
||||
// initial state
|
||||
const state = () => ({
|
||||
token: null,
|
||||
tokenData: null,
|
||||
clientId: null, // 5931fda0-e9e0-4754-80c2-18bcb9d9561a
|
||||
authority: null, // https://login.microsoftonline.com/3415f2f7-f5a8-4092-b52a-003aaf844853/v2.0
|
||||
tenant: null, // 3415f2f7-f5a8-4092-b52a-003aaf844853
|
||||
})
|
||||
|
||||
const callbackUrl =
|
||||
window.location.protocol + '//' + window.location.host + '/oidc'
|
||||
|
||||
// getters
|
||||
const getters = {
|
||||
isReady(state) {
|
||||
if (
|
||||
state.clientId == null ||
|
||||
state.authority == null ||
|
||||
state.tenant == null
|
||||
)
|
||||
return false
|
||||
return true
|
||||
},
|
||||
isValid(state, getters) {
|
||||
if (!getters.isReady) return false
|
||||
if (state.token == null || state.tokenData == null) return false
|
||||
const d = state.tokenData
|
||||
if (d.nonce !== state.nonce) return false
|
||||
if (d.iss !== state.authority) return false
|
||||
|
||||
if (d.aud !== state.clientId) return false
|
||||
const now = new Date()
|
||||
const exp = new Date(d.exp * 1000)
|
||||
if (now > exp) return false
|
||||
return true
|
||||
},
|
||||
email(state, getters) {
|
||||
if (!getters.isValid) return null
|
||||
return state.tokenData.email
|
||||
},
|
||||
userId(state, getters) {
|
||||
if (!getters.isValid) return null
|
||||
return state.tokenData.sub
|
||||
},
|
||||
isExpiringSoon(state, getters) {
|
||||
if (!getters.isValid) return false
|
||||
return true
|
||||
},
|
||||
expires(state, getters) {
|
||||
if (!getters.isValid) return 0
|
||||
return new Date(state.tokenData.exp * 1000)
|
||||
},
|
||||
loginUrl(state, getters) {
|
||||
if (!getters.isReady) return null
|
||||
return `https://login.microsoftonline.com/${
|
||||
state.tenant
|
||||
}/oauth2/v2.0/authorize?client_id=${
|
||||
state.clientId
|
||||
}&redirect_uri=${encodeURIComponent(
|
||||
callbackUrl
|
||||
)}&response_type=id_token&scope=${Scope}&nonce=${state.nonce}`
|
||||
},
|
||||
}
|
||||
|
||||
// actions
|
||||
const actions = {
|
||||
async initialize({ commit }) {
|
||||
const token = Cookies.get(TokenCookieName)
|
||||
const primaryClientId = Cookies.get(ClientIdCookieName)
|
||||
const primaryAuthority = Cookies.get(AuthorityCookieName)
|
||||
const primaryTenant = Cookies.get(TenantCookieName)
|
||||
const nonce = Cookies.get(NonceCookieName)
|
||||
if (!nonce) {
|
||||
const newNonce =
|
||||
Date.now().toString(36) +
|
||||
Math.random()
|
||||
.toString(36)
|
||||
.substring(2)
|
||||
Cookies.set(NonceCookieName, newNonce)
|
||||
commit('setNonce', newNonce)
|
||||
} else {
|
||||
commit('setNonce', nonce)
|
||||
}
|
||||
commit('setToken', token)
|
||||
commit('computeTokenVars')
|
||||
commit('setMetadata', [primaryClientId, primaryAuthority, primaryTenant])
|
||||
axios
|
||||
.get('/api/AuthMetadata')
|
||||
.then(response => {
|
||||
Cookies.set(ClientIdCookieName, response.data.clientId)
|
||||
Cookies.set(AuthorityCookieName, response.data.authority)
|
||||
Cookies.set(TenantCookieName, response.data.tenant)
|
||||
commit('setMetadata', [
|
||||
response.data.clientId,
|
||||
response.data.authority,
|
||||
response.data.tenant,
|
||||
])
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(error)
|
||||
})
|
||||
},
|
||||
async setToken({ commit }, token) {
|
||||
Cookies.set(TokenCookieName, token)
|
||||
commit('setToken', token)
|
||||
commit('computeTokenVars')
|
||||
},
|
||||
}
|
||||
|
||||
// mutations
|
||||
const mutations = {
|
||||
setToken(state, token) {
|
||||
state.token = token
|
||||
},
|
||||
setNonce(state, nonce) {
|
||||
state.nonce = nonce
|
||||
},
|
||||
computeTokenVars(state) {
|
||||
if (state.token == null) return
|
||||
try {
|
||||
state.tokenData = jwt_decode(state.token)
|
||||
} catch {
|
||||
console.log('Token was invalid.')
|
||||
state.tokenData = null
|
||||
state.token = null
|
||||
}
|
||||
},
|
||||
setMetadata(state, [clientId, authority, tenant]) {
|
||||
state.clientId = clientId
|
||||
state.authority = authority
|
||||
state.tenant = tenant
|
||||
},
|
||||
}
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
state,
|
||||
getters,
|
||||
actions,
|
||||
mutations,
|
||||
}
|
46
KTUSAPS/ClientApp/src/store/modules/msalAuth.js
Normal file
46
KTUSAPS/ClientApp/src/store/modules/msalAuth.js
Normal file
@@ -0,0 +1,46 @@
|
||||
import { WatchMsalState, GetMsalState } from '@/msal'
|
||||
|
||||
// initial state
|
||||
const state = () => ({
|
||||
isLoggedIn: false,
|
||||
accessToken: null,
|
||||
idToken: null,
|
||||
email: null,
|
||||
displayName: null,
|
||||
|
||||
debugFullTokenResponse: null,
|
||||
debugAccountInfo: null,
|
||||
})
|
||||
|
||||
// getters
|
||||
const getters = {}
|
||||
|
||||
// actions
|
||||
const actions = {
|
||||
initialize({ commit }) {
|
||||
WatchMsalState(() => {
|
||||
commit('setState', GetMsalState())
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
// mutations
|
||||
const mutations = {
|
||||
setState(state, msalState) {
|
||||
state.isLoggedIn = msalState.isLoggedIn
|
||||
state.accessToken = msalState.accessToken
|
||||
state.idToken = msalState.idToken
|
||||
state.debugFullTokenResponse = msalState.debugFullTokenResponse
|
||||
state.debugAccountInfo = msalState.debugAccountInfo
|
||||
state.email = msalState.email
|
||||
state.displayName = msalState.displayName
|
||||
},
|
||||
}
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
state,
|
||||
getters,
|
||||
actions,
|
||||
mutations,
|
||||
}
|
Reference in New Issue
Block a user