blob: 0c202b0b3fcaa7898b201f469ebcce8c4c6942a6 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080019import IHttpPromiseCallbackArg = angular.IHttpPromiseCallbackArg;
Matteo Scandolo828d1e82017-01-17 14:49:38 -080020import {IXosAppConfig} from '../../../index';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080021export interface IAuthRequestData {
22 username: string;
23 password: string;
24}
25
26export interface IAuthResponseData extends IHttpPromiseCallbackArg<any> {
27 data: {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080028 sessionid: string;
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080029 };
30}
Matteo Scandoloa4a47112016-12-16 10:06:13 -080031
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080032export interface IXosUser {
33 id: number;
34 email: string;
35}
36
Matteo Scandolo0f3692e2017-07-10 14:06:41 -070037export interface IXosRestError {
38 error: string;
39 specific_error: string;
40 fields: any;
41}
42
Matteo Scandoloa4a47112016-12-16 10:06:13 -080043export interface IXosAuthService {
44 login(data: IAuthRequestData): Promise<any>;
45 logout(): Promise<any>;
Matteo Scandolod62ea792016-12-22 14:02:28 -080046 getUser(): any; // NOTE how to define return user || false ???
47 isAuthenticated(): boolean;
Matteo Scandolo0e363772017-01-13 11:41:29 -080048 clearUser(): void;
Matteo Scandolo29edc0f2018-04-26 17:19:10 +020049 isAuthError(error: IXosRestError | string): boolean;
Matteo Scandoloa4a47112016-12-16 10:06:13 -080050}
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080051export class AuthService {
52
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080053 constructor(
54 private $http: angular.IHttpService,
55 private $q: angular.IQService,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080056 private $cookies: angular.cookies.ICookiesService,
Matteo Scandolo0f3692e2017-07-10 14:06:41 -070057 private AppConfig: IXosAppConfig,
58 private $state: angular.ui.IStateService
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080059 ) {
60 }
61
62 public login(data: IAuthRequestData): Promise<any> {
63 const d = this.$q.defer();
Matteo Scandolo1aee1982017-02-17 08:33:23 -080064 this.$http.post(`${this.AppConfig.apiEndpoint}/utility/login`, data)
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080065 .then((res: IAuthResponseData) => {
Matteo Scandoloebe5a222017-02-27 11:09:26 -080066 if (res.status >= 400) {
67 return d.reject(res.data);
68 }
Matteo Scandolo1aee1982017-02-17 08:33:23 -080069 this.$cookies.put('sessionid', res.data.sessionid, {path: '/'});
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080070 d.resolve(res.data);
71 })
72 .catch(e => {
73 d.reject(e);
74 });
75 return d.promise;
76 }
Matteo Scandoloa4a47112016-12-16 10:06:13 -080077
78 public logout(): Promise<any> {
79 const d = this.$q.defer();
Matteo Scandolo1aee1982017-02-17 08:33:23 -080080 this.$http.post(`${this.AppConfig.apiEndpoint}/utility/logout`, {
81 // xoscsrftoken: this.$cookies.get('xoscsrftoken'),
82 // sessionid: this.$cookies.get('sessionid')
Matteo Scandoloa4a47112016-12-16 10:06:13 -080083 })
84 .then(() => {
Matteo Scandolo0e363772017-01-13 11:41:29 -080085 this.clearUser();
Matteo Scandoloa4a47112016-12-16 10:06:13 -080086 d.resolve();
87 })
88 .catch(e => {
89 d.reject(e);
90 });
91 return d.promise;
92 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080093
Matteo Scandolo0e363772017-01-13 11:41:29 -080094 public clearUser(): void {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080095 // this.$cookies.remove('xoscsrftoken', {path: '/'});
96 this.$cookies.remove('sessionid', {path: '/'});
97 // this.$cookies.remove('xosuser', {path: '/'});
Matteo Scandolo0e363772017-01-13 11:41:29 -080098 }
99
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800100 public getUser(): IXosUser {
Matteo Scandolod62ea792016-12-22 14:02:28 -0800101 const user = this.$cookies.get('xosuser');
102 if (angular.isDefined(user)) {
103 return JSON.parse(user);
104 }
105 return;
106 }
107
108 public isAuthenticated(): boolean {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800109 // const token = this.$cookies.get('xoscsrftoken');
110 const session = this.$cookies.get('sessionid');
111 return angular.isDefined(session);
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800112 }
Matteo Scandolo0f3692e2017-07-10 14:06:41 -0700113
Matteo Scandolo29edc0f2018-04-26 17:19:10 +0200114 public isAuthError(res: IXosRestError | string): boolean {
Matteo Scandolo0f3692e2017-07-10 14:06:41 -0700115 let err;
116 if (angular.isString(res)) {
117 try {
118 err = JSON.parse(res);
119 } catch (e) {
120 // NOTE if it's not JSON it means that is not the error we're handling here
121 return;
122 }
123 }
124
125 if (angular.isObject(res)) {
126 err = res;
127 }
128
129 if (err && err.error) {
130 switch (err.error) {
131 case 'XOSPermissionDenied':
Matteo Scandolo29edc0f2018-04-26 17:19:10 +0200132 return true;
Matteo Scandolo0f3692e2017-07-10 14:06:41 -0700133 }
134 }
135 }
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800136}