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 | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 29 | // 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] | 30 | switch (res.status) { |
| 31 | case -1: |
| 32 | case 401: |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 33 | case 403: |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 34 | $cookies.remove('sessionid', {path: '/'}); |
| 35 | $state.go('login'); |
| 36 | return $q.reject(res); |
| 37 | default: |
Matteo Scandolo | 42c6692 | 2017-05-01 17:24:59 -0700 | [diff] [blame] | 38 | return $q.reject(res); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 39 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | return { |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 43 | responseError: checkLogin |
| 44 | }; |
| 45 | } |
| 46 | |
| 47 | export function CredentialsInterceptor($cookies: angular.cookies.ICookiesService) { |
| 48 | return { |
| 49 | request: (req) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 50 | if (!$cookies.get('sessionid')) { |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 51 | return req; |
| 52 | } |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 53 | req.headers['x-sessionid'] = $cookies.get('sessionid'); |
| 54 | req.headers['x-xossession'] = $cookies.get('sessionid'); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 55 | return req; |
| 56 | } |
| 57 | }; |
| 58 | } |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 59 | |
Matteo Scandolo | 42c6692 | 2017-05-01 17:24:59 -0700 | [diff] [blame] | 60 | export function NoHyperlinksInterceptor($q: ng.IQService) { |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 61 | return { |
| 62 | request: (req) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 63 | if (req.url.indexOf('.html') === -1) { |
| 64 | // NOTE force content type to be JSON |
| 65 | req.headers['Content-Type'] = 'application/json'; |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 66 | |
| 67 | if (req.method === 'PUT') { |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 68 | // XosModelStore.search add this value for visualization purpose, |
Matteo Scandolo | 520a8a1 | 2017-03-10 17:31:37 -0800 | [diff] [blame] | 69 | // no one should change models |
| 70 | delete req.data.modelName; |
| 71 | } |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 72 | } |
| 73 | return req; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 74 | }, |
| 75 | response: (res) => { |
| 76 | try { |
| 77 | // NOTE convert res.data from string to JSON |
| 78 | res.data = JSON.parse(res.data); |
| 79 | try { |
| 80 | // NOTE chameleon return everything inside an "items" field |
| 81 | res.data = res.data.items; |
| 82 | } catch (_e) { |
| 83 | res.data = res.data; |
| 84 | } |
| 85 | } catch (e) { |
| 86 | res.data = res.data; |
| 87 | } |
| 88 | return res; |
Matteo Scandolo | 42c6692 | 2017-05-01 17:24:59 -0700 | [diff] [blame] | 89 | }, |
| 90 | responseError: (res) => { |
| 91 | return $q.reject(res.data); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 92 | } |
| 93 | }; |
| 94 | } |