<html lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8">
	<style>
	table, th, td {
			border: 1px solid black;
			border-collapse: collapse;
	}
	</style>
	<title>Iotsa Devices</title>
</head>
<body>
	<h1>Install SSL key for {{device}}</h1>
	{% if not deviceKey and not deviceCertificate %}
		{% set caPlugin = igor.plugins._getPluginObject(pluginName="ca", token=token) %}
		{% if caPlugin %}
			{% set deviceKey, deviceCertificate = caPlugin._generateKeyAndSign(names=[device, "192.168.4.1"], keysize=1024, token=token) %}
		{% else %}
			<p><b>Error:</b> The Igor <i>ca</i> plugin is needed to sign certificates for iotsa devices.</p>
		{% endif %}
	{% endif %}
	{% if deviceKey and deviceCertificate %}
		{% set ds1 = pluginObject._getorset(device, module="config", protocol=protocol, credentials=credentials, port=port, noverify=noverify, token=token, httpsKey=deviceKey) %}
		{% set ds2 = pluginObject._getorset(device, module="config", protocol=protocol, credentials=credentials, port=port, noverify=noverify, token=token, httpsCertificate=deviceCertificate, reboot=1) %}
		{% set message = ds1.message or ds2.message %}
		{% if message %}
			<p><b>User intervention required:</b> {{message}}</p>
		{% else %}
			{% set _ = pluginObject._getpersist(device, clearNoverify=True) %}
			<p>It seems installing the certificate and key was successful. Re-enabled certificate verification. Visit <a href="_showDevice.html?device={{device}}">{{device}} status page</a> to test.</p>
		{% endif %}
		
	{% else %}
		<p>Both Key and Certificate must be specified.</p>
	{% endif %}


	<hr>
	<a href="_showDevice.html?device={{device}}">Return to {{device}} device status page.</a><br>
	{% if returnTo %}
		<a href="{{returnTo}}">Return to {{returnTo}}.</a><br>
	{% endif %}
	<a href="/plugin/{{pluginName}}/page/index.html">Return to iotsa device setup page.</a><br>
	<a href="/">Return to Igor homepage</a>


</body>
</html>
