Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 19 | import IHttpPromiseCallbackArg = angular.IHttpPromiseCallbackArg; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 20 | import {IXosAppConfig} from '../../../index'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 21 | export interface IAuthRequestData { |
| 22 | username: string; |
| 23 | password: string; |
| 24 | } |
| 25 | |
| 26 | export interface IAuthResponseData extends IHttpPromiseCallbackArg<any> { |
| 27 | data: { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 28 | sessionid: string; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 29 | }; |
| 30 | } |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 31 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 32 | export interface IXosUser { |
| 33 | id: number; |
| 34 | email: string; |
| 35 | } |
| 36 | |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 37 | export interface IXosRestError { |
| 38 | error: string; |
| 39 | specific_error: string; |
| 40 | fields: any; |
| 41 | } |
| 42 | |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 43 | export interface IXosAuthService { |
| 44 | login(data: IAuthRequestData): Promise<any>; |
| 45 | logout(): Promise<any>; |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 46 | getUser(): any; // NOTE how to define return user || false ??? |
| 47 | isAuthenticated(): boolean; |
Matteo Scandolo | 0e36377 | 2017-01-13 11:41:29 -0800 | [diff] [blame] | 48 | clearUser(): void; |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 49 | handleUnauthenticatedRequest(error: IXosRestError | string): void; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 50 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 51 | export class AuthService { |
| 52 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 53 | constructor( |
| 54 | private $http: angular.IHttpService, |
| 55 | private $q: angular.IQService, |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 56 | private $cookies: angular.cookies.ICookiesService, |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 57 | private AppConfig: IXosAppConfig, |
| 58 | private $state: angular.ui.IStateService |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 59 | ) { |
| 60 | } |
| 61 | |
| 62 | public login(data: IAuthRequestData): Promise<any> { |
| 63 | const d = this.$q.defer(); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 64 | this.$http.post(`${this.AppConfig.apiEndpoint}/utility/login`, data) |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 65 | .then((res: IAuthResponseData) => { |
Matteo Scandolo | ebe5a22 | 2017-02-27 11:09:26 -0800 | [diff] [blame] | 66 | if (res.status >= 400) { |
| 67 | return d.reject(res.data); |
| 68 | } |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 69 | this.$cookies.put('sessionid', res.data.sessionid, {path: '/'}); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 70 | d.resolve(res.data); |
| 71 | }) |
| 72 | .catch(e => { |
| 73 | d.reject(e); |
| 74 | }); |
| 75 | return d.promise; |
| 76 | } |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 77 | |
| 78 | public logout(): Promise<any> { |
| 79 | const d = this.$q.defer(); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 80 | this.$http.post(`${this.AppConfig.apiEndpoint}/utility/logout`, { |
| 81 | // xoscsrftoken: this.$cookies.get('xoscsrftoken'), |
| 82 | // sessionid: this.$cookies.get('sessionid') |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 83 | }) |
| 84 | .then(() => { |
Matteo Scandolo | 0e36377 | 2017-01-13 11:41:29 -0800 | [diff] [blame] | 85 | this.clearUser(); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 86 | d.resolve(); |
| 87 | }) |
| 88 | .catch(e => { |
| 89 | d.reject(e); |
| 90 | }); |
| 91 | return d.promise; |
| 92 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 93 | |
Matteo Scandolo | 0e36377 | 2017-01-13 11:41:29 -0800 | [diff] [blame] | 94 | public clearUser(): void { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 95 | // this.$cookies.remove('xoscsrftoken', {path: '/'}); |
| 96 | this.$cookies.remove('sessionid', {path: '/'}); |
| 97 | // this.$cookies.remove('xosuser', {path: '/'}); |
Matteo Scandolo | 0e36377 | 2017-01-13 11:41:29 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 100 | public getUser(): IXosUser { |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 101 | 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 Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 109 | // const token = this.$cookies.get('xoscsrftoken'); |
| 110 | const session = this.$cookies.get('sessionid'); |
| 111 | return angular.isDefined(session); |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 112 | } |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 113 | |
| 114 | public handleUnauthenticatedRequest(res: IXosRestError | string): void { |
| 115 | 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': |
| 132 | this.clearUser(); |
| 133 | this.$state.go('login'); |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 138 | } |