angular-eha.couchdb-auth
Advanced tools
Comparing version 0.3.1 to 0.4.0
{ | ||
"name": "angular-eha.couchdb-auth", | ||
"description": "CouchDB auth wrapper", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"authors": [ | ||
@@ -6,0 +6,0 @@ "Matt Richards <matt@lucidmoon.co.uk>" |
@@ -31,3 +31,3 @@ ;(function() { | ||
return $q.when(Restangular | ||
.all('_session') | ||
.all(options.sessionEndpoint) | ||
.customGET()); | ||
@@ -38,3 +38,3 @@ } | ||
return $q.when(Restangular | ||
.all('_session') | ||
.all(options.sessionEndpoint) | ||
.customPOST({ | ||
@@ -90,3 +90,3 @@ name: user.username, | ||
return $q.when(Restangular | ||
.all('_session') | ||
.all(options.sessionEndpoint) | ||
.remove()) | ||
@@ -212,3 +212,4 @@ .then(clearLocalUser) | ||
localStorageStoreName: 'auth', | ||
adminRoles: ['_admin'] | ||
adminRoles: ['_admin'], | ||
sessionEndpoint: '_session' | ||
}; | ||
@@ -215,0 +216,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(){"use strict";function a(a,b,c,d,e,f){function g(){return d.when(b.all("_session").customGET())}function h(a){return d.when(b.all("_session").customPOST({name:a.username,password:a.password})).then(function(a){return a.plain()}).then(s).then(function(a){return g().then(function(b){return angular.extend(b.plain(),a)})}).then(s).then(function(a){return a&&a.ok?(u.$broadcast("authenticationStateChange"),c.log("couchdb:login:success",a),q(a)):(c.log("couchdb:login:failure:unknown"),d.reject(new Error))})["catch"](function(a){return 401===a.status?(c.log("couchdb:login:failure:invalid-credentials",a),d.reject(new Error("Invalid Credentials"))):(c.log("couchdb:login:failure:unknown",a),d.reject(new Error(a)))})}function i(){return t=null,e.removeItem("user")}function j(a){return e.setItem("user",a)}function k(){return e.getItem("user")}function l(){return d.when(b.all("_session").remove()).then(i)["finally"](function(){u.$broadcast("authenticationStateChange")})}function m(a){return a.token&&a.password?d.when(b.all("reset-password").customPOST({token:a.token,password:a.password})):a.email&&a.callbackUrl?d.when(b.all("reset-password").customPOST({email:a.email,callbackUrl:a.callbackUrl})):d.reject("You must provide both email and callbackUrl properties in the payload")}function n(){return d.reject("NOT_IMPLEMENTED")}function o(){return d.reject("NOT_IMPLEMENTED")}function p(){return d.reject("NOT_IMPLEMENTED")}function q(b){return b.hasRole=function(a){var b=this;if(angular.isArray(a)){var c=a.filter(function(a){return b.roles.indexOf(a)>-1});return!!c.length}return angular.isString(a)?this.roles.indexOf(a)>-1:void 0},b.isAdmin=function(){return this.hasRole(a.adminRoles)},b}function r(){return t?d.when(q(t)):k().then(function(a){return a?(t=a,q(a)):d.reject("User not found")}).then(function(a){return g().then(function(){return a})})["catch"](function(a){return d.reject(a)})}function s(a){return a?(t=a,j(a)):void d.reject("No user found")}var t,u=f.$new(!0);return{signIn:h,signOut:l,resetPassword:m,accounts:{add:n,update:o,remove:p},getSession:g,getCurrentUser:r,on:u.$on.bind(u),trigger:u.$broadcast.bind(u)}}var b=angular.module("eha.couchdb-auth.auth.service",["restangular","LocalForageModule","ngCookies"]);b.provider("ehaCouchDbAuthService",["$localForageProvider","ehaCouchDbAuthHttpInterceptorProvider","$httpProvider",function(b,c,d){function e(a){return a.charAt(0).toUpperCase()+a.slice(1)}function f(a){var b=[a];return a.indexOf("-")>-1?b=a.split("-"):a.indexOf("_")>-1&&(b=a.split("_")),b=b.map(e),b.join("")}function g(a,b,c){return a.getCurrentUser().then(function(d){return d&&!d.hasRole(c)?(a.trigger("unauthorized"),b.reject("unauthorized")):d})["catch"](function(){return a.trigger("unauthenticated"),b.reject("unauthenticated")})}var h={localStorageNamespace:"eha",localStorageStoreName:"auth",adminRoles:["_admin"]};this.config=function(a){h=angular.extend(h,a),b.config({name:h.localStorageNamespace,storeName:h.localStorageStoreName}),a.interceptor&&(c.config({url:a.url,hosts:a.interceptor.hosts}),d.interceptors.push("ehaCouchDbAuthHttpInterceptor")),a.userRoles&&a.userRoles.forEach(function(a){var b="require"+f(a)+"User";this[b]=function(b,c){return g(b,c,[a])}}.bind(this))},this.requireAdminUser=function(a,b){return g(a,b,h.adminRoles)},this.requireAuthenticatedUser=function(a,b){return a.getCurrentUser().then(function(a){return a})["catch"](function(){return a.trigger("unauthenticated"),b.reject("unauthenticated")})},this.$get=["Restangular","$log","$q","$localForage","$rootScope",function(b,c,d,e,f){var g=b.withConfig(function(a){a.setBaseUrl(h.url),h.defaultHttpFields&&a.setDefaultHttpFields(h.defaultHttpFields)});return new a(h,g,c,d,e,f)}]}]),"undefined"!=typeof module&&module.exports&&(module.exports=b)}(),function(){"use strict";function a(a,b){function c(b){var c=a.hosts.filter(function(a){return b.indexOf(a)>-1});return!!c.length}var d=b.get("$q"),e=b.get("$log");return{request:function(a){if(c(a.url)){var f=b.get("ehaCouchDbAuthService");return f.getCurrentUser().then(function(b){return b&&b.bearerToken&&(a.headers.Authorization="Bearer "+b.bearerToken),a})["catch"](function(b){return e.error(b),a})}return d.when(a)},responseError:function(a){if(401===a.status&&c(a.config.url)){var e=b.get("ehaCouchDbAuthService");e.trigger("unauthorized")}return d.reject(a)}}}var b=angular.module("eha.couchdb-auth.http-interceptor",[]);b.provider("ehaCouchDbAuthHttpInterceptor",function(){var b={};this.config=function(a){b=a},this.$get=["$injector",function(c){return new a(b,c)}]}),"undefined"!=typeof module&&module.exports&&(module.exports=b)}(),angular.module("eha.couchdb-auth.show-for-role.directive",[]).directive("ehaShowForRole",["ehaCouchDbAuthService","$animate","$parse","$q","$log",function(a,b,c,d,e){var f="ng-hide",g="ng-hide-animate";return{restrict:"A",link:function(h,i,j){function k(c){a.getCurrentUser().then(function(a){return a&&a.hasRole(c)?(b.removeClass(i,f,{tempClasses:g}),!0):d.reject("Role not found")})["catch"](function(a){e.error(a),b.addClass(i,f,{tempClasses:g})})}i.addClass("ng-hide");var l,m=c(j.ehaShowForRole)(h);if(angular.isArray(m))l=m;else{if(!angular.isString(m))throw Error("You must pass a string or an array of strings");l=[m]}k(l),a.on("authenticationStateChange",function(){k(l)})}}}]),angular.module("eha.couchdb-auth.show-authenticated.directive",[]).directive("ehaShowAuthenticated",["ehaCouchDbAuthService","$animate",function(a,b){var c="ng-hide",d="ng-hide-animate";return{restrict:"A",link:function(e,f){function g(){a.getCurrentUser().then(function(){b.removeClass(f,c,{tempClasses:d})})["catch"](function(){b.addClass(f,c,{tempClasses:d})})}f.addClass("ng-hide"),g(),a.on("authenticationStateChange",g)}}}]),function(){"use strict";var a=angular.module("eha.couchdb-auth",["eha.couchdb-auth.http-interceptor","eha.couchdb-auth.auth.service","eha.couchdb-auth.show-for-role.directive","eha.couchdb-auth.show-authenticated.directive"]);"undefined"!=typeof module&&module.exports&&(module.exports=a)}(); | ||
!function(){"use strict";function a(a,b,c,d,e,f){function g(){return d.when(b.all(a.sessionEndpoint).customGET())}function h(e){return d.when(b.all(a.sessionEndpoint).customPOST({name:e.username,password:e.password})).then(function(a){return a.plain()}).then(s).then(function(a){return g().then(function(b){return angular.extend(b.plain(),a)})}).then(s).then(function(a){return a&&a.ok?(u.$broadcast("authenticationStateChange"),c.log("couchdb:login:success",a),q(a)):(c.log("couchdb:login:failure:unknown"),d.reject(new Error))})["catch"](function(a){return 401===a.status?(c.log("couchdb:login:failure:invalid-credentials",a),d.reject(new Error("Invalid Credentials"))):(c.log("couchdb:login:failure:unknown",a),d.reject(new Error(a)))})}function i(){return t=null,e.removeItem("user")}function j(a){return e.setItem("user",a)}function k(){return e.getItem("user")}function l(){return d.when(b.all(a.sessionEndpoint).remove()).then(i)["finally"](function(){u.$broadcast("authenticationStateChange")})}function m(a){return a.token&&a.password?d.when(b.all("reset-password").customPOST({token:a.token,password:a.password})):a.email&&a.callbackUrl?d.when(b.all("reset-password").customPOST({email:a.email,callbackUrl:a.callbackUrl})):d.reject("You must provide both email and callbackUrl properties in the payload")}function n(){return d.reject("NOT_IMPLEMENTED")}function o(){return d.reject("NOT_IMPLEMENTED")}function p(){return d.reject("NOT_IMPLEMENTED")}function q(b){return b.hasRole=function(a){var b=this;if(angular.isArray(a)){var c=a.filter(function(a){return b.roles.indexOf(a)>-1});return!!c.length}return angular.isString(a)?this.roles.indexOf(a)>-1:void 0},b.isAdmin=function(){return this.hasRole(a.adminRoles)},b}function r(){return t?d.when(q(t)):k().then(function(a){return a?(t=a,q(a)):d.reject("User not found")}).then(function(a){return g().then(function(){return a})})["catch"](function(a){return d.reject(a)})}function s(a){return a?(t=a,j(a)):void d.reject("No user found")}var t,u=f.$new(!0);return{signIn:h,signOut:l,resetPassword:m,accounts:{add:n,update:o,remove:p},getSession:g,getCurrentUser:r,on:u.$on.bind(u),trigger:u.$broadcast.bind(u)}}var b=angular.module("eha.couchdb-auth.auth.service",["restangular","LocalForageModule","ngCookies"]);b.provider("ehaCouchDbAuthService",["$localForageProvider","ehaCouchDbAuthHttpInterceptorProvider","$httpProvider",function(b,c,d){function e(a){return a.charAt(0).toUpperCase()+a.slice(1)}function f(a){var b=[a];return a.indexOf("-")>-1?b=a.split("-"):a.indexOf("_")>-1&&(b=a.split("_")),b=b.map(e),b.join("")}function g(a,b,c){return a.getCurrentUser().then(function(d){return d&&!d.hasRole(c)?(a.trigger("unauthorized"),b.reject("unauthorized")):d})["catch"](function(){return a.trigger("unauthenticated"),b.reject("unauthenticated")})}var h={localStorageNamespace:"eha",localStorageStoreName:"auth",adminRoles:["_admin"],sessionEndpoint:"_session"};this.config=function(a){h=angular.extend(h,a),b.config({name:h.localStorageNamespace,storeName:h.localStorageStoreName}),a.interceptor&&(c.config({url:a.url,hosts:a.interceptor.hosts}),d.interceptors.push("ehaCouchDbAuthHttpInterceptor")),a.userRoles&&a.userRoles.forEach(function(a){var b="require"+f(a)+"User";this[b]=function(b,c){return g(b,c,[a])}}.bind(this))},this.requireAdminUser=function(a,b){return g(a,b,h.adminRoles)},this.requireAuthenticatedUser=function(a,b){return a.getCurrentUser().then(function(a){return a})["catch"](function(){return a.trigger("unauthenticated"),b.reject("unauthenticated")})},this.$get=["Restangular","$log","$q","$localForage","$rootScope",function(b,c,d,e,f){var g=b.withConfig(function(a){a.setBaseUrl(h.url),h.defaultHttpFields&&a.setDefaultHttpFields(h.defaultHttpFields)});return new a(h,g,c,d,e,f)}]}]),"undefined"!=typeof module&&module.exports&&(module.exports=b)}(),function(){"use strict";function a(a,b){function c(b){var c=a.hosts.filter(function(a){return b.indexOf(a)>-1});return!!c.length}var d=b.get("$q"),e=b.get("$log");return{request:function(a){if(c(a.url)){var f=b.get("ehaCouchDbAuthService");return f.getCurrentUser().then(function(b){return b&&b.bearerToken&&(a.headers.Authorization="Bearer "+b.bearerToken),a})["catch"](function(b){return e.error(b),a})}return d.when(a)},responseError:function(a){if(401===a.status&&c(a.config.url)){var e=b.get("ehaCouchDbAuthService");e.trigger("unauthorized")}return d.reject(a)}}}var b=angular.module("eha.couchdb-auth.http-interceptor",[]);b.provider("ehaCouchDbAuthHttpInterceptor",function(){var b={};this.config=function(a){b=a},this.$get=["$injector",function(c){return new a(b,c)}]}),"undefined"!=typeof module&&module.exports&&(module.exports=b)}(),angular.module("eha.couchdb-auth.show-for-role.directive",[]).directive("ehaShowForRole",["ehaCouchDbAuthService","$animate","$parse","$q","$log",function(a,b,c,d,e){var f="ng-hide",g="ng-hide-animate";return{restrict:"A",link:function(h,i,j){function k(c){a.getCurrentUser().then(function(a){return a&&a.hasRole(c)?(b.removeClass(i,f,{tempClasses:g}),!0):d.reject("Role not found")})["catch"](function(a){e.error(a),b.addClass(i,f,{tempClasses:g})})}i.addClass("ng-hide");var l,m=c(j.ehaShowForRole)(h);if(angular.isArray(m))l=m;else{if(!angular.isString(m))throw Error("You must pass a string or an array of strings");l=[m]}k(l),a.on("authenticationStateChange",function(){k(l)})}}}]),angular.module("eha.couchdb-auth.show-authenticated.directive",[]).directive("ehaShowAuthenticated",["ehaCouchDbAuthService","$animate",function(a,b){var c="ng-hide",d="ng-hide-animate";return{restrict:"A",link:function(e,f){function g(){a.getCurrentUser().then(function(){b.removeClass(f,c,{tempClasses:d})})["catch"](function(){b.addClass(f,c,{tempClasses:d})})}f.addClass("ng-hide"),g(),a.on("authenticationStateChange",g)}}}]),function(){"use strict";var a=angular.module("eha.couchdb-auth",["eha.couchdb-auth.http-interceptor","eha.couchdb-auth.auth.service","eha.couchdb-auth.show-for-role.directive","eha.couchdb-auth.show-authenticated.directive"]);"undefined"!=typeof module&&module.exports&&(module.exports=a)}(); |
{ | ||
"name": "angular-eha.couchdb-auth", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "CouchDB auth wrapper", | ||
@@ -5,0 +5,0 @@ "main": [ |
@@ -32,2 +32,3 @@ # angular-eha.couchdb-auth | ||
userRoles: ['data_provider', 'analyst'],// Roles other than admin roles | ||
sessionEndpoint: '_session', // Configurable session endpoint (default: `'_session'`) | ||
interceptor: { // Enable HTTP Interceptor (default: false) | ||
@@ -34,0 +35,0 @@ hosts: [ // Configure hostnames that should be intercepted |
@@ -31,3 +31,3 @@ ;(function() { | ||
return $q.when(Restangular | ||
.all('_session') | ||
.all(options.sessionEndpoint) | ||
.customGET()); | ||
@@ -38,3 +38,3 @@ } | ||
return $q.when(Restangular | ||
.all('_session') | ||
.all(options.sessionEndpoint) | ||
.customPOST({ | ||
@@ -90,3 +90,3 @@ name: user.username, | ||
return $q.when(Restangular | ||
.all('_session') | ||
.all(options.sessionEndpoint) | ||
.remove()) | ||
@@ -212,3 +212,4 @@ .then(clearLocalUser) | ||
localStorageStoreName: 'auth', | ||
adminRoles: ['_admin'] | ||
adminRoles: ['_admin'], | ||
sessionEndpoint: '_session' | ||
}; | ||
@@ -215,0 +216,0 @@ |
73844
1436
175