login through OIDC now working with local JWT aund (test) user listing

This commit is contained in:
2019-04-04 16:06:05 +02:00
parent c0b53decc2
commit 5332940a5b
6 changed files with 117 additions and 7 deletions

5
package-lock.json generated
View File

@@ -12881,6 +12881,11 @@
} }
} }
}, },
"vue-cookies": {
"version": "1.5.13",
"resolved": "https://registry.npmjs.org/vue-cookies/-/vue-cookies-1.5.13.tgz",
"integrity": "sha512-8pjpXnvbNWx1Lft0t3MJnW+ylv0Wa2Tb6Ch617u/pQah3+SfXUZZdkh3EL3bSpe/Sw2Wdw3+DhycgQsKANSxXg=="
},
"vue-flag-icon": { "vue-flag-icon": {
"version": "1.0.6", "version": "1.0.6",
"resolved": "https://registry.npmjs.org/vue-flag-icon/-/vue-flag-icon-1.0.6.tgz", "resolved": "https://registry.npmjs.org/vue-flag-icon/-/vue-flag-icon-1.0.6.tgz",

View File

@@ -15,6 +15,7 @@
"vue": "^2.6.6", "vue": "^2.6.6",
"vue-axios": "^2.1.4", "vue-axios": "^2.1.4",
"vue-class-component": "^6.0.0", "vue-class-component": "^6.0.0",
"vue-cookies": "^1.5.13",
"vue-flag-icon": "^1.0.6", "vue-flag-icon": "^1.0.6",
"vue-i18n": "^8.9.0", "vue-i18n": "^8.9.0",
"vue-property-decorator": "^7.0.0", "vue-property-decorator": "^7.0.0",

View File

@@ -15,7 +15,7 @@ export function saveSurveyResponse (surveyResponse: any) {
} }
export function postNewSurvey (survey: any, jwt: any) { export function postNewSurvey (survey: any, jwt: any) {
return axios.post(`${API_URL}/surveys/`, survey, { headers: { Authorization: `Bearer: ${jwt}` } }) return axios.post(`${API_URL}/surveys/`, survey, { headers: { Authorization: `Bearer ${jwt}` } })
} }
export function authenticate (userData: any) { export function authenticate (userData: any) {
@@ -26,6 +26,21 @@ export function register (userData: any) {
return axios.post(`${API_URL}/auth/register`, userData) return axios.post(`${API_URL}/auth/register`, userData)
} }
// This function probably isn't really useful here, as a user must be redirected to the actual OIDC provider.
// -> So login involves user interaction through the frontend (and not just API calls).
export function oidc_login (redirection_url: any) {
return axios.get(`${API_URL}/auth/oidc`, redirection_url)
}
export function getFreshToken (refresh_token: any) {
return axios.get(`${API_URL}/auth/fresh`, refresh_token)
}
export function getProviders () { export function getProviders () {
return axios.get(`${API_URL}/auth/providers`) return axios.get(`${API_URL}/auth/providers`)
} }
export function fetchUsers (jwt: any) {
console.log("JWT: " + jwt);
return axios.get(`${API_URL}/v1/user`, { headers: { Authorization: `Bearer ${jwt}` } })
}

View File

@@ -13,6 +13,15 @@
<a :href="provider.url">{{ index }} ({{provider.type}})</a> <a :href="provider.url">{{ index }} ({{provider.type}})</a>
</li> </li>
</ul> </ul>
<h3>Users</h3>
<ul>
<li v-for="(user) in users" >
<a href="#">({{user.id}}) {{user.first_name}}</a>
</li>
</ul>
<button v-on:click="oidc_login">Login via OIDC</button>
<a href="/api/auth/oidc?redirect_url=/login">OIDC with redirect</a>
</section> </section>
<section class="section"> <section class="section">
<div class="container"> <div class="container">
@@ -61,6 +70,9 @@
this.$store.dispatch('register', { email: this.email, password: this.password }) this.$store.dispatch('register', { email: this.email, password: this.password })
.then(() => this.$router.push('/')); .then(() => this.$router.push('/'));
}, },
oidc_login () {
this.$store.dispatch('oidc_login', "\\oidc_login_redirection");
},
}, },
mounted () { mounted () {
EventBus.$on('failedRegistering', (msg) => { EventBus.$on('failedRegistering', (msg) => {
@@ -72,12 +84,28 @@
EventBus.$on('loginProvidersLoaded', (providers) => { EventBus.$on('loginProvidersLoaded', (providers) => {
this.loginProviders = providers; this.loginProviders = providers;
}); });
EventBus.$on('storedTokens', () => {
this.$store.dispatch('loadUsers');
});
this.$store.dispatch('loadLoginProviders'); this.$store.dispatch('loadLoginProviders');
// get tokens
if(this.$cookies.isKey("tokens")){
this.$store.dispatch('storeTokens', JSON.parse(atob(this.$cookies.get("tokens"))));
this.$cookies.remove("tokens");
}
console.log(this.$cookies.keys());
}, },
beforeDestroy () { beforeDestroy () {
EventBus.$off('failedRegistering'); EventBus.$off('failedRegistering');
EventBus.$off('failedAuthentication'); EventBus.$off('failedAuthentication');
}, },
computed: {
users () {
return this.$store.state.users;
},
},
} }
</script> </script>

View File

@@ -4,6 +4,7 @@ import VueAxios from 'vue-axios';
import App from './App.vue'; import App from './App.vue';
import router from './router'; import router from './router';
import store from './store'; import store from './store';
import VueCookies from 'vue-cookies'
import i18n from '@/plugins/i18n'; import i18n from '@/plugins/i18n';
// @ts-ignore // @ts-ignore
import FlagIcon from 'vue-flag-icon'; import FlagIcon from 'vue-flag-icon';
@@ -12,6 +13,7 @@ import FlagIcon from 'vue-flag-icon';
Vue.use(VueAxios, axios); Vue.use(VueAxios, axios);
Vue.use(FlagIcon); Vue.use(FlagIcon);
Vue.use(VueCookies)
// setup fake backend // setup fake backend
// import { configureFakeBackend } from './helpers'; // import { configureFakeBackend } from './helpers';

View File

@@ -3,7 +3,16 @@ import Vuex from 'vuex';
// imports of AJAX functions will go here // imports of AJAX functions will go here
import { fetchSurveys, fetchSurvey, getProviders, saveSurveyResponse, postNewSurvey, authenticate, register } from '@/api'; import {
fetchSurveys,
fetchSurvey,
getProviders,
saveSurveyResponse,
postNewSurvey,
authenticate,
register,
oidc_login, fetchUsers, getFreshToken
} from '@/api';
import { isValidJwt, EventBus } from '@/utils'; import { isValidJwt, EventBus } from '@/utils';
Vue.use(Vuex); Vue.use(Vuex);
@@ -14,7 +23,11 @@ const state = {
loginProviders: [], loginProviders: [],
currentSurvey: {}, currentSurvey: {},
user: {}, user: {},
users: [],
access_token: '',
refresh_token: '',
jwt: '', jwt: '',
jwt_refresh_token: '',
}; };
const actions = { const actions = {
@@ -35,6 +48,19 @@ const actions = {
addSurveyResponse(context: any) { addSurveyResponse(context: any) {
return saveSurveyResponse(context.state.currentSurvey); return saveSurveyResponse(context.state.currentSurvey);
}, },
loadUsers(context: any) {
return fetchUsers(context.state.access_token)
.then((response) => {
console.log(response);
console.log(response.data);
context.commit('setUsers', { users: response.data });
EventBus.$emit('usersLoaded', response.data);
})
.catch((error) => {
console.log('Error loading users!', error);
EventBus.$emit('failedLoadingUsers', error);
});
},
loadLoginProviders(context: any) { loadLoginProviders(context: any) {
return getProviders() return getProviders()
.then((response) => { .then((response) => {
@@ -45,12 +71,34 @@ const actions = {
login(context: any, userData: any) { login(context: any, userData: any) {
context.commit('setUserData', { userData }); context.commit('setUserData', { userData });
return authenticate(userData) return authenticate(userData)
.then((response) => context.commit('setJwtToken', { jwt: response.data })) .then((response) => context.commit('setJwtToken', { tokens: response.data }))
.catch((error) => { .catch((error) => {
console.log('Error Authenticating: ', error); console.log('Error Authenticating: ', error);
EventBus.$emit('failedAuthentication', error); EventBus.$emit('failedAuthentication', error);
}); });
}, },
oidc_login(context: any, redirection_url: any) {
//context.commit('setUserData', { userData });
return oidc_login(redirection_url)
.then((response) => context.commit('setJwtToken', { tokens: response.data }))
.catch((error) => {
console.log('Error Authenticating: ', error);
EventBus.$emit('failedAuthentication', error);
});
},
refreshToken(context: any) {
//context.commit('setUserData', { userData });
return getFreshToken(context.state.refresh_token)
.then((response) => context.commit('setTokens', { tokens: response.data }))
.catch((error) => {
console.log('Error Refreshing token: ', error);
EventBus.$emit('failedRefreshingToken', error);
});
},
storeTokens(context: any, tokens: any){
context.commit('setTokens', {tokens});
EventBus.$emit('storedTokens');
},
register(context: any, userData: any) { register(context: any, userData: any) {
context.commit('setUserData', { userData }); context.commit('setUserData', { userData });
return register(userData) return register(userData)
@@ -61,7 +109,7 @@ const actions = {
}); });
}, },
submitNewSurvey(context: any, survey: any) { submitNewSurvey(context: any, survey: any) {
return postNewSurvey(survey, context.state.jwt.token); return postNewSurvey(survey, context.state.access_token);
}, },
}; };
@@ -77,6 +125,9 @@ const mutations = {
} }
sState.currentSurvey = payload.survey; sState.currentSurvey = payload.survey;
}, },
setUsers(sState: any, payload: any) {
sState.users = payload.users;
},
setChoice(sState: any, payload: any) { setChoice(sState: any, payload: any) {
const { questionId, choice } = payload; const { questionId, choice } = payload;
const nQuestions = sState.currentSurvey.questions.length; const nQuestions = sState.currentSurvey.questions.length;
@@ -97,9 +148,17 @@ const mutations = {
}, },
setJwtToken(sState: any, payload: any) { setJwtToken(sState: any, payload: any) {
console.log('setJwtToken payload = ', payload); console.log('setJwtToken payload = ', payload);
localStorage.token = payload.jwt.token; localStorage.tokens = payload.tokens;
sState.jwt = payload.jwt; sState.jwt = payload.tokens.access_token;
sState.jwt_refresh_token = payload.tokens.refresh_token;
}, },
setTokens(sState: any, payload:any){
console.log('setTokens payload = ', payload);
sState.access_token = payload.tokens.access_token;
sState.refresh_token = payload.tokens.refresh_token;
console.log(sState.access_token);
console.log(sState.access_token);
}
}; };
const getters = { const getters = {