-
Notifications
You must be signed in to change notification settings - Fork 12
/
ldap_client.js
executable file
·131 lines (121 loc) · 3.82 KB
/
ldap_client.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
123
124
125
126
127
128
129
130
131
LDAP = {
data : function () { return null; },
username : function (user) { return ''; },
onSuccessfulLogin : function (userId) { },
extraFormFields: new ReactiveVar([])
};
var firstAttempt = new ReactiveVar(true);
var showForm = new ReactiveVar(false);
var customFormTemplate = new ReactiveVar('');
var creatingAccount = new ReactiveVar(false);
var errorMessage = new ReactiveVar('Invalid sign in details');
LDAP.customFormTemplate = customFormTemplate;
LDAP.forceCloseForm = function () {
showForm.set(false);
}
LDAP.formHelpers = {
failedLogin : function () {
return !firstAttempt.get(); // return true if more than one attempt has been made. Show Error Message
},
errorMessage : function () {
return errorMessage.get();
},
extraFormFields: function () {
return LDAP.extraFormFields.get();
}
};
LDAP.formEvents = {
'click #login-buttons-password': function (e, tpl) {
initLogin(e, tpl);
},
'keydown input' : function (e, tpl) {
if (e.keyCode === 13) { //If Enter Key Pressed
e.preventDefault();
initLogin(e, tpl);
}
},
'click #login-buttons-logout': function (e) {
firstAttempt.set(true);
Meteor.logout(function () {
showForm.set(false);
});
},
'submit form' : function (evt) {
// Although this is not a form elememt, custom UIs may use a form element
// we need to prevent the default behaviour of submitting the form
evt.preventDefault();
},
'click #login-forgot-passwork-link': function (e, tpl) {
var email = $('#at-field-email').val() || '';
var email = prompt('Send reset password link for this email address:', email);
if (email) {
Accounts.forgotPassword({email: email}, function (err) {
if (err) {
if (err.error === 403) {
alert('There is no account with this email address');
}
}
});
}
else if (email === '') {
alert('Email address required');
}
}
};
Meteor.loginWithLdap = function (username, password, extraFieldData, callback) {
var methodArguments = {username: username, pwd: password, ldap: true, data: _.extend(LDAP.data() || {}, extraFieldData)};
Accounts.callLoginMethod({
methodArguments: [methodArguments],
validateResult: function (result) {
},
userCallback: callback
});
};
Template.ldapLogin.helpers(LDAP.formHelpers);
Template.ldapLogin.events(LDAP.formEvents);
Template.ldapLoginButtons.helpers({
showForm : function () {
return showForm.get();
},
template : function () {
return !!Template[LDAP.customFormTemplate.get()] && LDAP.customFormTemplate.get() || "";
},
usernameOrEmail : function () {
return (_.isFunction(LDAP.username) && LDAP.username.call(this, this)) || this.username || (this.emails && this.emails[0] && this.emails[0].address) || 'Authenticated user';
}
});
Template.ldapLoginButtons.events({
'click .login-close-text' : function () {
showForm.set(false);
},
'click .login-link-text' : function () {
showForm.set(true);
}
});
// Initiate Login Process:
initLogin = function (e, tpl) {
firstAttempt.set(true);
errorMessage.set('Invalid sign in details');
var username = $(tpl.find('input[name="ldap"]')).val();
var password = $(tpl.find('input[name="password"]')).val();
var extraFieldData = {};
$('.login-extra-field').each(function (i, e) {
var field = Blaze.getData($(e)[0]);
extraFieldData[field.field] = $(e).val();
});
var result = Meteor.loginWithLdap(username, password, extraFieldData, function (err, res) {
if (Meteor.userId()) {
showForm.set(false);
LDAP.onSuccessfulLogin(Meteor.user());
return true;
}
else {
firstAttempt.set(false);
if (err && err.error === 401) {
alert("If you don't have an account provided, you need to sign in using an email address");
}
return false;
}
});
return result;
};