update odlux sources
[ccsdk/features.git] / sdnr / wt / odlux / framework / src / actions / 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 import { Dispatch } from '../flux/store';
19 import { Action } from '../flux/action';
20 import { AuthPolicy, User } from '../models/authentication';
21 import { GeneralSettings, Settings } from '../models/settings';
22 import { saveInitialSettings, SetGeneralSettingsAction, setGeneralSettingsAction } from './settingsAction';
23 import { endWebsocketSession } from '../services/notificationService';
24 import { endUserSession, startUserSession } from '../services/userSessionService';
25 import { IApplicationStoreState } from '../store/applicationStore';
26
27 export class UpdateUser extends Action {
28
29   constructor (public user?: User) {
30     super();
31   }
32 }
33
34 export class UpdatePolicies extends Action {
35
36   constructor (public authPolicies?: AuthPolicy[]) {
37     super();
38   }
39 }
40
41
42 export const loginUserAction = (user?: User) => (dispatcher: Dispatch) =>{
43   
44   dispatcher(new UpdateUser(user));
45   if(user){
46     startUserSession(user);
47     loadUserSettings(user, dispatcher);
48     localStorage.setItem("userToken", user.toString());
49   }
50 }
51
52 export const logoutUser = () => (dispatcher: Dispatch, getState: () => IApplicationStoreState) =>{
53
54   const {framework:{applicationState:{ authentication }, authenticationState: {user}}} = getState();
55   
56   dispatcher(new UpdateUser(undefined));
57   dispatcher(new SetGeneralSettingsAction(null));
58   endWebsocketSession();
59   endUserSession();
60   localStorage.removeItem("userToken");
61
62
63   //only call if a user is currently logged in
64   if (authentication === "oauth" && user) {
65
66     const url = window.location.origin;
67     window.location.href=`${url}/oauth/logout`;
68   }
69 }
70
71 const loadUserSettings = (user: User | undefined, dispatcher: Dispatch) =>{
72
73
74   //fetch used, because state change for user login is not done when frameworks restRequest call is started (and is accordingly undefined -> /userdata call yields 401, unauthorized) and triggering an action from inside the handler / login event is impossible
75   //no timeout used, because it's bad practise to add a timeout to hopefully avoid a race condition
76   //hence, fetch used to simply use supplied user data for getting settings
77
78   if(user && user.isValid){
79
80     fetch("/userdata", {
81       method: 'GET', 
82       headers: {
83         'Content-Type': 'application/json',
84         'Accept': 'application/json',
85         'Authorization': `${user.tokenType} ${user.token}`
86       }
87     }).then((res: Response)=>{
88       if(res.status==200){
89         return res.json();
90       }else{
91         return null;
92       }
93     }).then((result:Settings)=>{
94         dispatcher(saveInitialSettings(result));
95     })
96   }  
97 }