parsav  Artifact [434636bebc]

Artifact 434636bebc44a2e51a2c61653ab996f04f2860373e69029944679461e92c63b0:


-- vim: ft=terra
local pstr = lib.mem.ptr(int8)
local terra 
login_form(co: &lib.srv.convo, user: &lib.store.actor, creds: &lib.store.credset, msg: pstr)
	var doc = [lib.srv.convo.page] {
		title = 'instance logon';
		class = 'login';
		cache = false;
	}

	if user == nil then
		var form = data.view.login_username {
			loginmsg = msg;
		}
		if form.loginmsg.ptr == nil then
			form.loginmsg = '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 = 'enter the password associated with your account'
			ch.label = 'password'
			ch.method = 'pw'
			ch.auto = 'current-password';
		elseif creds.otp() then
			ch.challenge = 'enter a valid one-time password for your account'
			ch.label = 'OTP code'
			ch.method = 'otp'
			ch.auto = 'one-time-code';
		elseif creds.challenge() then
			ch.challenge = 'sign the challenge token: <code>...</code>'
			ch.label = 'digest'
			ch.method = 'challenge'
			ch.auto = 'one-time-code';
		else
			co:complain(500,'login failure','unknown login method')
			return
		end

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

	co:stdpage(doc)
	doc.body:free()
end

return login_form