Example #1
0
func changeUserProfile(userdb *db.UserDB,
	w http.ResponseWriter, r *http.Request,
	user userView, u *pb.User) bool {
	log.Printf("hello changeUserProfile")

	if *u.Id != user.Id {
		http.Error(w, "Unauthorized", http.StatusUnauthorized)
		return false
	}

	if err := r.ParseForm(); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return false
	}

	callsign := r.Form.Get("callsign")
	if len(callsign) > 10 {
		callsign = callsign[:10]
	}
	callsign = strings.ToUpper(callsign)
	if callsign != "" {
		u.Callsign = proto.String(callsign)
	} else {
		u.Callsign = nil
	}

	if err := userdb.Store(u); err != nil {
		log.Printf("Error storing user: %s", err.Error())
		http.Error(w, "", http.StatusInternalServerError)
		return false
	}

	return true
}
Example #2
0
func userViewHandler(
	cdb *db.ContactDB, userdb *db.UserDB, stationdb *db.StationDB,
	w http.ResponseWriter, r *http.Request, user userView) {

	if len(r.URL.Path) < len(userURLPrefix) {
		http.Error(w, "Invalid path", http.StatusBadRequest)
		return
	}
	id := r.URL.Path[len(userURLPrefix):]

	u, err := userdb.Lookup(id)
	if err != nil {
		log.Printf("Error looking up user: %s", err.Error())
		http.Error(w, "", http.StatusInternalServerError)
		return
	}
	if u == nil {
		http.NotFound(w, r)
		return
	}

	if r.Method == "POST" && !changeUserProfile(userdb, w, r, user, u) {
		return
	}
	renderUserProfile(cdb, stationdb, w, r, user, u)
}
Example #3
0
func RestoreUserTable(input *db.RecordReader, output *db.UserDB) error {
	for {
		rec, err := input.ReadRecord()
		if err == io.EOF {
			break
		} else if err != nil {
			log.Printf("Error reading record: %s", err.Error())
			return err
		}

		u := &pb.User{}
		err = proto.Unmarshal(rec, u)
		if err != nil {
			log.Printf("Error parsing record: %s", err.Error())
			return err
		}

		err = output.Store(u)
		if err != nil {
			log.Printf("Error writing record: %s", err.Error())
			return err
		}

		fmt.Printf(".")
	}
	fmt.Printf("\n")

	log.Printf("User table restored.")
	return nil
}
Example #4
0
func LookupUserView(userdb *db.UserDB, userid string) (v userView) {
	v.Id = userid

	u, _ := userdb.Lookup(userid)
	if u == nil {
		return v
	}

	if u.DisplayName != nil {
		v.Name = *u.DisplayName
		if u.Callsign != nil {
			v.Name += " " + *u.Callsign
		}
	}
	if u.PhotoUrl != nil {
		v.PhotoUrl = *u.PhotoUrl
	}

	return v
}
Example #5
0
func GoogleLoginCallbackHandler(s *Sessions, userdb *db.UserDB,
	w http.ResponseWriter, r *http.Request) {

	log.Print("hello GoogleLoginCallbackHandler")

	code := r.URL.Query().Get("code")
	redirect := r.URL.Query().Get("state")
	log.Printf("redirect: %s", redirect)

	t := &oauth.Transport{Config: getConfig()}
	token, err := t.Exchange(code)
	if err != nil {
		log.Print(err)
		return
	}

	log.Printf("GoogleLoginCallbackHandler got token: %s\n", token)

	resp, err := t.Client().Get(
		"https://www.googleapis.com/oauth2/v1/userinfo")
	if err != nil {
		log.Print(err)
		return
	}

	data, err := ioutil.ReadAll(resp.Body)
	resp.Body.Close()
	if err != nil {
		log.Print(err)
		return
	}

	var userinfo oAuthUserInfo
	if err := json.Unmarshal(data, &userinfo); err != nil {
		log.Print(err)
		return
	}
	log.Printf("User logged in: %s", userinfo.Email)

	if userinfo.Verified_email == false || userinfo.Email == "" {
		log.Printf("email not verified")
		http.Error(w, "Authentication failed", http.StatusUnauthorized)
		return
	}

	user, err := userdb.UserLogin(
		userinfo.Email, userinfo.Name, userinfo.Picture, GoogleOAuth2)
	if err != nil {
		log.Printf("UserDB error: %s", err.Error())
		http.Error(w, "Unable to complete login",
			http.StatusInternalServerError)
		return
	}

	cookie, err := s.Begin(
		*user.Id, user.DisplayName, user.PhotoUrl)
	if err != nil {
		log.Print(err)
		return
	}
	log.Printf("session id: %v", cookie)

	http.SetCookie(w, cookie)
	http.Redirect(w, r, redirect, http.StatusFound)
}