Skip to content

Commit

Permalink
Added two-factor authentication logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Sleeckx committed Aug 1, 2016
1 parent d16cb2a commit 4d0c8db
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 deletions.
9 changes: 6 additions & 3 deletions src/Libs/Vidyano/vidyano.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,8 @@ namespace Vidyano {
this.authToken = null;
delete data.authToken;

if (this.isUsingDefaultCredentials) {
data.userName = this._clientData.defaultUser;
if (this.defaultUserName) {
data.userName = this.defaultUserName;
delete data.password;
this._postJSON(url, data).then(resolve, reject);
} else {
Expand Down Expand Up @@ -915,13 +915,16 @@ namespace Vidyano {
}
}

signInUsingCredentials(userName: string, password: string): Promise<Application> {
signInUsingCredentials(userName: string, password: string, code?: string): Promise<Application> {
this._setUserName(userName);

const data = this._createData("getApplication");
data.userName = userName;
data.password = password;

if (code)
data.code = code;

return this._getApplication(data);
}

Expand Down
2 changes: 1 addition & 1 deletion src/WebComponents/SignIn/sign-in-provider.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
padding-left: var(--theme-h1);

> div {
&:first-child {
&:not(:last-child) {
margin-bottom: var(--theme-h4);
margin-top: var(--theme-h3);
}
Expand Down
6 changes: 6 additions & 0 deletions src/WebComponents/SignIn/sign-in.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ <h4>[[description]]</h4>
<label>[[translateMessage('Password', isAttached)]]</label>
<input value="{{password::input}}" id="pass" type="password" disabled$="[[signingIn]]" on-keydown="_keydown" />
</div>
<template is="dom-if" if="[[twoFactorAuthentication]]">
<div>
<label>[[translateMessage('TwoFactorCode', isAttached)]]</label>
<input value="{{twoFactorCode::input}}" id="twofactor" type="number" disabled$="[[signingIn]]" on-keydown="_keydown" />
</div>
</template>
<div class="layout horizontal">
<vi-checkbox class="flex" label="[[translateMessage('StaySignedIn', isAttached)]]" disabled$="[[signingIn]]" checked="{{staySignedIn}}"></vi-checkbox>
<div>
Expand Down
57 changes: 44 additions & 13 deletions src/WebComponents/SignIn/sign-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@
signInLabel: {
type: String,
computed: "_computeSigninButtonLabel(signingIn, signingInCounter, app)"
},
twoFactorAuthentication: {
type: Boolean,
readOnly: true,
value: false
},
twoFactorCode: {
type: String,
notify: true
}
},
listeners: {
Expand All @@ -152,9 +161,12 @@
isVidyano: boolean;
signingIn: boolean;
signingInCounter: number;
twoFactorAuthentication: boolean;
twoFactorCode: string;

private _setIsVidyano: (val: boolean) => void;
private _setSigningIn: (val: boolean) => void;
private _setTwoFactorAuthentication: (val: boolean) => void;

constructor(isVidyano: boolean, private _isOnlyProvider: boolean) {
super();
Expand Down Expand Up @@ -223,31 +235,50 @@
private _signIn() {
this._setSigningIn(true);

const password = this.password;
this.password = "";

const currentRoute = this.app.currentRoute;
this.app.service.staySignedIn = this.staySignedIn;
this.app.service.signInUsingCredentials(this.userName, password).then(() => {
this.app.service.signInUsingCredentials(this.userName, this.password, this.twoFactorAuthentication ? this.twoFactorCode : undefined).then(() => {
this._setTwoFactorAuthentication(false);
this._setSigningIn(false);

this.password = "";
this.twoFactorCode = "";

if (currentRoute === this.app.currentRoute) {
const route = this.findParent<AppRoute>(e => e instanceof Vidyano.WebComponents.AppRoute);
this.app.changePath(decodeURIComponent(route.parameters.returnUrl || ""));
}
}, e => {
this._setSigningIn(false);
this._setSigningIn(false);

if (e === "Two-factor authentication enabled for user." || e === "Invalid code.") {
if (e === "Invalid code.")
this.app.showAlert(e, NotificationType.Error, 3000);

const pass = <HTMLInputElement><any>this.$$("input#pass");
pass.focus();
this._setTwoFactorAuthentication(true);

this.app.showMessageDialog({
title: this.app.label || document.title,
message: e,
actions: [ this.translateMessage("OK") ],
actionTypes: ["Danger"]
});
Polymer.dom(this).flush();

const input = <HTMLInputElement>Polymer.dom(this.root).querySelector("#twofactor");
input.value = "";
input.focus();

return;
}

this.password = "";
this.twoFactorCode = "";

const pass = <HTMLInputElement><any>this.$$("input#pass");
pass.focus();

this.app.showMessageDialog({
title: this.app.label || document.title,
message: e,
actions: [this.translateMessage("OK")],
actionTypes: ["Danger"]
});
});
}

private _computeSigninButtonLabel(signingIn: boolean, signingInCounter: number, app: Vidyano.WebComponents.App): string {
Expand Down

0 comments on commit 4d0c8db

Please sign in to comment.