Subversion Repositories Applications.reseau

Rev

Rev 90 | Rev 101 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
56 mathias 1
$(document).ready(function() {
2
	// infobulles
3
	$('[data-toggle="tooltip"]').tooltip();
4
 
5
	// trucage ordre des champs pour passer du login au mdp sans passer
6
	// par le lien "oublié ?" => pas standard, c'est mal - @TODO faire mieux
7
	// Ordre : login => mdp => oublie => s'inscrire
8
	$('#login').keypress(function(e) {
9
		if(e.keyCode == 9) {
10
			if(! e.shiftKey) {
11
				$('#mdp').focus();
12
				return false;
13
			}
14
		}
15
	});
16
	$('#mdp').keypress(function(e) {
17
		if(e.keyCode == 9) {
18
			if(e.shiftKey) {
19
				$('#login').focus();
20
			} else {
21
				$('#mdp-oublie').focus();
22
			}
23
			return false;
24
		}
25
	});
26
	$('#mdp-oublie').keypress(function(e) {
27
		if(e.keyCode == 9) {
28
			if(e.shiftKey) {
29
				$('#mdp').focus();
30
			} else {
31
				$('#lien-inscription').focus();
32
			}
33
		}
34
		return false;
35
	});
36
	$('#lien-inscription').keypress(function(e) {
37
		if(e.keyCode == 9) {
38
			if(e.shiftKey) {
39
				$('#mdp-oublie').focus();
40
				return false;
41
			}
42
		}
43
	});
44
 
45
	// partenaire avec lequel se connecter
46
	$('#liste-fournisseurs ul li a').click(function() {
47
		$('#liste-fournisseurs ul li a').attr('data-focus', 'false');
48
		$(this).attr('data-focus', 'true');
78 mathias 49
		var nomPartenaire = $(this).data('nom-partenaire'),
79 mathias 50
			urlOubliMdp = $(this).data('url-oubli-mdp'),
51
			loginLegende = $(this).data('login-legende'),
52
			loginTypeChamp = $(this).data('login-type-champ');
56 mathias 53
		$('#nom-partenaire').html(nomPartenaire);
78 mathias 54
		$('#mdp-oublie').attr('href', urlOubliMdp);
79 mathias 55
		$('#login').attr('type', loginTypeChamp);
90 mathias 56
		$('#login').attr('placeholder', loginLegende);
56 mathias 57
	});
58
 
59
	// Connexion au SSO
60
	$('#formulaire-identification').submit(function() {
68 mathias 61
		connecterUtilisateur();
56 mathias 62
		return false;
63
	});
64
 
65
	// Déconnexion du SSO
66
	$('#deconnexion').click(function() {
68 mathias 67
		deconnecterUtilisateur();
68
		return false;
69
	});
70
 
71
	// vérification de l'état au chargement
72
	var urlAuth = baseUrlAuth + '/identite';
73
	$.ajax({
74
	    url: urlAuth,
75
	    type: "GET",
76
	    dataType: 'json',
77
	    xhrFields: {
78
	         withCredentials: true
79
	    }
80
	}).done(function(data) {
81
		// connecté
82
		definirUtilisateur(data.token);
83
		afficherPanneauBienvenue();
84
	})
85
	.fail(function(error) {
86
		// non connecté
87
		afficherPanneauIdentification();
88
	});
89
 
90
	// exécuter une action si définie (par ex: déconnexion)
91
	executerAction();
92
});
93
 
94
/**
95
 * Connecte l'utilisateur au SSO
96
 */
97
function connecterUtilisateur() {
98
	var login = $('#login').val(),
99
		mdp = $('#mdp').val();
100
	if (login == '' || mdp == '') {
101
		alert('Veuillez entrer votre login et votre mot de passe');
102
	} else {
103
		var urlAuth = baseUrlAuth + '/connexion?login=' + login + '&password=' + mdp;
78 mathias 104
		// partenaire ?
105
		var codePartenaire = $('#liste-fournisseurs a[data-focus="true"]').data('code-partenaire');
106
		if (codePartenaire != undefined) {
107
			urlAuth += '&partner=' + codePartenaire;
108
		}
56 mathias 109
		$.ajax({
110
		    url: urlAuth,
111
		    type: "GET",
112
		    dataType: 'json',
113
		    xhrFields: {
114
		         withCredentials: true
115
		    }
116
		})
117
		.done(function(data) {
68 mathias 118
			rediriger();
119
			definirUtilisateur(data.token);
120
			afficherPanneauBienvenue();
56 mathias 121
			masquerErreurs();
122
		})
123
		.fail(function(error) {
68 mathias 124
			afficherErreurLogin();
56 mathias 125
		});
68 mathias 126
	}
127
}
56 mathias 128
 
