示例#1
0
func (d *Discharger) login(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	if d.LoginHandler != nil {
		d.LoginHandler(d, w, r)
		return
	}
	al, err := d.GetAgentLogin(r)
	if err != nil {
		d.WriteJSON(w, http.StatusBadRequest, httpbakery.Error{
			Message: fmt.Sprintf("cannot read agent login: %s", err),
		})
		return
	}
	_, err = httpbakery.CheckRequest(d.Bakery, r, nil, nil)
	if err == nil {
		d.FinishWait(w, r, nil)
		d.WriteJSON(w, http.StatusOK, agent.AgentResponse{
			AgentLogin: true,
		})
		return
	}
	m, err := d.Bakery.NewMacaroon("", nil, []checkers.Caveat{
		bakery.LocalThirdPartyCaveat(al.PublicKey),
	})
	if err != nil {
		d.WriteJSON(w, http.StatusInternalServerError, httpbakery.Error{
			Message: fmt.Sprintf("cannot create macaroon: %s", err),
		})
		return
	}
	httpbakery.WriteDischargeRequiredError(w, m, "", nil)
}
示例#2
0
func (*DischargeSuite) TestDischargeAllLocalDischarge(c *gc.C) {
	svc, err := bakery.NewService(bakery.NewServiceParams{})
	c.Assert(err, gc.IsNil)

	clientKey, err := bakery.GenerateKey()
	c.Assert(err, gc.IsNil)

	m, err := svc.NewMacaroon("", nil, []checkers.Caveat{
		bakery.LocalThirdPartyCaveat(&clientKey.Public),
	})
	c.Assert(err, gc.IsNil)

	ms, err := bakery.DischargeAllWithKey(m, noDischarge(c), clientKey)
	c.Assert(err, gc.IsNil)

	err = svc.Check(ms, checkers.New())
	c.Assert(err, gc.IsNil)
}