Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / framework / src / models / authentication.ts
1 /**
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt odlux
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18
19 export type AuthToken = {
20   username: string;
21   access_token: string;
22   token_type: string;
23   /***
24    * datetime the token should expire in unix timestamp 
25    * 
26    * must be in seconds
27    */
28   expires: number;
29   /***
30    * time the token was issued in unix timestamp
31    * 
32    * must be in seconds
33    * 
34    */
35   issued: number;
36 }
37
38 export type AuthPolicy = {
39   path: string;
40   methods: {
41     get?: boolean;
42     post?: boolean;
43     put?: boolean;
44     patch?: boolean;
45     delete?: boolean;
46   }
47 }
48
49 export class User {
50
51   constructor (private _bearerToken: AuthToken) {
52
53   }
54
55   public get user(): string | null {
56     return this._bearerToken && this._bearerToken.username;
57   };
58
59   public get token(): string | null {
60     return this._bearerToken && this._bearerToken.access_token;
61   }
62
63   public get tokenType(): string | null {
64     return this._bearerToken && this._bearerToken.token_type;
65   }
66
67   /***
68    * Time the user should be logged out, in unix timestamp in seconds
69    */
70   public get logoutAt(): number{
71     return this._bearerToken && this._bearerToken.expires;
72   }
73
74    /***
75    * Time the user logged in, in unix timestamp in seconds
76    */
77   public get loginAt(): number{
78     return this._bearerToken && this._bearerToken.issued;
79   }
80
81   public get isValid(): boolean {
82     return (this._bearerToken && (new Date().valueOf()) < this._bearerToken.expires*1000) || false;
83   }
84
85   public toString() {
86     return JSON.stringify(this._bearerToken);
87   }
88
89   public static fromString(data: string) {
90     return new User(JSON.parse(data));
91   }
92
93
94 }