39f407e4047cac9a2c8dc72af0f4b6821fdcf6e5
[ccsdk/features.git] / sdnr / wt / odlux / framework / src / services / authenticationService.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 import { AuthPolicy, AuthToken } from "../models/authentication";
19 import { ExternalLoginProvider } from "../models/externalLoginProvider";
20
21 import { requestRest, formEncode, requestRestExt } from "./restService";
22
23 type AuthTokenResponse = {
24   access_token: string;
25   token_type: string;
26   expires_at: number;
27   issued_at: number;
28 }
29
30 class AuthenticationService {
31   public async getAvaliableExteralProvider() {
32     const result = await requestRest<ExternalLoginProvider[]>(`oauth/providers`, {
33       method: "GET",
34       headers: {
35         'Content-Type': 'application/x-www-form-urlencoded'
36       },
37     }, false);
38     return result;
39   }
40
41   public async authenticateUserOAuth(email: string, password: string, scope: string): Promise<AuthToken | null> {
42     const result = await requestRest<AuthTokenResponse>(`oauth/login`, {
43       method: "POST",
44       headers: {
45         'Content-Type': 'application/x-www-form-urlencoded'
46       },
47       body: formEncode({
48         grant_type: "password",
49         username: email,
50         password: password,
51         scope: scope
52       })
53     }, false);
54
55    
56     return result && {
57       username: email,
58       access_token: result.access_token,
59       token_type: result.token_type,
60       expires: result.expires_at,
61       issued: result.issued_at
62     } || null;
63   }
64
65    public async authenticateUserBasicAuth(email: string, password: string, scope: string): Promise<AuthToken | null> {
66     const result = await requestRest<string>(`rests/data/network-topology:network-topology/topology=topology-netconf?fields=node(node-id)`, {
67       method: "GET",
68       headers: {
69         'Authorization':  "Basic " + btoa(email + ":" + password)
70       },
71     }, false);
72
73     if (result) {
74       return {
75           username: email,
76           access_token:  btoa(email + ":" + password),
77           token_type: "Basic",
78           expires: (new Date()).valueOf() / 1000 + 86400, // 1 day
79           issued: (new Date()).valueOf() / 1000
80       }
81     }
82     return null;
83   }
84
85   public async getAccessPolicies(){
86     return await requestRest<AuthPolicy[]>(`oauth/policies`, { method: "GET" }, true);
87   }
88
89   public async getServerReadyState(){
90
91     const result = await fetch("/ready", {method: "GET"});
92     return result.status == (200 || 304) ? true : false;
93   }
94 }
95
96 export const authenticationService = new AuthenticationService();
97 export default authenticationService;