68 mathias 129
/**
130
 * Déconnecte l'utilisateur du SSO
131
 */
132
function deconnecterUtilisateur() {
133
	var urlAuth = baseUrlAuth + '/deconnexion';
56 mathias 134
	$.ajax({
135
	    url: urlAuth,
136
	    type: "GET",
137
	    dataType: 'json',
138
	    xhrFields: {
139
	         withCredentials: true
140
	    }
141
	})
68 mathias 142
	.done(function(data) {
143
		rediriger();
144
		definirUtilisateur();
145
		afficherPanneauIdentification();
146
		masquerErreurs();
147
	})
56 mathias 148
	.fail(function(error) {
68 mathias 149
		// @TODO gérer l'affichage de l'erreur, mais pas facile à placer
150
		// dans l'interface actuelle sans que ce soit moche
151
		//afficherErreurServeur();
56 mathias 152
	});
68 mathias 153
}
56 mathias 154
 
68 mathias 155
/**
156
 * Si la variable "action" est définie (provenant du paramètre GET "origine"),
157
 * exécute l'action associée (par ex: déconnexion)
158
 */
159
function executerAction() {
160
	switch (action) {
161
		case "deconnexion":
162
			deconnecterUtilisateur();
163
			break;
164
		default :
165
			// on ne fait rien
56 mathias 166
	}
68 mathias 167
}
56 mathias 168
 
68 mathias 169
function definirUtilisateur(jeton) {
170
	var nomComplet = '';
171
	if (jeton != undefined) {
172
		// décodage jeton
173
		var jetonDecode = decoderJeton(jeton);
174
		nomComplet = jetonDecode.intitule;
72 mathias 175
		email = jetonDecode.sub;
68 mathias 176
	}
177
	// affichage
178
	$('#nom-complet').html(nomComplet);
72 mathias 179
	$('#nom-complet').attr('title', email);
68 mathias 180
}
56 mathias 181
 
68 mathias 182
/**
100 mathias 183
 * Décodage "url-safe" des chaînes base64 retournées par le SSO (lib jwt)
184
 */
185
function b64d(input) {
186
	var remainder = input.length % 4;
187
	if (remainder != 0) {
188
		var padlen = 4 - remainder;
189
		for (var i=0; i < padlen; i++) {
190
			input += '=';
191
		}
192
	}
193
	input = input.replace('-', '+');
194
	input = input.replace('_', '/');
195
	return atob(input);
196
}
197
 
198
/**
68 mathias 199
 * Décodage à l'arrache d'un jeton JWT, ATTENTION CONSIDERE QUE LE
200
 * JETON EST VALIDE, ne pas décoder n'importe quoi - pas trouvé de lib simple
201
 */
202
function decoderJeton(jeton) {
203
	parts = jeton.split('.');
204
	payload = parts[1];
100 mathias 205
 
206
	payload = b64d(payload);
68 mathias 207
	payload = JSON.parse(payload, true);
56 mathias 208
 
68 mathias 209
	return payload;
210
}
56 mathias 211
 
68 mathias 212
/**
213
 * Si la variable "origine" est définie (provenant du paramètre GET "origine"),
214
 * redirige l'utilisateur vers cette page
215
 */
216
function rediriger() {
217
	if (origine != '') {
218
		window.location.href = origine;
56 mathias 219
	}
68 mathias 220
}
56 mathias 221
 
68 mathias 222
function afficherPanneauIdentification() {
223
	$('#panneau-identification').show();
224
	$('#panneau-bienvenue').hide();
78 mathias 225
	$('.boite-centre').addClass('reduite');
68 mathias 226
}
56 mathias 227
 
68 mathias 228
function afficherPanneauBienvenue() {
229
	$('#panneau-identification').hide();
230
	$('#panneau-bienvenue').show();
78 mathias 231
	$('.boite-centre').removeClass('reduite');
68 mathias 232
}
56 mathias 233
 
68 mathias 234
function afficherErreurLogin() {
235
	$('#info-erreur-login').show();
236
}
237
 
238
function afficherErreurServeur() {
239
	$('#info-erreur-serveur').show();
240
}
241
 
242
function masquerErreurs() {
243
	$('.info-erreur').hide();
244
}