mirror of
https://github.com/OpenFactorioServerManager/factorio-server-manager.git
synced 2024-12-31 03:11:17 +02:00
added auth handlers, added auth route and view
This commit is contained in:
parent
0eb92ea6fe
commit
bb56dab92b
24
auth.go
24
auth.go
@ -6,34 +6,40 @@ import (
|
||||
"github.com/apexskier/httpauth"
|
||||
)
|
||||
|
||||
type Auth struct {
|
||||
type AuthHTTP struct {
|
||||
backend httpauth.LeveldbAuthBackend
|
||||
aaa httpauth.Authorizer
|
||||
}
|
||||
|
||||
func initAuth() *Auth {
|
||||
return &Auth{}
|
||||
func initAuth() *AuthHTTP {
|
||||
return &AuthHTTP{}
|
||||
}
|
||||
|
||||
func (auth *Auth) createRoles() {
|
||||
func (auth *AuthHTTP) createRoles() {
|
||||
var err error
|
||||
roles := make(map[string]httpauth.Role)
|
||||
|
||||
roles["user"] = 30
|
||||
roles["admin"] = 80
|
||||
auth.aaa = httpauth.NewAuthorizer(auth.backend, "topsecretkey", "user", roles)
|
||||
auth.aaa, err = httpauth.NewAuthorizer(auth.backend, []byte("topsecretkey"), "user", roles)
|
||||
if err != nil {
|
||||
log.Printf("Error creating roles: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (auth *Auth) createUser(username, role, password, email string) error {
|
||||
user := httpauth.UserData{Username: username, Role: role}
|
||||
err = backend.SaveUser(user)
|
||||
func (auth *AuthHTTP) createUser(username, role, password, email string) error {
|
||||
user := httpauth.UserData{Username: username, Role: role, Email: email}
|
||||
err := auth.backend.SaveUser(user)
|
||||
if err != nil {
|
||||
log.Printf("Error saving user: %s", err)
|
||||
return err
|
||||
}
|
||||
|
||||
err := auth.aaa.Update(nil, nil, username, password, email)
|
||||
err = auth.aaa.Update(nil, nil, username, password, "")
|
||||
if err != nil {
|
||||
log.Printf("Error saving user: %s", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
6
main.go
6
main.go
@ -27,6 +27,7 @@ type Config struct {
|
||||
var (
|
||||
config Config
|
||||
FactorioServ *FactorioServer
|
||||
Auth *AuthHTTP
|
||||
)
|
||||
|
||||
func failOnError(err error, msg string) {
|
||||
@ -46,8 +47,6 @@ func loadServerConfig(f string) {
|
||||
decoder := json.NewDecoder(file)
|
||||
err = decoder.Decode(&config)
|
||||
|
||||
auth := initAuth
|
||||
|
||||
fmt.Println(config.Password)
|
||||
parseFlags()
|
||||
}
|
||||
@ -78,6 +77,9 @@ func main() {
|
||||
|
||||
FactorioServ = initFactorio()
|
||||
|
||||
Auth = initAuth()
|
||||
Auth.createRoles()
|
||||
|
||||
router := NewRouter()
|
||||
|
||||
fmt.Printf("Starting server on: %s:%s", config.ServerIP, config.ServerPort)
|
||||
|
22
routes.go
22
routes.go
@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
@ -18,6 +19,12 @@ type Routes []Route
|
||||
func NewRouter() *mux.Router {
|
||||
r := mux.NewRouter().StrictSlash(true)
|
||||
|
||||
r.Path("/login").
|
||||
Methods("GET").
|
||||
Methods("POST").
|
||||
Name("Login").
|
||||
Handler(http.StripPrefix("/login", http.FileServer(http.Dir("./app/"))))
|
||||
|
||||
// API subrouter
|
||||
// Serves all JSON REST handlers prefixed with /api
|
||||
s := r.PathPrefix("/api").Subrouter()
|
||||
@ -25,7 +32,7 @@ func NewRouter() *mux.Router {
|
||||
s.Methods(route.Method).
|
||||
Path(route.Pattern).
|
||||
Name(route.Name).
|
||||
Handler(route.HandlerFunc)
|
||||
Handler(CheckSession(route.HandlerFunc))
|
||||
}
|
||||
|
||||
// Serves the frontend application from the app directory
|
||||
@ -55,6 +62,19 @@ func NewRouter() *mux.Router {
|
||||
return r
|
||||
}
|
||||
|
||||
// Middleware returns a http.HandlerFunc which authenticates the users request
|
||||
func CheckSession(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("%+v", Auth.aaa)
|
||||
if err := Auth.aaa.Authorize(w, r, true); err != nil {
|
||||
log.Printf("Unauthenticated request %s %s %s", r.Method, r.Host, r.RequestURI)
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
h.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// Defines all API REST endpoints
|
||||
// All routes are prefixed with /api
|
||||
var apiRoutes = Routes{
|
||||
|
58
ui/App/components/LoginContent.jsx
Normal file
58
ui/App/components/LoginContent.jsx
Normal file
@ -0,0 +1,58 @@
|
||||
import React from 'react';
|
||||
import {IndexLink} from 'react-router';
|
||||
|
||||
class LoginContent extends React.Component {
|
||||
render() {
|
||||
return(
|
||||
<div className="content-wrapper">
|
||||
<section className="content-header">
|
||||
<h1>
|
||||
Saves
|
||||
<small>Factorio Save Files</small>
|
||||
</h1>
|
||||
<ol className="breadcrumb">
|
||||
<li><IndexLink to="/"><i className="fa fa-dashboard fa-fw"></i>Server Control</IndexLink></li>
|
||||
<li className="active">Here</li>
|
||||
</ol>
|
||||
</section>
|
||||
|
||||
<section className="content">
|
||||
<div className="row">
|
||||
<div className="login-box-body">
|
||||
<p className="login-box-msg">Sign in to start your session</p>
|
||||
|
||||
<form action="">
|
||||
<div className="form-group has-feedback">
|
||||
<input type="email" className="form-control" placeholder="Email" />
|
||||
<span className="fa fa-envelope form-control-feedback"></span>
|
||||
</div>
|
||||
<div className="form-group has-feedback">
|
||||
<input type="password" className="form-control" placeholder="Password" />
|
||||
<span className="fa fa-lock form-control-feedback"></span>
|
||||
</div>
|
||||
<div className="row">
|
||||
<div className="col-xs-8">
|
||||
<div className="checkbox">
|
||||
<label className="">
|
||||
<div className="" aria-checked="false" aria-disabled="false" style="position: relative;">
|
||||
<input type="checkbox" style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; border: 0px; opacity: 0; background: rgb(255, 255, 255);" />
|
||||
</div> Remember Me
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
<div className="col-xs-4">
|
||||
<button type="submit" className="btn btn-primary btn-block btn-flat">Sign In</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<a href="#">I forgot my password</a><br />
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default LoginContent
|
@ -6,12 +6,14 @@ import ModsContent from './App/components/ModsContent.jsx';
|
||||
import LogsContent from './App/components/LogsContent.jsx';
|
||||
import SavesContent from './App/components/SavesContent.jsx';
|
||||
import ConfigContent from './App/components/ConfigContent.jsx';
|
||||
import LoginContent from './App/components/LoginContent.jsx';
|
||||
import Index from './App/components/Index.jsx';
|
||||
|
||||
ReactDOM.render((
|
||||
<Router history={browserHistory}>
|
||||
<Route path="/" component={App}>
|
||||
<IndexRoute component={Index}/>
|
||||
<Route path="/login" component={LoginContent}/>
|
||||
<Route path="/mods" component={ModsContent}/>
|
||||
<Route path="/logs" component={LogsContent}/>
|
||||
<Route path="/saves" component={SavesContent}/>
|
||||
|
Loading…
Reference in New Issue
Block a user