u2fregistration.coffee 2.59 KB
Newer Older
Xavier Guimard's avatar
Xavier Guimard committed
1 2 3 4
###
LemonLDAP::NG U2F registration script
###

5 6 7 8 9 10 11
setMsg = (msg, level) ->
	$('#msg').html window.translate msg
	$('#color').removeClass 'message-positive message-warning alert-success alert-warning'
	$('#color').addClass "message-#{level}"
	level = 'success' if level == 'positive'
	$('#color').addClass "alert-#{level}"

12 13 14 15 16 17 18 19
displayError = (j, status, err) ->
	console.log 'Error', err
	res = JSON.parse j.responseText
	if res and res.error
		res = res.error.replace /.* /, ''
		console.log 'Returned error', res
		setMsg res, 'warning'

Xavier Guimard's avatar
Xavier Guimard committed
20
# Registration function (launched by "register" button)
Xavier Guimard's avatar
Xavier Guimard committed
21
register = ->
22 23 24
	# 1 get registration token
	$.ajax
		type: "POST",
25
		url: "#{portal}2fregisters/u/register"
26 27
		data: {}
		dataType: 'json'
28
		error: displayError
29 30 31 32 33 34 35 36 37 38 39 40
		success: (ch) ->
			# 2 build response
			request = [
				challenge: ch.challenge
				version: ch.version
			]
			setMsg 'touchU2fDevice', 'positive'
			$('#u2fPermission').show()
			u2f.register ch.appId, request, [], (data) ->
				$('#u2fPermission').hide()
				# Handle errors
				if data.errorCode
41
					setMsg data.error, 'warning'
42 43 44 45
				else
					# 3 send response
					$.ajax
						type: "POST"
46
						url: "#{portal}2fregisters/u/registration"
47
						data: 
48
							registration: JSON.stringify data
Xavier Guimard's avatar
Xavier Guimard committed
49
							challenge: JSON.stringify ch
50
							keyName: $('#keyName').val()
51 52 53
						dataType: 'json'
						success: (resp) ->
							if resp.error
Christophe Maudoux's avatar
Christophe Maudoux committed
54 55 56
								if resp.error.match /badName/
									setMsg 'badName', 'warning'
								else setMsg 'u2fFailed', 'warning'
57
							else if resp.result
58
								setMsg 'yourKeyIsRegistered', 'positive'
59
						error: displayError
60

Xavier Guimard's avatar
Xavier Guimard committed
61
# Verification function (launched by "verify" button)
62 63 64 65
verify = ->
	# 1 get challenge
	$.ajax
		type: "POST",
66
		url: "#{portal}2fregisters/u/verify"
67 68
		data: {}
		dataType: 'json'
69
		error: displayError
70 71 72
		success: (ch) ->
			# 2 build response
			setMsg 'touchU2fDevice', 'positive'
73
			u2f.sign ch.appId, ch.challenge, ch.registeredKeys, (data) ->
74 75
				# Handle errors
				if data.errorCode
Christophe Maudoux's avatar
Christophe Maudoux committed
76
					setMsg 'unableToGetKey', 'warning'
77 78 79 80
				else
					# 3 send response
					$.ajax
						type: "POST"
81
						url: "#{portal}2fregisters/u/signature"
82 83
						data:
							signature: JSON.stringify data
84
							challenge: ch.challenge
85 86 87 88 89
						dataType: 'json'
						success: (resp) ->
							if resp.error
								setMsg 'u2fFailed', 'warning'
							else if resp.result
90
								setMsg 'yourKeyIsVerified', 'positive'
91 92
						error: (j, status, err) ->
							console.log 'error', err
Xavier Guimard's avatar
Xavier Guimard committed
93

Xavier Guimard's avatar
Xavier Guimard committed
94
# Register "click" events
Xavier Guimard's avatar
Xavier Guimard committed
95
$(document).ready ->
96 97 98 99
	$('#u2fPermission').hide()
	$('#register').on 'click', register
	$('#verify').on 'click', verify
	$('#goback').attr 'href', portal