Odlux Update
[ccsdk/features.git] / sdnr / wt / odlux / framework / src / services / restService.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 import { ReplaceAction } from '../actions/navigationActions';
20 import { AddErrorInfoAction } from '../actions/errorActions';
21
22 import { storeService } from './storeService';
23
24 const baseUri = `${ window.location.origin }`;
25 const absUrlPattern = /^https?:\/\//;
26
27 export const formEncode = (params: { [key: string]: string | number }) => Object.keys(params).map((key) => {
28   return encodeURIComponent(key) + '=' + encodeURIComponent(params[key].toString());
29 }).join('&');
30
31 const wildcardToRegexp = (pattern: string) =>  {
32   return new RegExp('^' + pattern.split(/\*\*/).map((p) => p.split(/\*+/).map((i) => i.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')).join('^[/]')).join('.*') + '$');
33 };
34
35 export const getAccessPolicyByUrl = (url: string) => {
36   const result = {
37     GET : false,
38     POST: false,
39     PUT: false,
40     PATCH: false,
41     DELETE: false,
42   };
43   
44   if (!storeService.applicationStore) return result;
45
46   const { state: { framework: { applicationState: { enablePolicy }, authenticationState: { policies } } } } = storeService.applicationStore!;
47   
48   result.GET = true;
49   result.POST = true;
50   result.PUT = true;
51   result.PATCH = true;
52   result.DELETE = true; 
53
54   if (!enablePolicy || !policies || policies.length === 0) return result;
55
56   policies.forEach(p => {
57     const re = wildcardToRegexp(p.path);
58     if (re.test(url)) {
59       result.GET = p.methods.get != null ? p.methods.get : result.GET ;
60       result.POST = p.methods.post != null ? p.methods.post : result.POST ;
61       result.PUT = p.methods.put != null ? p.methods.put : result.PUT ;
62       result.PATCH = p.methods.patch != null ? p.methods.patch : result.PATCH ;
63       result.DELETE = p.methods.delete != null ? p.methods.delete : result.DELETE ;
64     }
65   }); 
66
67   return result;
68
69 };
70
71 /** Sends a rest request to the given path. 
72  * @returns The data, or null it there was any error
73  */
74 export async function requestRest<TData>(path: string = '', init: RequestInit = {}, authenticate: boolean = true, isResource: boolean = false): Promise<TData | null | undefined> {
75   const res = await requestRestExt<TData>(path, init, authenticate, isResource);
76   if (res && res.status >= 200 && res.status < 300) {
77     return res.data;
78   }
79   return null;
80 }
81
82 /** Sends a rest request to the given path and reports the server state. 
83  *  @returns An object with the server state, a message and the data or undefined in case of a json parse error.
84  */
85 export async function requestRestExt<TData>(path: string = '', init: RequestInit = {}, authenticate: boolean = true, isResource: boolean = false): Promise<{ status: number; message?: string; data: TData | null | undefined }> {
86   const result: { status: number; message?: string; data: TData | null } = {
87     status: -1,
88     data: null,
89   };
90   const isAbsUrl = absUrlPattern.test(path);
91   const uri = isAbsUrl ? path : isResource ? path.replace(/\/{2,}/i, '/') : (baseUri) + ('/' + path).replace(/\/{2,}/i, '/');
92   init = {
93     'method': 'GET',
94     ...init,
95     headers: {
96       'Content-Type': 'application/json',
97       'Accept': 'application/json',
98       ...init.headers,
99     },
100   };
101   if (!isAbsUrl && authenticate && storeService.applicationStore) {
102     const { state: { framework: { authenticationState: { user } } } } = storeService.applicationStore;
103     // do not request if the user is not valid
104
105     if (!user || !user.isValid) {
106       return {
107         ...result,
108         message: 'User is not valid or not logged in.',
109       };
110     }
111     (init.headers = {
112       ...init.headers,
113       'Authorization': `${user.tokenType} ${user.token}`,
114       //'Authorization': 'Basic YWRtaW46YWRtaW4='
115     });
116   }
117
118   const fetchResult = await fetch(uri, init);
119
120   if (fetchResult.status === 403) {
121     storeService.applicationStore && storeService.applicationStore.dispatch(new AddErrorInfoAction({ title: 'Forbidden', message:'Status: [403], access denied.' }));
122     return {
123       ...result,
124       status: 403,
125       message: 'Forbidden.',
126     };
127   } else if (fetchResult.status === 401) {
128     storeService.applicationStore && storeService.applicationStore.dispatch(new ReplaceAction(`/login?returnTo=${storeService.applicationStore.state.framework.navigationState.pathname}`));
129     return {
130       ...result,
131       status: 401,
132       message: 'Authentication requested by server.',
133     };
134   }
135   const contentType = fetchResult.headers.get('Content-Type') || fetchResult.headers.get('content-type');
136   const isJson = contentType && (contentType.toLowerCase().startsWith('application/json') || contentType.toLowerCase().startsWith('application/yang-data+json'));
137   try {
138     const data = (isJson ? await fetchResult.json() : await fetchResult.text()) as TData;
139     return {
140       ...result,
141       status: fetchResult.status,
142       message: fetchResult.statusText,
143       data: data,
144     };
145   } catch (error) {
146     return {
147       ...result,
148       status: fetchResult.status,
149       message: error && error.message || String(error),
150       data: undefined,
151     };
152   }
153 }