示例#1
0
// SessionInfo returns a web handler function that returns information about the
// current session.
func SessionInfo(auth evesso.Authenticator, sess server.Sessionizer, localdb db.LocalDB) web.HandlerFunc {
	return func(c web.C, w http.ResponseWriter, r *http.Request) {
		curSession := sess.GetSession(&c, w, r)
		returnInfo := sessionInfo{
			Authenticated: curSession.User != 0,
			OAuthURL:      auth.URL(curSession.State),
		}
		if curSession.Token != nil {
			returnInfo.OAuthExpiry = curSession.Token.Expiry
		}
		if curSession.User != 0 {
			// We're authenticated - also pass in the API keys registered to this
			// user.
			keys, err := localdb.APIKeys(curSession.User)
			if err != nil {
				log.Fatalf("Error - unable to retrieve API keys from database.")
			}
			returnInfo.APIKeys = keys
		}
		returnJSON, _ := json.Marshal(&returnInfo)
		w.Write(returnJSON)
	}
}
示例#2
0
// BlueprintsHandlers returns web handler functions that provide information on
// a toon's bluerpints.
func BlueprintsHandlers(localdb db.LocalDB, sde evego.Database, sess server.Sessionizer) (refresh, get web.HandlerFunc) {
	refresh = func(c web.C, w http.ResponseWriter, r *http.Request) {
		s := sess.GetSession(&c, w, r)
		myUserID := s.User
		charID, _ := strconv.Atoi(c.URLParams["charID"])
		apiKeys, err := localdb.APIKeys(myUserID)
		if err != nil {
			http.Error(w, `{"status": "Error", "error": "Ouch"}`,
				http.StatusInternalServerError)
			return
		}
		// Find the key for this character.
		var myKey *db.XMLAPIKey
		for _, key := range apiKeys {
			for _, toon := range key.Characters {
				if toon.ID == charID {
					myKey = &key
					break
				}
			}
		}
		if myKey == nil {
			if err != nil {
				http.Error(w, `{"status": "Error", "error": "Invalid character supplied."}`,
					http.StatusUnauthorized)
				return
			}
		}

		err = localdb.GetAssetsBlueprints(*myKey, charID)
		if err != nil {
			http.Error(w, `{"status": "Error", "error": "Ouch"}`,
				http.StatusInternalServerError)
			return
		}
		return
	}

	get = func(c web.C, w http.ResponseWriter, r *http.Request) {
		s := sess.GetSession(&c, w, r)
		myUserID := s.User
		charID, err := strconv.Atoi(c.URLParams["charID"])
		if err != nil {
			http.Error(w, `{"status": "Error", "error": "Invalid character ID supplied."}`,
				http.StatusBadRequest)
			return
		}
		blueprints, err := localdb.CharacterBlueprints(myUserID, charID)
		if err != nil {
			http.Error(w, `{"status": "Error", "error": "Unable to access database."}`,
				http.StatusInternalServerError)
			log.Printf("Error accessing database with user %v, character %v: %v", myUserID, charID, err)
			return
		}
		stations := make(map[string]*evego.Station)
		for i := range blueprints {
			bp := &blueprints[i]
			if _, found := stations[strconv.Itoa(bp.StationID)]; !found {
				stn, err := localdb.StationForID(bp.StationID)
				if err == nil {
					stations[strconv.Itoa(bp.StationID)] = stn
				}
			}
		}
		response := struct {
			Blueprints []evego.BlueprintItem     `json:"blueprints"`
			Stations   map[string]*evego.Station `json:"stations"`
		}{blueprints, stations}
		blueprintsJSON, err := json.Marshal(&response)
		if err != nil {
			http.Error(w, `{"status": "Error", "error": "Unable to marshal JSON."}`,
				http.StatusInternalServerError)
			log.Printf("Error marshalling JSON blueprints with user %v, character %v: %v", myUserID, charID, err)
			return
		}
		w.Write(blueprintsJSON)
		return
	}

	return
}
示例#3
0
文件: user.go 项目: backerman/eveindy
// XMLAPIKeysHandlers returns web handler functions that provide information on
// the user's API keys that have been registered with this application.
func XMLAPIKeysHandlers(localdb db.LocalDB, sess server.Sessionizer) (list, delete, add, refresh web.HandlerFunc) {
	// charRefresh refreshes the characters associated with an API key and returns
	// the current list of characters via the passed responseWriter.
	charRefresh := func(s *db.Session, key *db.XMLAPIKey, w http.ResponseWriter) {
		toons, err := localdb.GetAPICharacters(s.User, *key)
		if err != nil {
			http.Error(w, `{"status": "Error", "error": "Database connection error (add characters)"}`,
				http.StatusInternalServerError)
			return
		}
		for _, toon := range toons {
			// Update skills for this character.
			err = localdb.GetAPISkills(*key, toon.ID)
			if err != nil {
				http.Error(w, `{"status": "Error", "error": "Database connection error (add skills)"}`,
					http.StatusInternalServerError)
				return
			}

			// Update standings.
			err = localdb.GetAPIStandings(*key, toon.ID)
			if err != nil {
				http.Error(w, `{"status": "Error", "error": "Database connection error (add standings)"}`,
					http.StatusInternalServerError)
				return
			}

			// Update assets and blueprints.
			err = localdb.GetAssetsBlueprints(*key, toon.ID)
			if err != nil {
				http.Error(w, `{"status": "Error", "error": "Database connection error (add assets)"}`,
					http.StatusInternalServerError)
				log.Printf("Got error in GetAssets: %v", err)
				return
			}

		}
		response := struct {
			Status     string            `json:"status"`
			Characters []evego.Character `json:"characters"`
		}{
			Status:     "OK",
			Characters: toons,
		}
		responseJSON, err := json.Marshal(response)
		w.Write(responseJSON)
		return
	}

	list = func(c web.C, w http.ResponseWriter, r *http.Request) {
		s := sess.GetSession(&c, w, r)
		userKeys, err := localdb.APIKeys(s.User)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			w.Write([]byte(`{"status": "Error"}`))
			return
		}
		userKeysJSON, err := json.Marshal(userKeys)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			w.Write([]byte(`{"status": "Error"}`))
			return
		}
		w.Write(userKeysJSON)
	}

	delete = func(c web.C, w http.ResponseWriter, r *http.Request) {
		if r.Method != "POST" {
			http.Error(w, "This function must be called with the POST method",
				http.StatusMethodNotAllowed)
			w.Write([]byte(`{"status": "Error"}`))
			return
		}
		s := sess.GetSession(&c, w, r)
		keyID, _ := strconv.Atoi(c.URLParams["keyid"])
		err := localdb.DeleteAPIKey(s.User, keyID)
		if err != nil {
			http.Error(w, "Database connection error", http.StatusInternalServerError)
			w.Write([]byte(`{"status": "Error"}`))
			return
		}
		w.Write([]byte(`{"status": "OK"}`))
		return
	}

	add = func(c web.C, w http.ResponseWriter, r *http.Request) {
		if r.Method != "POST" {
			http.Error(w, "This function must be called with the POST method",
				http.StatusMethodNotAllowed)
			w.Write([]byte(`{"status": "Error"}`))
			return
		}
		s := sess.GetSession(&c, w, r)
		key, err := unmarshalKey(r, w)
		if err != nil {
			return
		}
		// Ensure that this key is added under the session's user's account.
		key.User = s.User

		err = localdb.AddAPIKey(*key)
		if err != nil {
			http.Error(w, "Database connection error (add key)", http.StatusInternalServerError)
			w.Write([]byte(`{"status": "Error"}`))
			return
		}

		charRefresh(s, key, w)
		return
	}

	refresh = func(c web.C, w http.ResponseWriter, r *http.Request) {
		if r.Method != "POST" {
			http.Error(w, "This function must be called with the POST method",
				http.StatusMethodNotAllowed)
			w.Write([]byte(`{"status": "Error"}`))
			return
		}
		s := sess.GetSession(&c, w, r)
		key, err := unmarshalKey(r, w)
		if err != nil {
			return
		}

		// Ensure that this key is added under the session's user's account.
		key.User = s.User
		charRefresh(s, key, w)
		return
	}

	return
}