From a97a24dbcd62684bdaf8015adf6571c82e1807c1 Mon Sep 17 00:00:00 2001 From: knoxfighter Date: Mon, 31 Jul 2017 18:45:24 +0200 Subject: [PATCH] added factorio Login to access the mod-portal-API --- src/handlers.go | 69 ++++++++++++----------- src/mods.go | 32 +++++++++++ src/routes.go | 5 ++ ui/App/components/Mods/ModOverview.jsx | 77 +++++++++++++++++++++++++- ui/App/components/Mods/ModSearch.jsx | 41 ++++++++++++++ 5 files changed, 190 insertions(+), 34 deletions(-) create mode 100644 ui/App/components/Mods/ModSearch.jsx diff --git a/src/handlers.go b/src/handlers.go index b357244..c845936 100644 --- a/src/handlers.go +++ b/src/handlers.go @@ -50,6 +50,38 @@ func ListInstalledMods(w http.ResponseWriter, r *http.Request) { } } +// Login into the Factorio Mod Portal System +func LoginFactorioModPortal(w http.ResponseWriter, r *http.Request) { + var err error + resp := JSONResponse{ + Success: false, + } + + w.Header().Set("Content-Type", "application/json;charset=UTF-8") + + username := r.FormValue("username") + password := r.FormValue("password") + + var statusCode int + resp.Data, err, statusCode = getUserToken(username, password) + + w.WriteHeader(statusCode) + + if err != nil { + resp.Data = fmt.Sprintf("Error in getUserToken or LoginFactorioModPortal handler: %s", err) + if err := json.NewEncoder(w).Encode(resp); err != nil { + log.Printf("Error in Factorio-Login: %s", err) + } + return + } + + resp.Success = true + + if err := json.NewEncoder(w).Encode(resp); err != nil { + log.Printf("Error in Factorio-Login: %s", err) + } +} + // Toggles mod passed in through mod variable // Updates mod-list.json file to toggle the enabled status of mods /*func ToggleMod(w http.ResponseWriter, r *http.Request) { @@ -90,33 +122,8 @@ func ListInstalledMods(w http.ResponseWriter, r *http.Request) { } }*/ -// Returns JSON response of all mods in the mod-list.json file -/*func ListMods(w http.ResponseWriter, r *http.Request) { - var err error - resp := JSONResponse{ - Success: false, - } - - w.Header().Set("Content-Type", "application/json;charset=UTF-8") - - resp.Data, err = parseModList() - if err != nil { - resp.Data = fmt.Sprintf("Could not parse mod list: %s", err) - if err := json.NewEncoder(w).Encode(resp); err != nil { - log.Printf("Error in list mods: %s", err) - } - return - } - - resp.Success = true - - if err := json.NewEncoder(w).Encode(resp); err != nil { - log.Printf("Error listing mods: %s", err) - } -}*/ - // Uploads mod to the mods directory -func UploadMod(w http.ResponseWriter, r *http.Request) { +/*func UploadMod(w http.ResponseWriter, r *http.Request) { var err error resp := JSONResponse{ Success: false, @@ -163,7 +170,7 @@ func UploadMod(w http.ResponseWriter, r *http.Request) { default: w.WriteHeader(http.StatusMethodNotAllowed) } -} +}*/ /*func RemoveMod(w http.ResponseWriter, r *http.Request) { var err error @@ -195,7 +202,7 @@ func UploadMod(w http.ResponseWriter, r *http.Request) { } }*/ -func DownloadMod(w http.ResponseWriter, r *http.Request) { +/*func DownloadMod(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json;charset=UTF-8") vars := mux.Vars(r) @@ -206,7 +213,7 @@ func DownloadMod(w http.ResponseWriter, r *http.Request) { log.Printf("%s downloading: %s", r.Host, modFile) http.ServeFile(w, r, modFile) -} +}*/ /*func CreateModPackHandler(w http.ResponseWriter, r *http.Request) { var err error @@ -278,7 +285,7 @@ func DownloadMod(w http.ResponseWriter, r *http.Request) { } }*/ -func DownloadModPack(w http.ResponseWriter, r *http.Request) { +/*func DownloadModPack(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json;charset=UTF-8") vars := mux.Vars(r) @@ -289,7 +296,7 @@ func DownloadModPack(w http.ResponseWriter, r *http.Request) { log.Printf("%s downloading: %s", r.Host, modFile) http.ServeFile(w, r, modFile) -} +}*/ /*func DeleteModPack(w http.ResponseWriter, r *http.Request) { var err error diff --git a/src/mods.go b/src/mods.go index efcd224..bf67922 100644 --- a/src/mods.go +++ b/src/mods.go @@ -4,6 +4,8 @@ import ( "io/ioutil" "log" "encoding/json" + "net/http" + "net/url" ) type Mod struct { @@ -34,3 +36,33 @@ func listInstalledMods(modDir string) ([]Mod, error) { return result.Mods, nil } + + +type LoginErrorResponse struct { + Message string `json:"message"` + Status int `json:"status"` +} +type LoginSuccessResponse struct { + UserKey []string `json:""` +} +//Log the user into factorio, so mods can be downloaded +func getUserToken(username string, password string) (string, error, int) { + resp, get_err := http.PostForm("https://auth.factorio.com/api-login", + url.Values{"require_game_ownership": {"true"}, "username": {username}, "password": {password}}) + if get_err != nil { + log.Fatal(get_err) + return "error", get_err, 500 + } + + text, err_io := ioutil.ReadAll(resp.Body) + resp.Body.Close() + + text_string := string(text) + + if err_io != nil { + log.Fatal(err_io) + return "error", err_io, resp.StatusCode + } + + return text_string, nil, resp.StatusCode +} diff --git a/src/routes.go b/src/routes.go index 18a685d..d535db2 100644 --- a/src/routes.go +++ b/src/routes.go @@ -153,6 +153,11 @@ var apiRoutes = Routes{ "GET", "/mods/list/installed", ListInstalledMods, + }, { + "LoginFactorioModPortal", + "POST", + "/mods/factorio/login", + LoginFactorioModPortal, }, /*{ "ListMods", "GET", diff --git a/ui/App/components/Mods/ModOverview.jsx b/ui/App/components/Mods/ModOverview.jsx index 5b21316..1978915 100644 --- a/ui/App/components/Mods/ModOverview.jsx +++ b/ui/App/components/Mods/ModOverview.jsx @@ -1,7 +1,61 @@ import React from 'react'; import Mod from './Mod.jsx'; +import ModSearch from './ModSearch.jsx'; class ModOverview extends React.Component { + constructor(props) { + super(props); + + this.handlerFactorioLogin = this.handlerFactorioLogin.bind(this); + + this.state = { + username: "", + userKey: "" + } + } + + handlerSearchMod(e) { + console.log($(e.target).find("input").val()); + e.preventDefault(); + //TODO + } + + handlerFactorioLogin(e) { + e.preventDefault(); + + let $form = $(e.target); + let username = $form.find('input[name=username]').val(); + + $.ajax({ + // url: "https://auth.factorio.com/api-login", + // url: "https://mods.factorio.com/api/mods", + url: "/api/mods/factorio/login", + method: "POST", + crossDomain: true, + data: $form.serialize(), + dataType: "JSON", + success: (data) => { + swal({ + title: "Logged in Successfully", + type: "success" + }); + + this.setState({ + "username": username, + "userKey": (JSON.parse(data.data))[0] + }); + }, + error: (jqXHR) => { + let json_data = JSON.parse(jqXHR.responseJSON.data); + + swal({ + title: json_data.message, + type: "error" + }); + } + }); + } + render() { return(
@@ -10,18 +64,35 @@ class ModOverview extends React.Component {
+
+ + +
+ +
+
+
- + {this.props.installedMods.map ( (mod, i) => { - if(mod != "base") + if(mod.name !== "base") return( +
+ + + + +
+ + ) + } else { + return ( +
+

Login into Factorio

+
+ + +
+
+ + +
+ + + ) + } + } +} + +ModSearch.propTypes = { + submitSearchMod: React.PropTypes.func.isRequired, + userKey: React.PropTypes.string.isRequired, + submitFactorioLogin: React.PropTypes.func.isRequired +} + +export default ModSearch; \ No newline at end of file
Name StatusToggle StatusToggle/Remove