-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathangular-lock.js
122 lines (110 loc) · 3.55 KB
/
angular-lock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
(function() {
'use strict';
angular.module('auth0.lock', []).provider('lock', lock);
function lock() {
if (typeof Auth0Lock !== 'function') {
throw new Error('Auth0Lock must be loaded.');
}
// Stub required functions to allow auth0-angular to initialize
Auth0Lock.prototype.getClient = function() {
void 0;
};
Auth0Lock.prototype.parseHash = function() {
void 0;
};
this.init = function(config) {
if (!config) {
throw new Error('clientID and domain must be provided to lock');
}
this.clientID = config.clientID;
this.domain = config.domain;
this.options = config.options || {};
this.options._telemetryInfo = {
name: 'angular-lock',
version: '%%PACKAGE_VERSION%%'
};
};
this.$get = [
'$rootScope',
'$location',
function($rootScope, $location) {
var Lock = new Auth0Lock(this.clientID, this.domain, this.options);
var webAuthOptions = {
clientID: this.clientID,
domain: this.domain,
_telemetryInfo: this.options._telemetryInfo,
_sendTelemetry: this.options._sendTelemetry
};
var shouldVerifyIdToken = true;
if (this.options._idTokenVerification === false)
shouldVerifyIdToken = false;
var lock = {};
var functions = [];
for (var i in Lock) {
if (typeof Lock[i] === 'function') {
functions.push(i);
}
}
function safeApply(fn) {
var phase = $rootScope.$root.$$phase;
if (phase === '$apply' || phase === '$digest') {
if (fn && typeof fn === 'function') {
fn();
}
} else {
$rootScope.$apply(fn);
}
}
function wrapArguments(parameters) {
var lastIndex = parameters.length - 1,
func = parameters[lastIndex];
if (typeof func === 'function') {
parameters[lastIndex] = function() {
var args = arguments;
safeApply(function() {
func.apply(Lock, args);
});
};
}
return parameters;
}
for (var i = 0; i < functions.length; i++) {
lock[functions[i]] = (function(name) {
var customFunction = function() {
return Lock[name].apply(Lock, wrapArguments(arguments));
};
return customFunction;
})(functions[i]);
}
lock.interceptHash = function() {
if (typeof auth0.WebAuth !== 'function') {
throw new Error('Auth0.js version 8 or higher must be loaded');
return;
}
$rootScope.$on('$locationChangeStart', function(event, location) {
if (
/id_token=/.test(location) ||
/access_token=/.test(location) ||
/error=/.test(location)
) {
var webAuth = new auth0.WebAuth(webAuthOptions);
var hash = $location.hash() || window.location.hash;
webAuth.parseHash(
{ hash: hash, _idTokenVerification: shouldVerifyIdToken },
function(err, authResult) {
if (err) {
Lock.emit('authorization_error', err);
}
if (authResult && authResult.idToken) {
Lock.emit('authenticated', authResult);
}
}
);
}
});
};
return lock;
}
];
}
})();