|
| 1 | +var abp = abp || {}; |
| 2 | +(function () { |
| 3 | + |
| 4 | + /* Application paths *****************************************/ |
| 5 | + |
| 6 | + //Current application root path (including virtual directory if exists). |
| 7 | + abp.appPath = abp.appPath || '/'; |
| 8 | + |
| 9 | + /* AUTHORIZATION **********************************************/ |
| 10 | + //Implements Authorization API that simplifies usage of authorization scripts generated by Abp. |
| 11 | + |
| 12 | + abp.auth = abp.auth || {}; |
| 13 | + |
| 14 | + abp.auth.tokenCookieName = 'Abp.AuthToken'; |
| 15 | + abp.auth.tokenHeaderName = 'Authorization'; |
| 16 | + |
| 17 | + abp.auth.setToken = function (authToken, expireDate) { |
| 18 | + abp.utils.setCookieValue(abp.auth.tokenCookieName, authToken, expireDate, abp.appPath); |
| 19 | + }; |
| 20 | + |
| 21 | + abp.auth.getToken = function () { |
| 22 | + return abp.utils.getCookieValue(abp.auth.tokenCookieName); |
| 23 | + } |
| 24 | + |
| 25 | + abp.auth.clearToken = function () { |
| 26 | + abp.auth.setToken(); |
| 27 | + } |
| 28 | + |
| 29 | + /* UTILS ***************************************************/ |
| 30 | + |
| 31 | + abp.utils = abp.utils || {}; |
| 32 | + |
| 33 | + /** |
| 34 | + * Sets a cookie value for given key. |
| 35 | + * This is a simple implementation created to be used by ABP. |
| 36 | + * Please use a complete cookie library if you need. |
| 37 | + * @param {string} key |
| 38 | + * @param {string} value |
| 39 | + * @param {Date} expireDate (optional). If not specified the cookie will expire at the end of session. |
| 40 | + * @param {string} path (optional) |
| 41 | + */ |
| 42 | + abp.utils.setCookieValue = function (key, value, expireDate, path) { |
| 43 | + var cookieValue = encodeURIComponent(key) + '='; |
| 44 | + |
| 45 | + if (value) { |
| 46 | + cookieValue = cookieValue + encodeURIComponent(value); |
| 47 | + } |
| 48 | + |
| 49 | + if (expireDate) { |
| 50 | + cookieValue = cookieValue + "; expires=" + expireDate.toUTCString(); |
| 51 | + } |
| 52 | + |
| 53 | + if (path) { |
| 54 | + cookieValue = cookieValue + "; path=" + path; |
| 55 | + } |
| 56 | + |
| 57 | + document.cookie = cookieValue; |
| 58 | + }; |
| 59 | + |
| 60 | + /** |
| 61 | + * Gets a cookie with given key. |
| 62 | + * This is a simple implementation created to be used by ABP. |
| 63 | + * Please use a complete cookie library if you need. |
| 64 | + * @param {string} key |
| 65 | + * @returns {string} Cookie value or null |
| 66 | + */ |
| 67 | + abp.utils.getCookieValue = function (key) { |
| 68 | + var equalities = document.cookie.split('; '); |
| 69 | + for (var i = 0; i < equalities.length; i++) { |
| 70 | + if (!equalities[i]) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + var splitted = equalities[i].split('='); |
| 75 | + if (splitted.length != 2) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + if (decodeURIComponent(splitted[0]) === key) { |
| 80 | + return decodeURIComponent(splitted[1] || ''); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return null; |
| 85 | + }; |
| 86 | + |
| 87 | + /** |
| 88 | + * Deletes cookie for given key. |
| 89 | + * This is a simple implementation created to be used by ABP. |
| 90 | + * Please use a complete cookie library if you need. |
| 91 | + * @param {string} key |
| 92 | + * @param {string} path (optional) |
| 93 | + */ |
| 94 | + abp.utils.deleteCookie = function (key, path) { |
| 95 | + var cookieValue = encodeURIComponent(key) + '='; |
| 96 | + |
| 97 | + cookieValue = cookieValue + "; expires=" + (new Date(new Date().getTime() - 86400000)).toUTCString(); |
| 98 | + |
| 99 | + if (path) { |
| 100 | + cookieValue = cookieValue + "; path=" + path; |
| 101 | + } |
| 102 | + |
| 103 | + document.cookie = cookieValue; |
| 104 | + } |
| 105 | + |
| 106 | + /* SECURITY ***************************************/ |
| 107 | + abp.security = abp.security || {}; |
| 108 | + abp.security.antiForgery = abp.security.antiForgery || {}; |
| 109 | + |
| 110 | + abp.security.antiForgery.tokenCookieName = 'XSRF-TOKEN'; |
| 111 | + abp.security.antiForgery.tokenHeaderName = 'X-XSRF-TOKEN'; |
| 112 | + |
| 113 | + abp.security.antiForgery.getToken = function () { |
| 114 | + return abp.utils.getCookieValue(abp.security.antiForgery.tokenCookieName); |
| 115 | + }; |
| 116 | + |
| 117 | +})(); |
0 commit comments