123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- "undefined" != typeof module && "undefined" != typeof exports && module.exports === exports && (module.exports = "ng-token-auth"), angular.module("ng-token-auth", ["ipCookie"]).provider("$auth", function() {
- var t, e;
- return t = {
- "default": {
- apiUrl: "/api",
- signOutUrl: "/auth/sign_out",
- emailSignInPath: "/auth/sign_in",
- emailRegistrationPath: "/auth",
- accountUpdatePath: "/auth",
- accountDeletePath: "/auth",
- confirmationSuccessUrl: function() {
- return window.location.href
- },
- passwordResetPath: "/auth/password",
- passwordUpdatePath: "/auth/password",
- passwordResetSuccessUrl: function() {
- return window.location.href
- },
- tokenValidationPath: "/auth/validate_token",
- proxyIf: function() {
- return !1
- },
- proxyUrl: "/proxy",
- validateOnPageLoad: !0,
- omniauthWindowType: "sameWindow",
- storage: "cookies",
- tokenFormat: {
- "access-token": "{{ token }}",
- "token-type": "Bearer",
- client: "{{ clientId }}",
- expiry: "{{ expiry }}",
- uid: "{{ uid }}"
- },
- parseExpiry: function(t) {
- return 1e3 * parseInt(t.expiry, 10) || null
- },
- handleLoginResponse: function(t) {
- return t.data
- },
- handleAccountUpdateResponse: function(t) {
- return t.data
- },
- handleTokenValidationResponse: function(t) {
- return t.data
- },
- authProviderPaths: {
- github: "/auth/github",
- facebook: "/auth/facebook",
- google: "/auth/google_oauth2"
- }
- }
- }, e = "default", {
- configure: function(n) {
- var r, i, s, a, o, u, c, d, h;
- if (n instanceof Array && n.length) {
- for (a = d = 0, h = n.length; h > d; a = ++d) {
- r = n[a], u = null;
- for (o in r) c = r[o], u = o, 0 === a && (e = u);
- i = angular.copy(t["default"]), s = {}, s[u] = angular.extend(i, r[u]), angular.extend(t, s)
- }
- "default" !== e && delete t["default"]
- } else {
- if (!(n instanceof Object)) throw "Invalid argument: ng-token-auth config should be an Array or Object.";
- angular.extend(t["default"], n)
- }
- return t
- },
- $get: ["$http", "$q", "$location", "ipCookie", "$window", "$timeout", "$rootScope", "$interpolate", function(n) {
- return function(r, i, s, a, o, u, c, d) {
- return {
- header: null,
- dfd: null,
- user: {},
- mustResetPassword: !1,
- listener: null,
- initialize: function() {
- return this.initializeListeners(), this.cancelOmniauthInAppBrowserListeners = function() {}, this.addScopeMethods()
- },
- initializeListeners: function() {
- return this.listener = angular.bind(this, this.handlePostMessage), o.addEventListener ? o.addEventListener("message", this.listener, !1) : void 0
- },
- cancel: function(t) {
- return null != this.requestCredentialsPollingTimer && u.cancel(this.requestCredentialsPollingTimer), this.cancelOmniauthInAppBrowserListeners(), null != this.dfd && this.rejectDfd(t), u(function(t) {
- return function() {
- return t.requestCredentialsPollingTimer = null
- }
- }(this), 0)
- },
- destroy: function() {
- return this.cancel(), o.removeEventListener ? o.removeEventListener("message", this.listener, !1) : void 0
- },
- handlePostMessage: function(t) {
- var e, n;
- return "deliverCredentials" === t.data.message && (delete t.data.message, n = t.data.oauth_registration, delete t.data.oauth_registration, this.handleValidAuth(t.data, !0), c.$broadcast("auth:login-success", t.data), n && c.$broadcast("auth:oauth-registration", t.data)), "authFailure" === t.data.message ? (e = {
- reason: "unauthorized",
- errors: [t.data.error]
- }, this.cancel(e), c.$broadcast("auth:login-error", e)) : void 0
- },
- addScopeMethods: function() {
- return c.user = this.user, c.authenticate = angular.bind(this, this.authenticate), c.signOut = angular.bind(this, this.signOut), c.destroyAccount = angular.bind(this, this.destroyAccount), c.submitRegistration = angular.bind(this, this.submitRegistration), c.submitLogin = angular.bind(this, this.submitLogin), c.requestPasswordReset = angular.bind(this, this.requestPasswordReset), c.updatePassword = angular.bind(this, this.updatePassword), c.updateAccount = angular.bind(this, this.updateAccount), this.getConfig().validateOnPageLoad ? this.validateUser({
- config: this.getSavedConfig()
- }) : void 0
- },
- submitRegistration: function(t, e) {
- var n;
- return null == e && (e = {}), n = this.getResultOrValue(this.getConfig(e.config).confirmationSuccessUrl), angular.extend(t, {
- confirm_success_url: n,
- config_name: this.getCurrentConfigName(e.config)
- }), r.post(this.apiUrl(e.config) + this.getConfig(e.config).emailRegistrationPath, t).success(function(e) {
- return c.$broadcast("auth:registration-email-success", t)
- }).error(function(t) {
- return c.$broadcast("auth:registration-email-error", t)
- })
- },
- submitLogin: function(t, e) {
- return null == e && (e = {}), this.initDfd(), r.post(this.apiUrl(e.config) + this.getConfig(e.config).emailSignInPath, t).success(function(t) {
- return function(n) {
- var r;
- return t.setConfigName(e.config), r = t.getConfig(e.config).handleLoginResponse(n, t), t.handleValidAuth(r), c.$broadcast("auth:login-success", t.user)
- }
- }(this)).error(function(t) {
- return function(e) {
- return t.rejectDfd({
- reason: "unauthorized",
- errors: ["Invalid credentials"]
- }), c.$broadcast("auth:login-error", e)
- }
- }(this)), this.dfd.promise
- },
- userIsAuthenticated: function() {
- return this.retrieveData("auth_headers") && this.user.signedIn && !this.tokenHasExpired()
- },
- requestPasswordReset: function(t, e) {
- var n;
- return null == e && (e = {}), n = this.getResultOrValue(this.getConfig(e.config).passwordResetSuccessUrl), t.redirect_url = n, null != e.config && (t.config_name = e.config), r.post(this.apiUrl(e.config) + this.getConfig(e.config).passwordResetPath, t).success(function(e) {
- return c.$broadcast("auth:password-reset-request-success", t)
- }).error(function(t) {
- return c.$broadcast("auth:password-reset-request-error", t)
- })
- },
- updatePassword: function(t) {
- return r.put(this.apiUrl() + this.getConfig().passwordUpdatePath, t).success(function(t) {
- return function(e) {
- return c.$broadcast("auth:password-change-success", e), t.mustResetPassword = !1
- }
- }(this)).error(function(t) {
- return c.$broadcast("auth:password-change-error", t)
- })
- },
- updateAccount: function(t) {
- return r.put(this.apiUrl() + this.getConfig().accountUpdatePath, t).success(function(t) {
- return function(e) {
- var n, r, i, s, a, o;
- if (s = t.getConfig().handleAccountUpdateResponse(e), n = t.retrieveData("auth_headers"), angular.extend(t.user, s), n) {
- i = {}, o = t.getConfig().tokenFormat;
- for (r in o) a = o[r], n[r] && s[r] && (i[r] = s[r]);
- t.setAuthHeaders(i)
- }
- return c.$broadcast("auth:account-update-success", e)
- }
- }(this)).error(function(t) {
- return c.$broadcast("auth:account-update-error", t)
- })
- },
- destroyAccount: function(t) {
- return r["delete"](this.apiUrl() + this.getConfig().accountUpdatePath, t).success(function(t) {
- return function(e) {
- return t.invalidateTokens(), c.$broadcast("auth:account-destroy-success", e)
- }
- }(this)).error(function(t) {
- return c.$broadcast("auth:account-destroy-error", t)
- })
- },
- authenticate: function(t, e) {
- return null == e && (e = {}), null == this.dfd && (this.setConfigName(e.config), this.initDfd(), this.openAuthWindow(t, e)), this.dfd.promise
- },
- setConfigName: function(t) {
- return null == t && (t = e), this.persistData("currentConfigName", t, t)
- },
- openAuthWindow: function(t, e) {
- var n, r;
- if (r = this.getConfig(e.config).omniauthWindowType, n = this.buildAuthUrl(r, t, e), "newWindow" === r) return this.requestCredentialsViaPostMessage(this.createPopup(n));
- if ("inAppBrowser" === r) return this.requestCredentialsViaExecuteScript(this.createPopup(n));
- if ("sameWindow" === r) return this.visitUrl(n);
- throw 'Unsupported omniauthWindowType "#{omniauthWindowType}"'
- },
- visitUrl: function(t) {
- return o.location.replace(t)
- },
- buildAuthUrl: function(t, e, n) {
- var r, i, s, a;
- null == n && (n = {}), r = this.getConfig(n.config).apiUrl, r += this.getConfig(n.config).authProviderPaths[e], r += "?auth_origin_url=" + encodeURIComponent(o.location.href), s = angular.extend({}, n.params || {}, {
- omniauth_window_type: t
- });
- for (i in s) a = s[i], r += "&", r += encodeURIComponent(i), r += "=", r += encodeURIComponent(a);
- return r
- },
- requestCredentialsViaPostMessage: function(t) {
- return t.closed ? this.handleAuthWindowClose(t) : (t.postMessage("requestCredentials", "*"), this.requestCredentialsPollingTimer = u(function(e) {
- return function() {
- return e.requestCredentialsViaPostMessage(t)
- }
- }(this), 500))
- },
- requestCredentialsViaExecuteScript: function(t) {
- var e, n;
- return this.cancelOmniauthInAppBrowserListeners(), e = this.handleAuthWindowClose.bind(this, t), n = this.handleLoadStop.bind(this, t), t.addEventListener("loadstop", n), t.addEventListener("exit", e), this.cancelOmniauthInAppBrowserListeners = function() {
- return t.removeEventListener("loadstop", n), t.removeEventListener("exit", e)
- }
- },
- handleLoadStop: function(t) {
- return n = this, t.executeScript({
- code: "requestCredentials()"
- }, function(e) {
- var r, i;
- return r = e[0], r ? (i = new Event("message"), i.data = r, n.cancelOmniauthInAppBrowserListeners(), o.dispatchEvent(i), n.initDfd(), t.close()) : void 0
- })
- },
- handleAuthWindowClose: function(t) {
- return this.cancel({
- reason: "unauthorized",
- errors: ["User canceled login"]
- }), this.cancelOmniauthInAppBrowserListeners, c.$broadcast("auth:window-closed")
- },
- createPopup: function(t) {
- return o.open(t, "_blank")
- },
- resolveDfd: function() {
- return this.dfd.resolve(this.user), u(function(t) {
- return function() {
- return t.dfd = null, c.$$phase ? void 0 : c.$digest()
- }
- }(this), 0)
- },
- buildQueryString: function(t, e) {
- var n, r, i, s;
- i = [];
- for (r in t) s = t[r], r = e ? e + "[" + r + "]" : r, n = angular.isObject(s) ? this.buildQueryString(s, r) : r + "=" + encodeURIComponent(s), i.push(n);
- return i.join("&")
- },
- parseLocation: function(t) {
- var e, n, r, i;
- i = t.substring(1).split("&"), n = {}, r = void 0, e = void 0;
- for (e in i) e = e, "" !== i[e] && (r = i[e].split("="), n[decodeURIComponent(r[0])] = decodeURIComponent(r[1]));
- return n
- },
- validateUser: function(t) {
- var e, n, r, i, a, o, u, d, h;
- return null == t && (t = {}), n = t.config, null == this.dfd && (this.initDfd(), this.userIsAuthenticated() ? this.resolveDfd() : (o = s.search(), i = this.parseLocation(window.location.search), a = 0 === Object.keys(o).length ? i : o, u = a.auth_token || a.token, void 0 !== u ? (e = a.client_id, d = a.uid, r = a.expiry, n = a.config, this.setConfigName(n), this.mustResetPassword = a.reset_password, this.firstTimeLogin = a.account_confirmation_success, this.oauthRegistration = a.oauth_registration,
- this.setAuthHeaders(this.buildAuthHeaders({
- token: u,
- clientId: e,
- uid: d,
- expiry: r
- })),
- h = s.path() || "/", ["token", "client_id", "uid", "expiry", "config", "reset_password", "account_confirmation_success", "oauth_registration"].forEach(function(t) {
- return delete a[t]
- }),
- Object.keys(a).length > 0 && (h += "?" + this.buildQueryString(a)), s.url(h)) : this.retrieveData("currentConfigName") && (n = this.retrieveData("currentConfigName")),
- isEmpty(this.retrieveData("auth_headers")) ? (this.rejectDfd({
- reason: "unauthorized",
- errors: ["No credentials"]
- }), c.$broadcast("auth:invalid")) : this.tokenHasExpired() ? (c.$broadcast("auth:session-expired"),
- this.rejectDfd({
- reason: "unauthorized",
- errors: ["Session expired."]
- })) : this.validateToken({
- config: n
- }))), this.dfd.promise
- },
- validateToken: function(t) {
- return null == t && (t = {}), this.tokenHasExpired() ? this.rejectDfd({
- reason: "unauthorized",
- errors: ["Expired credentials"]
- }) : r.get(this.apiUrl(t.config) + this.getConfig(t.config).tokenValidationPath).success(function(e) {
- return function(n) {
- var r;
- return r = e.getConfig(t.config).handleTokenValidationResponse(n), e.handleValidAuth(r), e.firstTimeLogin && c.$broadcast("auth:email-confirmation-success", e.user), e.oauthRegistration && c.$broadcast("auth:oauth-registration", e.user), e.mustResetPassword && c.$broadcast("auth:password-reset-confirm-success", e.user), c.$broadcast("auth:validation-success", e.user)
- }
- }(this)).error(function(t) {
- return function(e) {
- return t.firstTimeLogin && c.$broadcast("auth:email-confirmation-error", e), t.mustResetPassword && c.$broadcast("auth:password-reset-confirm-error", e), c.$broadcast("auth:validation-error", e), t.rejectDfd({
- reason: "unauthorized",
- errors: e.errors
- })
- }
- }(this))
- },
- tokenHasExpired: function() {
- var t, e;
- return t = this.getExpiry(), e = (new Date).getTime(), t && e > t
- },
- getExpiry: function() {
- return this.getConfig().parseExpiry(this.retrieveData("auth_headers") || {})
- },
- invalidateTokens: function() {
- var t, e, n;
- n = this.user;
- for (t in n) e = n[t], delete this.user[t];
- return this.deleteData("currentConfigName"), null != this.timer && u.cancel(this.timer), this.deleteData("auth_headers")
- },
- signOut: function() {
- return r["delete"](this.apiUrl() + this.getConfig().signOutUrl).success(function(t) {
- return function(e) {
- return t.invalidateTokens(), c.$broadcast("auth:logout-success")
- }
- }(this)).error(function(t) {
- return function(e) {
- return t.invalidateTokens(), c.$broadcast("auth:logout-error", e)
- }
- }(this))
- },
- handleValidAuth: function(t, e) {
- return null == e && (e = !1), null != this.requestCredentialsPollingTimer && u.cancel(this.requestCredentialsPollingTimer), this.cancelOmniauthInAppBrowserListeners(), angular.extend(this.user, t), this.user.signedIn = !0, this.user.configName = this.getCurrentConfigName(), e && this.setAuthHeaders(this.buildAuthHeaders({
- token: this.user.auth_token,
- clientId: this.user.client_id,
- uid: this.user.uid,
- expiry: this.user.expiry
- })), this.resolveDfd()
- },
- buildAuthHeaders: function(t) {
- var e, n, r, i;
- e = {}, i = this.getConfig().tokenFormat;
- for (n in i) r = i[n], e[n] = d(r)(t);
- return e
- },
- persistData: function(t, e, n) {
- if (this.getConfig(n).storage instanceof Object) return this.getConfig(n).storage.persistData(t, e, this.getConfig(n));
- switch (this.getConfig(n).storage) {
- case "localStorage":
- return o.localStorage.setItem(t, JSON.stringify(e));
- default:
- return a(t, e, {
- path: "/",
- expires: 9999,
- expirationUnit: "days"
- })
- }
- },
- retrieveData: function(t) {
- if (this.getConfig().storage instanceof Object) return this.getConfig().storage.retrieveData(t);
- switch (this.getConfig().storage) {
- case "localStorage":
- return JSON.parse(o.localStorage.getItem(t));
- default:
- return a(t)
- }
- },
- deleteData: function(t) {
- switch (this.getConfig().storage instanceof Object && this.getConfig().storage.deleteData(t), this.getConfig().storage) {
- case "localStorage":
- return o.localStorage.removeItem(t);
- default:
- return a.remove(t, {
- path: "/"
- })
- }
- },
- setAuthHeaders: function(t) {
- var e, n, r, i;
- return n = angular.extend(this.retrieveData("auth_headers") || {}, t), i = this.persistData("auth_headers", n), e = this.getExpiry(), r = (new Date).getTime(), e > r && (null != this.timer && u.cancel(this.timer), this.timer = u(function(t) {
- return function() {
- return t.validateUser({
- config: t.getSavedConfig()
- })
- }
- }(this), parseInt(e - r))), i
- },
- initDfd: function() {
- return this.dfd = i.defer()
- },
- rejectDfd: function(t) {
- return this.invalidateTokens(), null != this.dfd ? (this.dfd.reject(t), u(function(t) {
- return function() {
- return t.dfd = null
- }
- }(this), 0)) : void 0
- },
- apiUrl: function(t) {
- return this.getConfig(t).proxyIf() ? this.getConfig(t).proxyUrl : this.getConfig(t).apiUrl
- },
- getConfig: function(e) {
- return t[this.getCurrentConfigName(e)]
- },
- getResultOrValue: function(t) {
- return "function" == typeof t ? t() : t
- },
- getCurrentConfigName: function(t) {
- return t || this.getSavedConfig()
- },
- getSavedConfig: function() {
- var t, n, r, i;
- t = void 0, i = "currentConfigName", r = !1;
- try {
- r = !! o.localStorage
- } catch (s) {
- n = s
- }
- return r && null == t && (t = JSON.parse(o.localStorage.getItem(i))), null == t && (t = a(i)), t || e
- }
- }
- }
- }(this)]
- }
- }).config(["$httpProvider", function(t) {
- var e, n, r;
- return n = function(t, e) {
- var n, r;
- return r = Number(t.getExpiry()), n = Number(t.getConfig().parseExpiry(e || {})), n >= r
- }, r = function(t, e) {
- var r, i, s, a;
- i = {}, a = t.getConfig().tokenFormat;
- for (r in a) {s = a[r];
- // i[r]=e.headers(r)}
- // i[r]=a[r]
- // }
- if(e.headers(r)){i[r]=e.headers(r)}}
- // e.headers(r) && (i[r] = e.headers(r));
- return n(t, i) ? t.setAuthHeaders(i) : void 0
- }, t.interceptors.push(["$injector", function(t) {
- return {
- request: function(e) {
- return t.invoke(["$http", "$auth", function(t, n) {
- var r, i, s, a;
- if (e.url.match(n.apiUrl())) {
- s = n.retrieveData("auth_headers"), a = [];
- for (r in s) i = s[r], a.push(e.headers[r] = i);
- return a
- }
- }]), e
- },
- response: function(e) {
- return t.invoke(["$http", "$auth", function(t, n) {
- return e.config.url.match(n.apiUrl()) ? r(n, e) : void 0
- }]), e
- },
- responseError: function(e) {
- return t.invoke(["$http", "$auth", function(t, n) {
- return e.config.url.match(n.apiUrl()) ? r(n, e) : void 0
- }]), t.get("$q").reject(e)
- }
- }
- }]), e = ["get", "post", "put", "patch", "delete"], angular.forEach(e, function(e) {
- var n;
- return null == (n = t.defaults.headers)[e] && (n[e] = {}), t.defaults.headers[e]["If-Modified-Since"] = "Mon, 26 Jul 1997 05:00:00 GMT"
- })
- }]).run(["$auth", "$window", "$rootScope", function(t, e, n) {
- return t.initialize()
- }]), window.isOldIE = function() {
- var t, e, n;
- return e = !1, t = navigator.userAgent.toLowerCase(), t && -1 !== t.indexOf("msie") && (n = parseInt(t.split("msie")[1]), 10 > n && (e = !0)), e
- }, window.isIE = function() {
- var t;
- return t = navigator.userAgent.toLowerCase(), t && -1 !== t.indexOf("msie") || !! navigator.userAgent.match(/Trident.*rv\:11\./)
- }, window.isEmpty = function(t) {
- var e, n;
- if (!t) return !0;
- if (t.length > 0) return !1;
- if (0 === t.length) return !0;
- for (e in t) if (n = t[e], Object.prototype.hasOwnProperty.call(t, e)) return !1;
- return !0
- };
|