From 02ef15652d8354d19cf764cfafb0444fad9a3e75 Mon Sep 17 00:00:00 2001
From: Chris Hines <chris.hines@monash.edu>
Date: Tue, 22 Sep 2020 12:01:08 +1000
Subject: [PATCH] add an expiry field to the Identity (corresponding to the
 certificate expiry) and display on the account info page

---
 .../accountinfo/accountinfo.component.html    |  3 +++
 src/app/accountinfo/accountinfo.component.ts  | 23 +++++++++++++++++++
 src/app/authorisation.service.ts              | 14 +++++++++--
 src/app/computesites.service.ts               |  2 +-
 src/app/identity.ts                           |  6 +++--
 5 files changed, 43 insertions(+), 5 deletions(-)

diff --git a/src/app/accountinfo/accountinfo.component.html b/src/app/accountinfo/accountinfo.component.html
index e59a260..8aa198a 100644
--- a/src/app/accountinfo/accountinfo.component.html
+++ b/src/app/accountinfo/accountinfo.component.html
@@ -1,4 +1,7 @@
 <div  fxLayout="column" fxLayoutAlign="start none" style="width: 100%" class="strudel-accountinfo-typography">
+    <div *ngIf="nowSeconds(identity$ | async) ; let time">
+        Login certificates expire in {{ time }}
+    </div>
     <div *ngIf="identity$.value !== null && identity$.value !== undefined"> 
         <!--<div *ngIf="identity$.value.systemalerts.value !== null">-->
             <div *ngFor="let h of (identity$.value.systemalerts | async)">
diff --git a/src/app/accountinfo/accountinfo.component.ts b/src/app/accountinfo/accountinfo.component.ts
index 2992e72..af15fc4 100644
--- a/src/app/accountinfo/accountinfo.component.ts
+++ b/src/app/accountinfo/accountinfo.component.ts
@@ -67,4 +67,27 @@ export class AccountinfoComponent implements OnInit {
     }
   }
 
+  nowSeconds(id: Identity) {
+    let seconds = (id.expiry - Date.now().valueOf())/1000;
+    console.log('seconds remaining',seconds);
+    let remaining = this.secondsToHms(seconds);
+    console.log('login seconds remaining',remaining);
+    return remaining;
+  }
+  secondsToHms(d: number) {
+    var sign = ""
+    if (d < 0) {
+      d = -d;
+      sign="-"
+    }
+    
+    var h: number = Math.floor(d / 3600);
+    var m: number = Math.floor(d % 3600 / 60);
+    var s: number = Math.floor(d % 3600 % 60);
+
+    var hDisplay = h > 0 ? h + (h == 1 ? " hour " : " hours ") : "";
+    var mDisplay = m > 0 ? m + (m == 1 ? " minute " : " minutes ") : "";
+    return sign + hDisplay + mDisplay ; 
+  }
+
 }
diff --git a/src/app/authorisation.service.ts b/src/app/authorisation.service.ts
index e00fcf8..1104a67 100644
--- a/src/app/authorisation.service.ts
+++ b/src/app/authorisation.service.ts
@@ -207,14 +207,13 @@ public getKeys(id?: Identity) {
      catchError((e) => { this.querySshAgentError(e); return of([])}),
      tap((resp) => { console.log('agent contents',resp) }),
      tap((resp) => { console.log('agent contents',resp) }),
+     switchMap((resp) => of(this.addExpiryField(resp))),
      tap((resp) => { 
        if (this.agentContents.value !== null && this.agentContents.value.length > resp.length) {
          this.notifications.notify("Your login expired. Please login again");
-         console.log('login expired');
        } else {
          this.notifications.notify("");
        };
-       console.log('updating agent contents');
        this.agentContents.next(resp)
      }),
      catchError((e) => { console.error('updateAGentContents error',e) ; return of([])})
@@ -223,6 +222,17 @@ public getKeys(id?: Identity) {
    return agentpipe$
  }
 
+ private addExpiryField(resp): any[] {
+  var res: any[]
+  res = []
+  for (let id of resp) {
+    var validstr: String;
+    validstr = id.Valid[0];
+    id.expiry = Date.parse(validstr.split(" ")[3])
+    res.push(id);
+  }
+  return res
+ }
 
  private killAgent() {
    this.notifications.notify("Logging out")
diff --git a/src/app/computesites.service.ts b/src/app/computesites.service.ts
index 2a1e995..42b0499 100644
--- a/src/app/computesites.service.ts
+++ b/src/app/computesites.service.ts
@@ -204,7 +204,7 @@ export class ComputesitesService {
         let principals = this.siteMatch(certs[i],cs);
         for (let principal of principals) {
           if (principal != null) {
-              let id = new Identity(principal,cs);
+              let id = new Identity(principal,cs,certs[i].expiry);
               identities.push(id);
               if (cs.appCatalogUri != null || cs.appCatalogCmd != null) {
                 appidentities.push(id);
diff --git a/src/app/identity.ts b/src/app/identity.ts
index a037fbb..ac86100 100644
--- a/src/app/identity.ts
+++ b/src/app/identity.ts
@@ -12,7 +12,8 @@ export class Identity {
   accountalerts: BehaviorSubject<Health[]>;
   joblist: BehaviorSubject<Job[]>;
   quotas: any[];
-  constructor( username: string, site: Computesite) {
+  expiry: number;
+  constructor( username: string, site: Computesite, expiry: number) {
     this.username = username;
     this.site = site;
     this.keyCerts = [];
@@ -20,10 +21,11 @@ export class Identity {
     this.accountalerts = new BehaviorSubject<Health[]>(null);
     this.joblist = new BehaviorSubject<Job[]>([]);
     this.quotas = [];
+    this.expiry = expiry;
   }
 
   copy_skip_catalog(): Identity {
-    let id = new Identity(null,null);
+    let id = new Identity(null,null,null);
     id.username = this.username;
     id.systemalerts = null;
     id.accountalerts = null;
-- 
GitLab