Skip to content
Snippets Groups Projects
Commit 99817d6f authored by Hermann's avatar Hermann
Browse files

move login to OAuth for new API version

parent 9c0b6ab8
No related branches found
No related tags found
No related merge requests found
import m from 'mithril';
import axios from 'axios'; import axios from 'axios';
import ClientOAuth2 from 'client-oauth2';
import * as localStorage from './localStorage'; import * as localStorage from './localStorage';
import config from './config.json'; import config from './config.json';
const m = require('mithril');
// Object which stores the current login-state // Object which stores the current login-state
const APISession = { const APISession = {
authenticated: false, authenticated: false,
token: '', token: '',
}; };
// OAuth Handler
const oauth = new ClientOAuth2({
clientId: 'Local Tool',
authorizationUri: `${config.apiUrl}/oauth`,
redirectUri: 'http://localhost:9000/oauthcallback',
});
export function resetSession() { export function resetSession() {
APISession.authenticated = false; APISession.authenticated = false;
APISession.token = ''; APISession.token = '';
m.route.set('/login'); localStorage.remove('token');
window.location.replace(oauth.token.getUri());
} }
const amivapi = axios.create({ const amivapi = axios.create({
...@@ -77,27 +85,11 @@ export function getSession() { ...@@ -77,27 +85,11 @@ export function getSession() {
}); });
} }
export function login(username, password) {
return new Promise((resolve, reject) => {
amivapi.post('sessions', { username, password })
.then((response) => {
if (response.status === 201) {
APISession.token = response.data.token;
APISession.authenticated = true;
localStorage.set('token', response.data.token);
resolve();
}
reject();
}).catch(reject);
});
}
export class ResourceHandler { export class ResourceHandler {
constructor(resource, searchKeys = false) { constructor(resource, searchKeys = false) {
this.resource = resource; this.resource = resource;
this.searchKeys = searchKeys || config[resource].searchKeys; this.searchKeys = searchKeys || config[resource].searchKeys;
this.patchKeys = config[resource].patchableKeys; this.patchKeys = config[resource].patchableKeys;
checkAuthenticated(); checkAuthenticated();
} }
...@@ -250,3 +242,15 @@ export class ResourceHandler { ...@@ -250,3 +242,15 @@ export class ResourceHandler {
}); });
} }
} }
export class OauthRedirect {
view() {
oauth.token.getToken(m.route.get()).then((response) => {
APISession.authenticated = true;
APISession.token = response.accessToken;
localStorage.set('token', response.accessToken);
m.route.set('/users');
});
return 'redirecting...';
}
}
{ {
"apiUrl": "https://amiv-api.ethz.ch/", "apiUrl": "https://api-dev.amiv.ethz.ch",
"events": { "events": {
"keyDescriptors": { "keyDescriptors": {
"title_de": "German Title", "title_de": "German Title",
......
import m from 'mithril'; import m from 'mithril';
import LoginScreen from './login'; import { OauthRedirect } from './auth';
import TableView from './views/tableView'; import TableView from './views/tableView';
import { UserModal, UserTable, NewUser } from './userTool'; import { UserModal, UserTable, NewUser } from './userTool';
import { MembershipView } from './membershipTool'; import { MembershipView } from './membershipTool';
...@@ -13,7 +13,6 @@ import './style'; ...@@ -13,7 +13,6 @@ import './style';
const root = document.body; const root = document.body;
function layoutWith(view) { function layoutWith(view) {
return { return {
view() { view() {
...@@ -22,6 +21,7 @@ function layoutWith(view) { ...@@ -22,6 +21,7 @@ function layoutWith(view) {
}; };
} }
m.route.prefix('');
m.route(root, '/users', { m.route(root, '/users', {
'/users': layoutWith(UserTable), '/users': layoutWith(UserTable),
'/users/:id': layoutWith(UserModal), '/users/:id': layoutWith(UserModal),
...@@ -40,6 +40,8 @@ m.route(root, '/users', { ...@@ -40,6 +40,8 @@ m.route(root, '/users', {
}); });
}, },
}), }),
'/login': LoginScreen, '/oauthcallback': OauthRedirect,
// '/announce': layoutWith(AnnounceTool), // '/announce': layoutWith(AnnounceTool),
}); });
m.route.prefix('');
...@@ -3,6 +3,7 @@ import '@material/drawer'; ...@@ -3,6 +3,7 @@ import '@material/drawer';
import { List, ListTile, Icon, Toolbar, ToolbarTitle } from 'polythene-mithril'; import { List, ListTile, Icon, Toolbar, ToolbarTitle } from 'polythene-mithril';
import { styler } from 'polythene-core-css'; import { styler } from 'polythene-core-css';
import { icons } from './views/elements'; import { icons } from './views/elements';
import { resetSession } from './auth';
const layoutStyle = [ const layoutStyle = [
{ {
...@@ -65,6 +66,7 @@ export default class Layout { ...@@ -65,6 +66,7 @@ export default class Layout {
m('div.wrapper-main.smooth', [ m('div.wrapper-main.smooth', [
m(Toolbar, { className: 'main-toolbar' }, [ m(Toolbar, { className: 'main-toolbar' }, [
m(ToolbarTitle, { text: 'AMIV Admintools' }), m(ToolbarTitle, { text: 'AMIV Admintools' }),
m('a', { onclick: resetSession }, 'Logout'),
]), ]),
m( m(
'nav.mdc-drawer.mdc-drawer--permanent.mdc-typography.wrapper-sidebar', 'nav.mdc-drawer.mdc-drawer--permanent.mdc-typography.wrapper-sidebar',
...@@ -98,14 +100,6 @@ export default class Layout { ...@@ -98,14 +100,6 @@ export default class Layout {
), ),
m('div.wrapper-content', children), m('div.wrapper-content', children),
]), ]),
/*return m('div.wrapper-sidebar.smooth', m('div.container-fluid', [
m('a[href=/]', { oncreate: m.route.link }, [
m('img.sidebar-logo[src="res/logo/main.svg"]'),
]),
m('ul.nav.nav-pills.nav-stacked.nav-sidebar', [
]),
]));*/
]); ]);
} }
} }
...@@ -34,3 +34,4 @@ export function set(key, value, shortSession = false) { ...@@ -34,3 +34,4 @@ export function set(key, value, shortSession = false) {
window.localStorage.setItem(`glob-${key}`, value); window.localStorage.setItem(`glob-${key}`, value);
} }
} }
import { login } from './auth';
const m = require('mithril');
const FormState = {
username: '',
setUsername(v) { FormState.username = v; },
password: '',
setPassword(v) { FormState.password = v; },
};
export default class LoginScreen {
view() {
return m('div', { class: 'loginPanel smooth' }, [
m('div.col-sm-4'),
m('div.col-sm-4', [
m('img.login-logo', { src: 'res/logo/main.svg' }),
m('div.input-group', [
m('input.form-control', {
oninput: m.withAttr('value', FormState.setUsername),
placeholder: 'user',
}),
m('span.input-group-addon', '@student.ethz.ch'),
]),
m('br'),
m('input.form-control', {
oninput: m.withAttr('value', FormState.setPassword),
placeholder: 'password',
type: 'password',
}),
m('br'),
m('button.btn.btn-default', {
onclick() {
login(FormState.username, FormState.password).then(() => {
m.route.set('/users');
});
},
}, 'Submit'),
]),
m('div.col-sm-4'),
m('div', [
m('span', FormState.username),
m('span', FormState.password),
]),
]);
}
}
...@@ -20,6 +20,9 @@ const config = { ...@@ -20,6 +20,9 @@ const config = {
port: 9000, port: 9000,
hot: true, hot: true,
index: 'index.html', index: 'index.html',
historyApiFallback: {
index: 'index.html',
},
}, },
plugins: [ plugins: [
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment