parsav  Artifact [671b92715b]

Artifact 671b92715b5b2887e5e304b3b1e691c0cd10476ecd8010936b7ecefd1ccd339f:


-- vim: ft=terra
local pstr = lib.mem.ptr(int8)
local P = lib.str.plit
local terra 
login_form(co: &lib.srv.convo, user: &lib.store.actor, creds: &lib.store.credset, msg: pstr)
	var doc = data.view.docskel {
		instance = co.srv.cfg.instance;
		title = lib.str.plit 'instance logon';
		class = lib.str.plit 'login';
		navlinks = co.navbar;
	}

	if user == nil then
		var form = data.view.login_username {
			loginmsg = msg;
		}
		if form.loginmsg.ptr == nil then
			form.loginmsg = lib.str.plit 'identify yourself for access to this instance.'
		end
		doc.body = form:tostr()
	elseif creds:sz() == 0 then
		co:complain(403,'access denied','your host is not eligible to authenticate as this user')
		return
	elseif creds:sz() == 1 then
		if creds.trust() then
			-- TODO log in immediately
			return
		end

		var ch = data.view.login_challenge {
			handle = user.handle;
			name = lib.coalesce(user.nym, user.handle);
		}
		if creds.pw() then
			ch.challenge = P'enter the password associated with your account'
			ch.label = P'password'
			ch.method = P'pw'
		elseif creds.otp() then
			ch.challenge = P'enter a valid one-time password for your account'
			ch.label = P'OTP code'
			ch.method = P'otp'
		elseif creds.challenge() then
			ch.challenge = P'sign the challenge token: <code>...</code>'
			ch.label = P'digest'
			ch.method = P'challenge'
		else
			co:complain(500,'login failure','unknown login method')
			return
		end

		doc.body = ch:tostr()
	else
		-- pick a method
	end

	var hdrs = array(
		lib.http.header { 'Content-Type', 'text/html; charset=UTF-8' }
	)
	doc:send(co.con,200,[lib.mem.ptr(lib.http.header)] {ct = 1, ptr = &hdrs[0]})
	doc.body:free()
end

return login_form