Ejemplo n.º 1
0
// Authenticates a login request and returns capabilities to initial data.
func loginHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		log.Printf("Invalid request: %v", r)
		return
	}
	var cred struct {
		Username string `json:"username"`
		Password string `json:"password"`
	}
	err := util.JSONBody(r, &cred)
	if err != nil {
		panic(fmt.Sprintf("invalid request: %v", err))
	}

	log.Printf("%v attempt to login", cred.Username)
	rev, err := dept.AuthReviewer(model.ReviewerId(cred.Username), cred.Password)
	if err != nil {
		util.JSONResponse(w,
			map[string]interface{}{"msg": "invalid username or password"})
		return
	}

	reviewers, err := dept.GetReviewerIdMap()
	if err != nil {
		panic(err)
	}

	matsCap := capServer.Grant(materialKey, cred.Username)

	err = util.JSONResponse(w, map[string]interface{}{
		"revId":            cred.Username,
		"friendlyName":     rev.Name,
		"appsCap":          capServer.Grant(dataKey, cred.Username),
		"materialsCap":     matsCap,
		"fetchCommentsCap": capServer.Grant(fetchCommentsKey, cred.Username),
		"reviewers":        reviewers,
	})
	if err != nil {
		panic("serializing response")
	}
	return
}
Ejemplo n.º 2
0
func dataHandler(key string, w http.ResponseWriter, r *http.Request) {
	apps, err := dept.Applications(key)
	if err != nil {
		log.Printf("reading apps: %v", err)
		return
	}

	err = util.JSONResponse(w, apps)
	if err != nil {
		log.Printf("writing response: %v", err)
		return
	}
}
Ejemplo n.º 3
0
func fetchCommentsHandler(key string, w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		panic("expected GET")
	}

	query, err := url.ParseQuery(r.URL.RawQuery)
	if err != nil {
		panic(err)
	}
	appId := query.Get("appId")

	comments, err := dept.LoadComments(appId)
	if err != nil {
		panic(err)
	}

	rev, err := dept.GetReviewerById(model.ReviewerId(key))
	if err != nil {
		panic(err)
	}

	env, err := util.JSONToString(&FetchCommentsEnv{rev.Name, rev.Id, appId})
	if err != nil {
		panic(err)
	}

	highlightedBy, err := dept.HighlightsByApp(appId)
	if err != nil {
		panic(err)
	}

	_ = util.JSONResponse(w, map[string]interface{}{
		"appId":          appId,
		"comments":       comments,
		"post":           capServer.Grant(postCommentKey, env),
		"setScoreCap":    capServer.Grant(setScoreKey, env),
		"highlightCap":   capServer.Grant(setHighlightKey, env),
		"unhighlightCap": capServer.Grant(delHighlightKey, env),
		"highlightedBy":  highlightedBy,
	})

	log.Printf("%v fetched comments for %v", key, appId)
}
Ejemplo n.º 4
0
func jsonHandler(value string, w http.ResponseWriter, r *http.Request) {
	resp := make(map[string]interface{}, 1)
	resp["v"] = value
	util.JSONResponse(w, resp)
}