Exemple #1
0
func (fe *FrontEnd) replyAPI(req *http.Request) *http.Response {
	p := req.URL.Path
	p = p[len("/api/"):]
	path := strings.Split(p, "/", -1)
	if len(path) != 1 {
		return newRespNotFound()
	}
	api := path[0]

	query := req.URL.RawQuery
	args, err := http.ParseQuery(query)
	if err != nil {
		return newRespBadRequest()
	}

	switch api {
	case "accept":
		return fe.replyAPIAccept(args)
	case "add":
		return fe.replyAPIAdd(args)
	case "live":
		return fe.replyAPILive(args)
	case "monitor":
		return fe.replyAPIMonitor(args)
	case "revoke":
		return fe.replyAPIRevoke(args)
	case "update":
		return fe.replyAPIUpdate(args)
	case "myinfo":
		return fe.replyAPIMyInfo(args)
	default:
		return newRespBadRequest()
	}
	panic("unreach")
}
Exemple #2
0
func (fe *FrontEnd) replyAdminReinvite(req *http.Request) *http.Response {

	args, err := http.ParseQuery(req.URL.RawQuery)
	s, ok := args["s"]
	if !ok || s == nil || len(s) != 1 {
		return newRespBadRequest()
	}
	sn, err := strconv.Atoi(s[0])
	if err != nil {
		return newRespBadRequest()
	}
	v, err := fe.bank.GetBySlot(sn)
	if err != nil {
		return newRespBadRequest()
	}

	data := reinviteData{Invite: fe.makeInvite(v)}

	// prepare content of page
	var w bytes.Buffer
	err = fe.tmplReinvite.Execute(&data, &w)
	if err != nil {
		return newRespServiceUnavailable()
	}

	// wrap into a page frame
	pdata := pageData{
		Title:      sys.Name + " — Invitation email",
		CSSLinks:   []string{"reinvite.css"},
		JSLinks:    []string{"reinvite.js"},
		GridLayout: "",
		Content:    w.String(),
	}
	var w2 bytes.Buffer
	err = fe.tmplPage.Execute(&pdata, &w2)
	if err != nil {
		return newRespServiceUnavailable()
	}
	return buildResp(w2.String())
}
Exemple #3
0
func (fe *FrontEnd) replyAdminAccept(req *http.Request) *http.Response {
	args, err := http.ParseQuery(req.URL.RawQuery)
	if err != nil {
		return buildResp("Invalid accept link")
	}
	// Read SignatureKey
	s, ok := args["sk"]
	if !ok || s == nil || len(s) != 1 {
		return buildResp("Invalid accept link")
	}
	sigkeyText := s[0]
	_, err = sys.ParseSigPubKey(sigkeyText)
	if err != nil {
		return buildResp("Invalid accept link")
	}
	// Read DialKey
	d, ok := args["dk"]
	if !ok || d == nil || len(d) != 1 {
		return buildResp("Invalid accept link")
	}
	dialkeyText := d[0]
	dialkey, err := sys.ParseDialKey(dialkeyText)
	if err != nil {
		return buildResp("Invalid accept link")
	}
	// Read AcceptKey (optional)
	a, ok := args["ak"]
	var acceptkey *sys.DialKey
	if ok && a != nil && len(a) == 1 {
		acceptkey, err = sys.ParseDialKey(a[0])
		if err != nil {
			return buildResp("Invalid accept link")
		}
	}
	// Read Name
	var name string
	n, ok := args["na"]
	if ok && n != nil && len(n) == 1 {
		name = n[0]
	}
	// Read Email
	var email string
	e, ok := args["em"]
	if ok && e != nil && len(e) == 1 {
		email = e[0]
	}
	// Read Addr
	var addr string
	ad, ok := args["ad"]
	if ok && ad != nil && len(ad) == 1 {
		addr = ad[0]
	}

	// Reconcile with local database
	var v sys.View
	if acceptkey != nil {
		v, err = fe.bank.GetByAcceptKey(acceptkey)
		if err != nil {
			v = nil
		}
	}
	if v == nil {
		v, err = fe.bank.GetByDialKey(dialkey)
		if err != nil {
			v = nil
		}
	}
	if v != nil {
		if v.GetName() != "" {
			name = v.GetName()
		}
		if v.GetEmail() != "" {
			email = v.GetEmail()
		}
		if v.GetAddr() != "" {
			addr = v.GetAddr()
		}
	}
	var slotText string
	if v != nil {
		slotText = strconv.Itoa(v.GetSlot())
	}

	// prepare content
	data := acceptData{
		Name:     name,
		Email:    email,
		Addr:     addr,
		Slot:     slotText,
		DialKey:  dialkeyText,
		SigKey:   sigkeyText,
		AdminURL: fe.adminURL,
	}
	var w bytes.Buffer
	err = fe.tmplAccept.Execute(&data, &w)
	if err != nil {
		return newRespServiceUnavailable()
	}

	// prepare page
	pdata := pageData{
		Title:      sys.Name + " — Accept invitation?",
		CSSLinks:   []string{"accept.css"},
		JSLinks:    []string{"accept.js"},
		GridLayout: "",
		Content:    w.String(),
	}
	var w2 bytes.Buffer
	err = fe.tmplPage.Execute(&pdata, &w2)
	if err != nil {
		return newRespServiceUnavailable()
	}
	return buildResp(w2.String())
}
Exemple #4
0
func (fe *FrontEnd) replyAdminEdit(req *http.Request) *http.Response {

	args, err := http.ParseQuery(req.URL.RawQuery)
	if err != nil {
		return newRespBadRequest()
	}
	s, ok := args["s"]
	if !ok || s == nil || len(s) != 1 {
		return newRespBadRequest()
	}
	sn, err := strconv.Atoi(s[0])
	if err != nil {
		return newRespBadRequest()
	}
	v, err := fe.bank.GetBySlot(sn)
	if err != nil {
		return newRespBadRequest()
	}

	data := editData{
		Slot:  sn,
		Name:  v.GetName(),
		Email: v.GetEmail(),
		Addr:  v.GetAddr(),
	}
	if v.GetId() != nil {
		data.Id = v.GetId().Eye()
	}
	if v.GetDialKey() != nil {
		data.DialKey = v.GetDialKey().String()
	}
	if v.GetAcceptKey() != nil {
		data.AcceptKey = v.GetAcceptKey().String()
	}
	if v.GetHelloKey() != nil {
		data.HelloKey = v.GetHelloKey().String()
	}
	if v.GetSignatureKey() != nil {
		data.SigKey = v.GetSignatureKey().String()
	}

	// prepare content of page
	var w bytes.Buffer
	err = fe.tmplEdit.Execute(&data, &w)
	if err != nil {
		return newRespServiceUnavailable()
	}

	// wrap into a page frame
	pdata := pageData{
		Title:      "Tonika — Edit contact",
		CSSLinks:   []string{"edit.css"},
		JSLinks:    []string{"edit.js"},
		GridLayout: "",
		Content:    w.String(),
	}
	var w2 bytes.Buffer
	err = fe.tmplPage.Execute(&pdata, &w2)
	if err != nil {
		return newRespServiceUnavailable()
	}
	return buildResp(w2.String())
}