Add session token to session storage.

This commit is contained in:
Spencer Ofwiti 2021-01-18 16:04:16 +03:00
parent 461f861b42
commit f966a24014
8 changed files with 60 additions and 26 deletions

6
package-lock.json generated
View File

@ -1784,9 +1784,9 @@
"dev": true "dev": true
}, },
"@types/node": { "@types/node": {
"version": "12.19.3", "version": "12.19.14",
"resolved": "https://registry.npmjs.org/@types/node/-/node-12.19.3.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-12.19.14.tgz",
"integrity": "sha512-8Jduo8wvvwDzEVJCOvS/G6sgilOLvvhn1eMmK3TW8/T217O7u1jdrK6ImKLv80tVryaPSVeKu6sjDEiFjd4/eg==", "integrity": "sha512-2U9uLN46+7dv9PiS8VQJcHhuoOjiDPZOLAt0WuA1EanEknIMae+2QbMhayF7cgGqjvRVIfNpt+6jLPczJZFiRw==",
"dev": true "dev": true
}, },
"@types/q": { "@types/q": {

View File

@ -31,7 +31,7 @@
"@angular/compiler-cli": "~10.2.0", "@angular/compiler-cli": "~10.2.0",
"@types/jasmine": "~3.5.0", "@types/jasmine": "~3.5.0",
"@types/jasminewd2": "~2.0.3", "@types/jasminewd2": "~2.0.3",
"@types/node": "^12.11.1", "@types/node": "^12.19.14",
"codelyzer": "^6.0.0", "codelyzer": "^6.0.0",
"jasmine-core": "~3.6.0", "jasmine-core": "~3.6.0",
"jasmine-spec-reporter": "~5.0.0", "jasmine-spec-reporter": "~5.0.0",

View File

@ -1,4 +1,5 @@
import * as openpgp from '../../assets/js/openpgp.min.js'; // import * as openpgp from '../../assets/js/openpgp.min.js';
const openpgp = require('../../assets/js/openpgp.min.js');
export function UnsafeKeyStore(): void { export function UnsafeKeyStore(): void {
this.key = undefined; this.key = undefined;
@ -13,7 +14,7 @@ UnsafeKeyStore.prototype.fingerprint = function(): any {
return this.key.keyPacket.fingerprint; return this.key.keyPacket.fingerprint;
}; };
UnsafeKeyStore.prototype.keyid = function(): any { UnsafeKeyStore.prototype.keyId = function(): any {
return this.key.getKeyId(); return this.key.getKeyId();
}; };

View File

@ -4,16 +4,24 @@ import { hobaParseChallengeHeader } from '../../assets/js/hoba.js';
import { signChallenge } from '../../assets/js/hoba-pgp.js'; import { signChallenge } from '../../assets/js/hoba-pgp.js';
const origin = 'http://localhost:4444'; const origin = 'http://localhost:4444';
const pgpKeyStore = new UnsafeKeyStore();
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class AuthService { export class AuthService {
pgpKeyStore = new UnsafeKeyStore(); sessionToken: any;
sessionToken = undefined;
sessionLoginCount = 0; sessionLoginCount = 0;
privateKey: any;
constructor() {} constructor() {
if (sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'))) {
this.sessionToken = sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'));
}
if (localStorage.getItem(btoa('CICADA_PRIVATE_KEY'))) {
this.privateKey = localStorage.getItem(btoa('CICADA_PRIVATE_KEY'));
}
}
setState(s): void { setState(s): void {
(document.getElementById('state') as HTMLInputElement).value = s; (document.getElementById('state') as HTMLInputElement).value = s;
@ -51,6 +59,7 @@ export class AuthService {
throw new Error('login rejected'); throw new Error('login rejected');
} }
this.sessionToken = xhr.getResponseHeader('Token'); this.sessionToken = xhr.getResponseHeader('Token');
sessionStorage.setItem(btoa('CICADA_SESSION_TOKEN'), this.sessionToken);
this.sessionLoginCount++; this.sessionLoginCount++;
this.setState('click to perform login ' + this.sessionLoginCount + ' with token ' + this.sessionToken); this.setState('click to perform login ' + this.sessionLoginCount + ' with token ' + this.sessionToken);
console.log('received', xhr.responseText); console.log('received', xhr.responseText);
@ -59,7 +68,7 @@ export class AuthService {
xhr.send(); xhr.send();
} }
getChallenge(callback): void { getChallenge(): void {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.responseType = 'arraybuffer'; xhr.responseType = 'arraybuffer';
xhr.open('GET', origin + window.location.search.substring(1)); xhr.open('GET', origin + window.location.search.substring(1));
@ -67,7 +76,7 @@ export class AuthService {
if (xhr.status === 401) { if (xhr.status === 401) {
const authHeader = xhr.getResponseHeader('WWW-Authenticate'); const authHeader = xhr.getResponseHeader('WWW-Authenticate');
const o = hobaParseChallengeHeader(authHeader); const o = hobaParseChallengeHeader(authHeader);
callback(o); this.loginResponse(o).then();
} }
}; };
xhr.send(); xhr.send();
@ -84,7 +93,7 @@ export class AuthService {
} }
} else { } else {
try { try {
const o = this.getChallenge(this.loginResponse); const o = this.getChallenge();
return true; return true;
} catch (e) { } catch (e) {
console.error('login challenge failed', e); console.error('login challenge failed', e);
@ -94,21 +103,22 @@ export class AuthService {
} }
async loginResponse(o): Promise<void> { async loginResponse(o): Promise<any> {
const r = await signChallenge(o.challenge, o.realm, origin, this.pgpKeyStore); const r = await signChallenge(o.challenge, o.realm, origin, pgpKeyStore);
this.sendResponse(r); this.sendResponse(r);
} }
loginView(): void { loginView(): void {
document.getElementById('one').style.display = 'none'; document.getElementById('one').style.display = 'none';
document.getElementById('two').style.display = 'block'; document.getElementById('two').style.display = 'block';
this.setState('click to log in with PGP key ' + this.pgpKeyStore.keyId().toHex()); this.setState('click to log in with PGP key ' + pgpKeyStore.keyId().toHex());
} }
async setKey(privateKeyArmored): Promise<boolean> { async setKey(privateKeyArmored): Promise<boolean> {
console.log('settings pk' + privateKeyArmored); console.log('settings pk' + privateKeyArmored);
try { try {
await this.pgpKeyStore.set(privateKeyArmored); await pgpKeyStore.set(privateKeyArmored);
localStorage.setItem(btoa('CICADA_PRIVATE_KEY'), privateKeyArmored);
} catch (e) { } catch (e) {
console.error('failed setting key', e); console.error('failed setting key', e);
return false; return false;
@ -116,4 +126,9 @@ export class AuthService {
this.loginView(); this.loginView();
return true; return true;
} }
logout(): void {
sessionStorage.removeItem(btoa('CICADA_SESSION_TOKEN'));
window.location.reload(true);
}
} }

View File

@ -31,7 +31,7 @@
</form> </form>
</div> </div>
<div id="two" style="display: none" class="card-body p-4"> <div id="two" style="display: none" class="card-body p-4 align-items-center">
<div class="text-center w-75 m-auto"> <div class="text-center w-75 m-auto">
<h4 class="text-dark-50 text-center font-weight-bold">Enter Passphrase</h4> <h4 class="text-dark-50 text-center font-weight-bold">Enter Passphrase</h4>
@ -48,11 +48,20 @@
</div> </div>
</mat-form-field> </mat-form-field>
<button mat-raised-button matRipple color="primary" type="submit" [disabled]="loading"> <div class="form-group mb-0 text-center">
<span *ngIf="loading" class="spinner-border spinner-border-sm mr-1"></span> <button mat-raised-button matRipple color="primary" type="submit">
<!-- <span *ngIf="loading" class="spinner-border spinner-border-sm mr-1"></span>-->
Login Login
</button> </button>
</div>
</form> </form>
<div class="row mt-3">
<div class="col-12 text-center">
<p class="text-muted">Change private key? <a (click)="switchWindows()" class="text-muted ml-1"><b>Enter private key</b></a></p>
</div> <!-- end col-->
</div>
<!-- end row -->
</div> </div>
</div> </div>
</div> </div>

View File

@ -43,6 +43,14 @@ export class AddKeyComponent implements OnInit, AfterViewInit {
this.passphraseForm = this.formBuilder.group({ this.passphraseForm = this.formBuilder.group({
passphrase: ['', Validators.required], passphrase: ['', Validators.required],
}); });
if (this.authService.privateKey !== undefined ) {
this.authService.setKey(this.authService.privateKey).then(r => {
if (this.authService.sessionToken !== undefined) {
this.authService.setState(
'click to perform login ' + this.authService.sessionLoginCount + ' with token ' + this.authService.sessionToken);
}
});
}
} }
get keyFormStub(): any { return this.keyForm.controls; } get keyFormStub(): any { return this.keyForm.controls; }
@ -55,11 +63,10 @@ export class AddKeyComponent implements OnInit, AfterViewInit {
this.loading = true; this.loading = true;
this.authService.setKey(this.keyFormStub.key.value).then(); this.authService.setKey(this.keyFormStub.key.value).then();
this.switchWindows();
} }
login(): void { login(): void {
if (this.passphraseForm.invalid) { return; } // if (this.passphraseForm.invalid) { return; }
this.authService.login(); this.authService.login();
} }

View File

@ -5,8 +5,8 @@ import { AuthComponent } from './auth.component';
import {AddKeyComponent} from './add-key/add-key.component'; import {AddKeyComponent} from './add-key/add-key.component';
const routes: Routes = [ const routes: Routes = [
{ path: '', component: AuthComponent }, { path: '', component: AddKeyComponent },
{ path: 'key', component: AddKeyComponent }, { path: '**', redirectTo: '', pathMatch: 'full'},
]; ];
@NgModule({ @NgModule({

View File

@ -3,7 +3,9 @@
"extends": "./tsconfig.json", "extends": "./tsconfig.json",
"compilerOptions": { "compilerOptions": {
"outDir": "./out-tsc/app", "outDir": "./out-tsc/app",
"types": [] "types": [
"node",
]
}, },
"files": [ "files": [
"src/main.ts", "src/main.ts",