Esempio n. 1
0
func LoginAsCoderUser(t *testing.T, ctx context.Context, r *http.Request) context.Context {
	r.SetBasicAuth(coderUserName, testingPassword)
	c, err := passenger.NewContextFromRequest(ctx, r)
	if err != nil {
		t.Fatal(err)
	}
	return c
}
Esempio n. 2
0
// auth is there to associate a user with the incoming request.
func auth(h ContextHandlerFunc) ContextHandlerFunc {
	return func(ctx context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) {
		ctx, err = passenger.NewContextFromRequest(ctx, r)
		if err != nil {
			log.Warningf(ctx, "auth: "+err.Error())
		}
		return h(ctx, w, r)
	}
}
Esempio n. 3
0
func (h ContextHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	defer func() {
		if p := recover(); p != nil {
			deal(nil, w, r, http.StatusInternalServerError, errors.New(fmt.Sprint(p)))
		}
	}()

	ctx := appengine.NewContext(r)
	hsts(w)
	if !cors(w, r) {
		return
	}

	// Add authentication metadata.
	ctx, err := passenger.NewContextFromRequest(ctx, r)

	buf := bufferedResponseWriter{
		b: new(bytes.Buffer),
		w: w,
		s: 0,
	}

	status, err := h(ctx, buf, r)

	if status == 0 {
		status = buf.s
	}

	// No error and a low status code means
	// everything went well.
	if err == nil && status < 400 {
		if status == 0 {
			status = http.StatusOK
		}
		buf.flush(status)
		return
	}

	deal(ctx, w, r, status, err)
}