From dadeb4534296f94dbe1796b3807abf8c3105ba2b Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Tue, 6 Nov 2018 23:54:47 +0000 Subject: [PATCH] --- app/ayanova/src/api/auth.js | 84 +++++++++++++++++++++++------ app/ayanova/src/router.js | 96 ++++++++++++++++----------------- app/ayanova/src/utils/auth.js | 31 +++++------ app/ayanova/src/views/login.vue | 43 +++++++++------ server/AyaNova/Startup.cs | 16 +++++- 5 files changed, 173 insertions(+), 97 deletions(-) diff --git a/app/ayanova/src/api/auth.js b/app/ayanova/src/api/auth.js index 614ba678..af0cc98b 100644 --- a/app/ayanova/src/api/auth.js +++ b/app/ayanova/src/api/auth.js @@ -1,24 +1,74 @@ +/*eslint-disable*/ import ayconfig from "../utils/config"; +function status(response) { + if (response.status >= 200 && response.status < 300) { + return Promise.resolve(response); + } else { + return Promise.reject(new Error(response.statusText)); + } +} + +function json(response) { + var v = response.json(); + return v; +} + export default { - authenticate(login, password) { - fetch(ayconfig.apiUrl + "/auth", { - login: login, - password: password - }) - .then(function(response) { - if (response.status != 200) { - alert("Error: " + response.statusText); - return; - } - // Handle response you get from the server - response.json().then(function(data) { - //set token in config here - return data; - }); + authenticate(login, password, cb) { + fetch(ayconfig.apiUrl + "auth", { + method: "post", + mode: "cors", + headers: { + Accept: "application/json, text/plain, */*", + "Content-Type": "application/json" + }, + body: JSON.stringify({ + login: login, + password: password }) - .catch(function(err) { - alert("Fetch Error :-S", err); + }) + .then(status) + .then(json) + .then(function(data) { + console.log( + "AUTH.JS::authenticate() -> Request succeeded with JSON response", + data + ); + cb(data); + //return data; + }) + .catch(function(error) { + console.log("Request failed", error); + cb(error); //sb cb(data,error or something) + //return error; + }); + }, + async authenticatepromise(login, password) { + return fetch(ayconfig.apiUrl + "auth", { + method: "post", + mode: "cors", + headers: { + Accept: "application/json, text/plain, */*", + "Content-Type": "application/json" + }, + body: JSON.stringify({ + login: login, + password: password + }) + }) + .then(status) + .then(json) + .then(function(data) { + console.log( + "AUTH.JS::authenticatepromise() -> Request succeeded with JSON response", + data + ); + return data; + }) + .catch(function(error) { + console.log("Request failed", error); + return error; }); } }; diff --git a/app/ayanova/src/router.js b/app/ayanova/src/router.js index bdb771bc..dc8817dc 100644 --- a/app/ayanova/src/router.js +++ b/app/ayanova/src/router.js @@ -1,7 +1,7 @@ import Vue from "vue"; import Router from "vue-router"; import Home from "./views/Home.vue"; -import { isLoggedIn, login, logout } from './utils/auth'; +//import { isLoggedIn, login, logout } from "./utils/auth"; Vue.use(Router); @@ -9,30 +9,30 @@ export default new Router({ mode: "history", base: process.env.BASE_URL, routes: [ - { - path: "/", - redirect: { - name: "login" - } - }, + // { + // path: "/", + // redirect: { + // name: "login" + // } + // }, { path: "/login", name: "login", component: () => import(/* webpackChunkName: "login" */ "./views/login.vue") }, - { - path: "/secure", - name: "secure", - component: () => - import(/* webpackChunkName: "secure" */ "./views/secure.vue") - }, - // { - // path: "/", - // name: "home", - // component: Home + // path: "/secure", + // name: "secure", + // component: () => + // import(/* webpackChunkName: "secure" */ "./views/secure.vue") // }, + + { + path: "/", + name: "home", + component: Home + }, { path: "/about", name: "about", @@ -45,34 +45,34 @@ export default new Router({ ] }); -router.beforeEach((to, from, next) => { - if(to.matched.some(record => record.meta.requiresAuth)) { - if (localStorage.getItem('jwt') == null) { - next({ - path: '/login', - params: { nextUrl: to.fullPath } - }) - } else { - let user = JSON.parse(localStorage.getItem('user')) - if(to.matched.some(record => record.meta.is_admin)) { - if(user.is_admin == 1){ - next() - } - else{ - next({ name: 'userboard'}) - } - }else { - next() - } - } - } else if(to.matched.some(record => record.meta.guest)) { - if(localStorage.getItem('jwt') == null){ - next() - } - else{ - next({ name: 'userboard'}) - } - }else { - next() - } -}) +// Router.beforeEach((to, from, next) => { +// if(to.matched.some(record => record.meta.requiresAuth)) { +// if (localStorage.getItem('jwt') == null) { +// next({ +// path: '/login', +// params: { nextUrl: to.fullPath } +// }) +// } else { +// let user = JSON.parse(localStorage.getItem('user')) +// if(to.matched.some(record => record.meta.is_admin)) { +// if(user.is_admin == 1){ +// next() +// } +// else{ +// next({ name: 'userboard'}) +// } +// }else { +// next() +// } +// } +// } else if(to.matched.some(record => record.meta.guest)) { +// if(localStorage.getItem('jwt') == null){ +// next() +// } +// else{ +// next({ name: 'userboard'}) +// } +// }else { +// next() +// } +// }) diff --git a/app/ayanova/src/utils/auth.js b/app/ayanova/src/utils/auth.js index c308e2cc..fa27868f 100644 --- a/app/ayanova/src/utils/auth.js +++ b/app/ayanova/src/utils/auth.js @@ -3,20 +3,6 @@ import decode from "jwt-decode"; //import auth0 from 'auth0-js'; //import Router from 'vue-router'; //import Auth0Lock from 'auth0-lock'; -const ID_TOKEN_KEY = "id_token"; -const ACCESS_TOKEN_KEY = "access_token"; -const USER_ROLES = AuthorizationRoles.NoRole; - -// const CLIENT_ID = '{AUTH0_CLIENT_ID}'; -// const CLIENT_DOMAIN = '{AUTH0_DOMAIN}'; -// const REDIRECT = 'YOUR_CALLBACK_URL'; -// const SCOPE = '{SCOPE}'; -// const AUDIENCE = 'AUDIENCE_ATTRIBUTE'; - -// var auth = new auth0.WebAuth({ -// clientID: CLIENT_ID, -// domain: CLIENT_DOMAIN -// }); //https://stackoverflow.com/questions/15551652/javascript-enum-flag-check const AuthorizationRoles = { @@ -54,6 +40,21 @@ const AuthorizationRoles = { OpsAdminFull: 16384 }; //end AuthorizationRoles +const ID_TOKEN_KEY = "id_token"; +const ACCESS_TOKEN_KEY = "access_token"; +const USER_ROLES = AuthorizationRoles.NoRole; + +// const CLIENT_ID = '{AUTH0_CLIENT_ID}'; +// const CLIENT_DOMAIN = '{AUTH0_DOMAIN}'; +// const REDIRECT = 'YOUR_CALLBACK_URL'; +// const SCOPE = '{SCOPE}'; +// const AUDIENCE = 'AUDIENCE_ATTRIBUTE'; + +// var auth = new auth0.WebAuth({ +// clientID: CLIENT_ID, +// domain: CLIENT_DOMAIN +// }); + export function login() { // auth.authorize({ // responseType: 'token id_token', @@ -149,4 +150,4 @@ export function hasRole(role) { // } } -//TODO: Auth JWT needs to return roles as an int enum +//TODO: Auth JWT needs to return roles as an int enum diff --git a/app/ayanova/src/views/login.vue b/app/ayanova/src/views/login.vue index 42b1e932..ffa64e2a 100644 --- a/app/ayanova/src/views/login.vue +++ b/app/ayanova/src/views/login.vue @@ -22,23 +22,34 @@ export default { methods: { login() { if (this.input.username != "" && this.input.password != "") { - // eslint-disable-next-line - var response = auth.authenticate( - this.input.username, - this.input.password - ); - - if ( - this.input.username == this.$store.state.mockAccount.username && - this.input.password == this.$store.state.mockAccount.password + auth.authenticate(this.input.username, this.input.password, function( + data ) { - this.$emit("authenticated", true); - this.$router.replace({ name: "secure" }); - } else { - alert("The username and / or password is incorrect"); - } - } else { - alert("A username and password must be present"); + // eslint-disable-next-line + console.log("LOGIN.VUE::login() -> CB VERSION DATA:", data); + }); + + // if ( + // this.input.username == this.$store.state.mockAccount.username && + // this.input.password == this.$store.state.mockAccount.password + // ) { + // this.$emit("authenticated", true); + // this.$router.replace({ name: "secure" }); + // } else { + // alert("The username and / or password is incorrect"); + // } + // } else { + // alert("A username and password must be present"); + + auth + .authenticatepromise(this.input.username, this.input.password) + .then(response => { + // eslint-disable-next-line + console.log( + "LOGIN.VUE::login() -> PROMISE VERSION RESPONSE:", + response + ); + }); } } } diff --git a/server/AyaNova/Startup.cs b/server/AyaNova/Startup.cs index 78df6513..d8912fa5 100644 --- a/server/AyaNova/Startup.cs +++ b/server/AyaNova/Startup.cs @@ -130,6 +130,18 @@ namespace AyaNova }); + // Add service and create Policy with options + _log.LogDebug("BOOT: init CORS service"); + services.AddCors(options => + { + options.AddPolicy("CorsPolicy", + builder => builder.AllowAnyOrigin() + .AllowAnyMethod() + .AllowAnyHeader() + .AllowCredentials()); + }); + + _log.LogDebug("BOOT: init MVC service"); _log.LogDebug("BOOT: init Metrics service"); @@ -240,7 +252,7 @@ namespace AyaNova // Token will only be valid if not expired yet, with 5 minutes clock skew. ValidateLifetime = true, - RequireExpirationTime = true, + RequireExpirationTime = true, ClockSkew = new TimeSpan(0, 5, 0), }; }); @@ -357,6 +369,8 @@ namespace AyaNova #endregion + _log.LogDebug("BOOT: pipeline - CORS"); + app.UseCors("CorsPolicy"); //USE MVC _log.LogDebug("BOOT: pipeline - MVC");