Commit b11c7804 authored by Cédric Moreau's avatar Cédric Moreau

[enh] Connexion sous 2 modes possibles

parent 9175dc13
......@@ -27,6 +27,7 @@ import {AuthService} from "../services/auth-service";
import {AuthModal} from "../components/auth_modal/auth_modal";
import {AccountPage} from "../pages/account/account";
import {ConnectPage} from "../pages/connect/connect";
import {CryptoService} from "../services/crypo-service";
const appRoutes: Routes = [
{
......@@ -121,6 +122,7 @@ const appRoutes: Routes = [
AnnounceService,
ImageService,
AuthService,
CryptoService,
{provide: APP_BASE_HREF, useValue: '/#/'},
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
......
import {Component, OnInit, ViewChild} from "@angular/core";
import {NavParams, ViewController} from "ionic-angular";
const scrypt = require('scrypt-async')
const tweetnacl = require('tweetnacl')
const tweetnaclUtil = require('tweetnacl-util')
import {CryptoService} from "../../services/crypo-service";
const base58 = require('../../lib/base58')
@Component({
......@@ -24,6 +22,7 @@ export class AuthModal implements OnInit {
constructor(
private params: NavParams,
public cryptoService: CryptoService,
public viewCtrl: ViewController
) {
this.expectedPub = this.params.get('expectedPub')
......@@ -46,21 +45,7 @@ export class AuthModal implements OnInit {
}
getKeyPair() {
return new Promise((res) => {
scrypt(this.passwd, this.salt, {
N: 4096,
r: 16,
p: 1,
dkLen: 32,
encoding: 'base64'
}, function(derivedKey) {
res(derivedKey);
});
})
.then((seed) => {
const byteseed = tweetnaclUtil.decodeBase64(seed)
return tweetnacl.sign.keyPair.fromSeed(byteseed)
})
return this.cryptoService.getKeyPair(this.salt, this.passwd)
}
valideCle() {
......
<ion-content padding>
<ion-row>
<ion-col col-12>
<ion-col radio-group
[(ngModel)]="connectionType"
name="type"
#type="ngModel">
<p *ngIf="accountService.acc.pub == loginService.pub">
<button ion-button [routerLink]="['/account/edit', accountService.acc.pub]">Modifier votre fiche</button>
<h2>Identification</h2>
<p>
Vous pouvez vous identifier sur ğchange.
</p>
<p>
S'identifier permet de personnaliser la navigation et d'accéder à des menus concernant votre compte et vos annonces. Toutefois aucune action de modification ne sera disponible directement : pour modifier du contenu, vous devrez vous <b>authentifer</b> le moment venu.
</p>
<h4>Méthode</h4>
<ion-list>
<ion-item class="left-radio">
<ion-radio value="pubkey"></ion-radio>
<ion-label>Par clé publique</ion-label>
</ion-item>
<ion-item class="left-radio">
<ion-label>Par identifiants secrets</ion-label>
<ion-radio
value="brainwallet"></ion-radio>
</ion-item>
</ion-list>
<ng-container *ngIf="connectionType == 'pubkey'">
<p>
<ion-label class="descriptif">C'est la méthode la plus simple et sans aucun risque : donnez une clé publique <b>que vous possédez</b> (si vous ne la possédez pas, vous ne pourrez faire aucune modification ensuite).</ion-label>
</p>
<ion-item>
<ion-label>Clé publique</ion-label>
<ion-input type="text"
[(ngModel)]="pub"
placeholder="Exemple : « AbsxSY4qoZRzyV2irfep1V9xw1EMNyKJw2TkuVD4N1mv »."></ion-input>
</ion-item>
</ng-container>
<ng-container *ngIf="connectionType == 'brainwallet'">
<p>
<ion-label class="descriptif">C'est une méthode avancée qui génère une clé publique à partir d'identifiants secrets. Cette méthode <b>générera temporairement la clé privée associée</b>, et l'oubliera immédaitement après l'identification.</ion-label>
</p>
<ion-item>
<ion-label>Identifiant secret</ion-label>
<ion-input type="password"
[(ngModel)]="salt"
placeholder="Tapez votre identifiant secret."></ion-input>
</ion-item>
<ion-item>
<ion-label>Mot de passe</ion-label>
<ion-input type="password"
[(ngModel)]="passwd"
placeholder="Tapez votre mot de passe." ></ion-input>
</ion-item>
</ng-container>
<p>
<button ion-button
*ngIf="(connectionType == 'pubkey' || connectionType == 'brainwallet')"
[disabled]="!((connectionType == 'pubkey' && pub) || (connectionType == 'brainwallet' && salt && passwd))"
(click)="connect()">S'identifier</button>
</p>
</ion-col>
</ion-row>
</ion-content>
......@@ -6,4 +6,31 @@ connect {
margin-top: 1.8rem;
padding-right: 15px;
}
ion-item.left-radio {
.item-inner {
display: block !important;
font-weight: bold;
}
.input-wrapper {
float: right;
width: 100%;
padding-left: 27px;
}
ion-radio {
margin-top: 14px !important;
}
}
.connection {
padding-left: 30px;
//padding-bottom: 30px;
}
.descriptif {
color: gray;
}
}
import {Component, OnInit} from "@angular/core";
import {AccountService} from "../../services/account-service";
import {LoginService} from "../../services/login-service";
import {ActivatedRoute} from "@angular/router";
import {AnnounceService} from "../../services/announce-service";
import {ToastController} from "ionic-angular";
import {CryptoService} from "../../services/crypo-service";
import {Router} from "@angular/router";
const base58 = require('../../lib/base58')
@Component({
selector: 'connect',
......@@ -11,41 +12,48 @@ import {AnnounceService} from "../../services/announce-service";
export class ConnectPage implements OnInit {
title:string = "Se connecter"
announces:any
connectionType:string
pub:string
salt:string
passwd:string
constructor(
private route: ActivatedRoute,
private router: Router,
public loginService:LoginService,
public accountService:AccountService,
public announceService:AnnounceService) {
if (this.route.snapshot.data.creation) {
this.accountService.beginCreation(loginService.pub)
} else {
this.accountService.clean()
}
public cryptoService: CryptoService,
public toastCtrl: ToastController) {
}
ngOnInit() {
this.route.params.subscribe(params => {
if (params['pub']) {
return this.accountService.getAccountInfos(params['pub'])
.then(res => this.accountService.acc = res.acc)
.then(() => {
return this.announceService.myAnnounces(this.accountService.acc.pub)
})
.then(res => {
this.announces = []
for (const a of res.announces) {
if (parseInt(a.stock)) {
if (a.images.length) {
a.thumbnail = a.images[0]
}
this.announces.push(a)
}
}
})
this.connectionType = ""
this.pub = ""
this.salt = ""
this.passwd = ""
}
connect() {
if (this.connectionType == 'pubkey') {
if (!this.pub.match(/[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{43,44}/)) {
this.message("Échec d'identification : '" + this.pub + "' n'est pas une une clé publique valide.")
} else {
this.loginService.identify(this.pub)
this.router.navigate([`/mon_compte`])
}
}
else if (this.connectionType == 'brainwallet') {
this.cryptoService.getKeyPair(this.salt, this.passwd)
.then((pair) => {
this.loginService.identify(base58.encode(pair.publicKey))
this.router.navigate([`/mon_compte`])
})
}
}
message(message) {
let toast = this.toastCtrl.create({
message,
duration: 3000
});
toast.present();
}
}
<ion-content padding>
<ng-container *ngIf="!loginService.pub">
<p>Vous êtes déconnecté. Vous pouvez néanmoins <button (click)="loginService.showPrompt()">vous reconnecter</button>.</p>
<p>Vous êtes déconnecté. Vous pouvez néanmoins <a routerLink="/connect">vous reconnecter</a>.</p>
</ng-container>
<ng-container *ngIf="loginService.pub">
......
import {Injectable} from "@angular/core";
const scrypt = require('scrypt-async')
const tweetnacl = require('tweetnacl')
const tweetnaclUtil = require('tweetnacl-util')
@Injectable()
export class CryptoService {
constructor() {
}
getKeyPair(salt, passwd) {
return new Promise((res) => {
scrypt(passwd, salt, {
N: 4096,
r: 16,
p: 1,
dkLen: 32,
encoding: 'base64'
}, function(derivedKey) {
res(derivedKey);
});
})
.then((seed) => {
const byteseed = tweetnaclUtil.decodeBase64(seed)
return tweetnacl.sign.keyPair.fromSeed(byteseed)
})
}
}
......@@ -41,10 +41,7 @@ export class LoginService {
else if (!data.title.match(/[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{43,44}/)) {
this.cleIncorrecte("Échec d'identification : '" + data.title + "' n'est pas une une clé publique valide.")
} else {
localStorage.setItem('pub', data.title)
this.pub = data.title
this.estIdentifie = true
this.cleIncorrecte("Identification réussie.")
this.identify(data.title)
}
}
}
......@@ -53,6 +50,13 @@ export class LoginService {
prompt.present();
}
identify(pub:string) {
localStorage.setItem('pub', pub)
this.pub = pub
this.estIdentifie = true
this.cleIncorrecte("Identification réussie.")
}
cleIncorrecte(message) {
let toast = this.toastCtrl.create({
message,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment