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 | /// <reference path="../typings/index.d.ts" /> |
| 20 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 21 | export function interceptorConfig($httpProvider: angular.IHttpProvider, $resourceProvider: angular.resource.IResourceServiceProvider) { |
| 22 | $httpProvider.interceptors.push('UserStatusInterceptor'); |
| 23 | $httpProvider.interceptors.push('CredentialsInterceptor'); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 24 | $httpProvider.interceptors.push('NoHyperlinksInterceptor'); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 25 | } |
| 26 | |
Matteo Scandolo | ebe5a22 | 2017-02-27 11:09:26 -0800 | [diff] [blame] | 27 | export function userStatusInterceptor($state: angular.ui.IStateService, $cookies: ng.cookies.ICookiesService, $q: ng.IQService) { |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 28 | const checkLogin = (res) => { |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 29 | |
| 30 | // NOTE canceled request (as the modeldefs one) have no res |
| 31 | if (angular.isUndefined(res) || res === null) { |
| 32 | return $q.reject(res); |
| 33 | } |
| 34 | |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 35 | // NOTE this interceptor may never be called as the request is not rejected byt the "model-discoverer" service |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 36 | switch (res.status) { |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 37 | case 401: |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 38 | case 403: |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 39 | $cookies.remove('sessionid', {path: '/'}); |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 40 | return $q.reject(res); |
| 41 | default: |
Matteo Scandolo | 42c6692 | 2017-05-01 17:24:59 -0700 | [diff] [blame] | 42 | return $q.reject(res); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 43 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | return { |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 47 | responseError: checkLogin |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | export function CredentialsInterceptor($cookies: angular.cookies.ICookiesService) { |
| 52 | return { |
| 53 | request: (req) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 54 | if (!$cookies.get('sessionid')) { |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 55 | return req; |
| 56 | } |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 57 | req.headers['x-sessionid'] = $cookies.get('sessionid'); |
| 58 | req.headers['x-xossession'] = $cookies.get('sessionid'); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 59 | return req; |
| 60 | } |
| 61 | }; |
| 62 | } |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 63 | |
Matteo Scandolo | 42c6692 | 2017-05-01 17:24:59 -0700 | [diff] [blame] | 64 | export function NoHyperlinksInterceptor($q: ng.IQService) { |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 65 | return { |
| 66 | request: (req) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 67 | if (req.url.indexOf('.html') === -1) { |
| 68 | // NOTE force content type to be JSON |
| 69 | req.headers['Content-Type'] = 'application/json'; |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 70 | |
| 71 | if (req.method === 'PUT') { |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 72 | // XosModelStore.search add this value for visualization purpose, |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 73 | // no one should change models |
| 74 | delete req.data.modelName; |
| 75 | } |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 76 | } |
| 77 | return req; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 78 | }, |
| 79 | response: (res) => { |
| 80 | try { |
| 81 | // NOTE convert res.data from string to JSON |
| 82 | res.data = JSON.parse(res.data); |
| 83 | try { |
| 84 | // NOTE chameleon return everything inside an "items" field |
| 85 | res.data = res.data.items; |
| 86 | } catch (_e) { |
| 87 | res.data = res.data; |
| 88 | } |
| 89 | } catch (e) { |
| 90 | res.data = res.data; |
| 91 | } |
| 92 | return res; |
Matteo Scandolo | 42c6692 | 2017-05-01 17:24:59 -0700 | [diff] [blame] | 93 | }, |
| 94 | responseError: (res) => { |
| 95 | return $q.reject(res.data); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 96 | } |
| 97 | }; |
| 98 | } |