Skip to content

Commit

Permalink
Fix config file corruption on ssl pining for console client
Browse files Browse the repository at this point in the history
  • Loading branch information
jypelle committed Jan 8, 2022
1 parent 59941f8 commit 6a184e6
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 60 deletions.
2 changes: 1 addition & 1 deletion internal/cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (c *ClientConfig) GetCert() []byte {
}

func (c *ClientConfig) SetCert(cert []byte) error {
err := os.WriteFile(c.GetCompleteConfigFilename(), cert, 0660)
err := os.WriteFile(filepath.Join(c.ConfigDir, configCertFilename), cert, 0660)
if err != nil {
return err
}
Expand Down
63 changes: 59 additions & 4 deletions internal/cliwa/app_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ func NewApp(debugMode bool) *App {
eventFunc: make(chan func(), 100),
}

app.StartComponent = NewStartComponent(app)

logrus.Infof("Client created")

return app
Expand All @@ -50,7 +48,34 @@ func NewApp(debugMode bool) *App {
func (a *App) Start() {
a.retrieveServerCredentials()
a.HideLoader()
a.StartComponent.Render()

// Autolog ?
func() {
username := jst.LocalStorage.Get("mifasolUsername").String()
password := jst.LocalStorage.Get("mifasolPassword").String()
if username != "" || password != "" {
a.config.Username = username
a.config.Password = password

// Create rest Client
var err error
a.restClient, err = restClientV1.NewRestClient(&a.config, true)
if err != nil {
logrus.Errorf("Unable to instantiate mifasol rest client: %v", err)
} else {
if a.ConnectedUserId() == restApiV1.UndefinedUserId {
logrus.Errorf("Wrong credentials")

jst.LocalStorage.Set("mifasolUsername", "")
jst.LocalStorage.Set("mifasolPassword", "")
} else {
a.ConnectAction()
return
}
}
}
a.DisconnectAction()
}()

// Keep wasm app alive with event func loop
func() {
Expand Down Expand Up @@ -142,7 +167,11 @@ func (a *App) retrieveServerCredentials() {
}

func (a *App) ConnectedUserId() restApiV1.UserId {
return a.restClient.UserId()
if a.restClient == nil {
return restApiV1.UndefinedUserId
} else {
return a.restClient.UserId()
}
}

func (a *App) IsConnectedUserAdmin() bool {
Expand Down Expand Up @@ -171,3 +200,29 @@ func (c *App) ShowLoader(message string) {
func (c *App) HideLoader() {
jst.Document.Get("body").Get("classList").Call("remove", "loading")
}

func (c *App) ConnectAction() {
c.localDb = localdb.NewLocalDb(c.restClient, c.config.Collator())
c.HomeComponent = NewHomeComponent(c)
c.StartComponent = nil
c.Render()
c.HomeComponent.Reload()
}

func (c *App) DisconnectAction() {
jst.LocalStorage.Set("mifasolUsername", "")
jst.LocalStorage.Set("mifasolPassword", "")
c.restClient = nil
c.localDb = nil
c.HomeComponent = nil
c.StartComponent = NewStartComponent(c)
c.Render()
}

func (c *App) Render() {
if c.ConnectedUserId() == restApiV1.UndefinedUserId {
c.StartComponent.Render()
} else {
c.HomeComponent.Render()
}
}
5 changes: 5 additions & 0 deletions internal/cliwa/component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package cliwa

type Component interface {
Render()
}
9 changes: 0 additions & 9 deletions internal/cliwa/homeComponent_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ func (c *HomeComponent) Render() {
c.LibraryComponent.Render()
c.CurrentComponent.Render()
c.PlayerComponent.Render()

c.Reload()

}

func (c *HomeComponent) uploadSongsAction() {
Expand All @@ -49,12 +46,6 @@ func (c *HomeComponent) uploadSongsAction() {
component.Render()
}

func (c *HomeComponent) logOutAction() {
jst.LocalStorage.Set("mifasolUsername", "")
jst.LocalStorage.Set("mifasolPassword", "")
c.app.StartComponent.Render()
}

func (c *HomeComponent) refreshAction() {
c.Reload()
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cliwa/homeHeaderButtonsComponent_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (c *HomeHeaderButtonsComponent) Render() {
uploadSongsButton := jst.Id("uploadSongsButton")
uploadSongsButton.Call("addEventListener", "click", c.app.AddEventFunc(c.app.HomeComponent.uploadSongsAction))
logOutButton := jst.Id("logOutButton")
logOutButton.Call("addEventListener", "click", c.app.AddEventFunc(c.app.HomeComponent.logOutAction))
logOutButton.Call("addEventListener", "click", c.app.AddEventFunc(c.app.DisconnectAction))
refreshButton := jst.Id("refreshButton")
refreshButton.Call("addEventListener", "click", c.app.AddEventFunc(c.app.HomeComponent.refreshAction))
}
Expand Down
42 changes: 1 addition & 41 deletions internal/cliwa/startComponent_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cliwa

import (
"github.com/jypelle/mifasol/internal/cliwa/jst"
"github.com/jypelle/mifasol/internal/localdb"
"github.com/jypelle/mifasol/restApiV1"
"github.com/jypelle/mifasol/restClientV1"
"github.com/sirupsen/logrus"
Expand All @@ -21,34 +20,6 @@ func NewStartComponent(app *App) *StartComponent {
}

func (c *StartComponent) Render() {
c.app.restClient = nil
c.app.localDb = nil

// Autolog ?
username := jst.LocalStorage.Get("mifasolUsername").String()
password := jst.LocalStorage.Get("mifasolPassword").String()
if username != "" || password != "" {
c.app.config.Username = username
c.app.config.Password = password

// Create rest Client
var err error
c.app.restClient, err = restClientV1.NewRestClient(&c.app.config, true)
if err != nil {
logrus.Errorf("Unable to instantiate mifasol rest client: %v", err)
} else {
if c.app.ConnectedUserId() == restApiV1.UndefinedUserId {
logrus.Errorf("Wrong credentials")

jst.LocalStorage.Set("mifasolUsername", "")
jst.LocalStorage.Set("mifasolPassword", "")
} else {
c.goHome()
return
}
}
}

// No autolog or autolog failed
mainComponent := jst.Id("mainComponent")
mainComponent.Set("innerHTML", c.app.RenderTemplate(nil, "start/index"))
Expand All @@ -57,10 +28,8 @@ func (c *StartComponent) Render() {
jst.Id("mifasolUsername").Call("focus")

// Set button
//js.Global().Set("logInAction", c.app.AddEventFunc(c.logInAction))
startForm := jst.Id("startForm")
startForm.Call("addEventListener", "submit", c.app.AddEventFuncPreventDefault(c.logInAction))

}

func (c *StartComponent) logInAction() {
Expand Down Expand Up @@ -96,14 +65,5 @@ func (c *StartComponent) logInAction() {
jst.LocalStorage.Set("mifasolPassword", c.app.config.Password)
}

c.goHome()
}

func (c *StartComponent) goHome() {
// c.app.restClient = restClient
c.app.localDb = localdb.NewLocalDb(c.app.restClient, c.app.config.Collator())

c.app.HomeComponent = NewHomeComponent(c.app)
c.app.HomeComponent.Render()

c.app.ConnectAction()
}
5 changes: 2 additions & 3 deletions internal/srv/serverApp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/jypelle/mifasol/internal/tool"
"github.com/jypelle/mifasol/internal/version"
"github.com/sirupsen/logrus"
"io/ioutil"
_ "modernc.org/sqlite"
"net/http"
"os"
Expand All @@ -30,7 +29,7 @@ type ServerApp struct {

func NewServerApp(configDir string, debugMode bool) *ServerApp {

logrus.Debugf("Creation of mifasol server %s ...", version.AppVersion.String())
logrus.Infof("Creation of mifasol server %s ...", version.AppVersion.String())

app := &ServerApp{
ServerConfig: config.ServerConfig{
Expand Down Expand Up @@ -63,7 +62,7 @@ func NewServerApp(configDir string, debugMode bool) *ServerApp {
// Open configuration file
var draftServerEditableConfig *config.ServerEditableConfig

rawConfig, err := ioutil.ReadFile(app.ServerConfig.GetCompleteConfigFilename())
rawConfig, err := os.ReadFile(app.ServerConfig.GetCompleteConfigFilename())
if err == nil {
// Interpret configuration file
draftServerEditableConfig = &config.ServerEditableConfig{}
Expand Down
2 changes: 1 addition & 1 deletion internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ var (
AppVersion = restApiV1.Version{
MajorNumber: 0,
MinorNumber: 4,
PatchNumber: 3,
PatchNumber: 4,
}
)

0 comments on commit 6a184e6

Please sign in to comment.