예제 #1
0
파일: http.go 프로젝트: kellegous/404
func Setup(r pork.Router) {
	r.RespondWithFunc("/debug/goroutine", func(w pork.ResponseWriter, r *http.Request) {
		p := pprof.Lookup("goroutine")
		w.Header().Set("Content-Type", "text/plain;charset=utf-8")
		p.WriteTo(w, 2)
	})
}
예제 #2
0
파일: main.go 프로젝트: kellegous/404
func setup(r pork.Router, ctx *context.Context) error {
	root, err := findRoot()
	if err != nil {
		return err
	}

	c := pork.Content(pork.NewConfig(pork.None),
		http.Dir(root))

	// serves up static content
	r.RespondWithFunc("/", func(w pork.ResponseWriter, r *http.Request) {
		_, err := auth.SessionFromRequest(ctx, r)
		if err != nil {
			http.Redirect(w, r, "/auth/a", http.StatusTemporaryRedirect)
			return
		}

		c.ServePork(w, r)
	})

	// some debugging handlers
	r.RespondWithFunc("/info", func(w pork.ResponseWriter, r *http.Request) {
		sess, user, err := auth.UserFromRequest(ctx, r)
		if err != nil {
			panic(err)
		}

		fmt.Fprintln(w, sess, user)
	})

	return nil
}
예제 #3
0
파일: auth.go 프로젝트: kellegous/404
func Setup(r pork.Router, ctx *context.Context) {
	r.RespondWithFunc("/auth/a", func(w pork.ResponseWriter, r *http.Request) {
		http.Redirect(w, r,
			configFromRequest(ctx.Cfg, r).AuthCodeURL(""),
			http.StatusTemporaryRedirect)
	})

	r.RespondWithFunc("/auth/z", func(w pork.ResponseWriter, r *http.Request) {
		code := r.FormValue("code")
		if code == "" {
			http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
			return
		}

		tx := oauth.Transport{
			Config: configFromRequest(ctx.Cfg, r),
		}

		_, err := tx.Exchange(code)
		if err != nil {
			http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
			return
		}

		var user ghUser
		if err := fetchGhUser(&tx, &user); err != nil {
			http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
			return
		}

		sess, err := createSessionFrom(ctx, &user, tx.Token)
		if err != nil {
			panic(err)
		}

		if err := setAuthCookie(w, ctx.Cfg, sess); err != nil {
			panic(err)
		}
	})

	r.RespondWithFunc("/auth/sock", func(w pork.ResponseWriter, r *http.Request) {
		sess, err := SessionFromRequest(ctx, r)
		if err != nil {
			http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
			return
		}

		buf, err := secure.Encrypt(sess.Key, ctx.Cfg.AesKey, ctx.Cfg.HmacKey)
		if err != nil {
			panic(err)
		}

		var res bytes.Buffer
		e := base62.NewEncoder(&res)
		if _, err := e.Write(buf); err != nil {
			panic(err)
		}
		e.Close()

		w.Header().Set("Content-Type", "text/plain")
		w.Write(res.Bytes())
	})

	r.RespondWithFunc("/auth/exit", func(w pork.ResponseWriter, r *http.Request) {
		sid, err := SessionIdFromRequest(ctx, r)
		if err != nil {
			panic(err)
		}

		if sid == nil {
			return
		}

		if err := store.DeleteSession(ctx, sid); err != nil {
			panic(err)
		}

		http.SetCookie(w, &http.Cookie{
			Name:     AuthCookieName,
			Value:    "",
			Path:     "/",
			MaxAge:   0,
			HttpOnly: true,
		})
	})
}