Skip to content

Commit

Permalink
store myApps Sessions encrypted on local disk by using a secret per c…
Browse files Browse the repository at this point in the history
…lient
  • Loading branch information
ricoschulte committed Feb 25, 2023
1 parent 756c56a commit dd9aec1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ accountConfig := &connection.Config{
Password: "examplePassword",
UserAgent: "myApps (Go)",
SessionFilePath: "myapps_session.json",
SecretKey: []byte("Secretkey to encrypt myapps sessionkeys on local disk"),
Debug: true,
InsecureSkipVerify: false,
}
Expand All @@ -44,6 +45,7 @@ In this example, the following information is provided:
- **Password**: The password for the myApps account you want to use.
- **UserAgent**: The user agent that will be sent to the myApps server. This is used to identify sessions of the client at the Account Security list within the myApps Clients.
- **SessionFilePath**: The file path where the session keys state will be stored. This allows you to resume a session after a disconnect. Please note that they are (for now) unencrypted stored.
- **SecretKey**: A Password to encrypt the SessionFilePath file on the local disk
- **Debug**: A boolean value indicating whether or not to enable debug logging. Default is false, meaning no debug messages.
- **InsecureSkipVerify**: A boolean value indicating whether or not to verify the SSL/TLS certificate. Default is false, so connections are aborted, if the Host does not provide a valid certificate.

Expand Down Expand Up @@ -107,6 +109,7 @@ func main() {
InsecureSkipVerify: true,
UserAgent: "myApps Go client",
SessionFilePath: "myapps_session.json",
SecretKey: []byte("Secretkey to encrypt myapps sessionkeys on local disk"),
Debug: true,
}

Expand Down Expand Up @@ -148,6 +151,7 @@ func main() {
InsecureSkipVerify: false,
UserAgent: "myApps Go client",
SessionFilePath: "myapps_session.json",
SecretKey: []byte("Secretkey to encrypt myapps sessionkeys on local disk"),
Debug: true,
})

Expand All @@ -158,6 +162,7 @@ func main() {
Password: "examplePassword2",
UserAgent: "myBot (Go)",
SessionFilePath: "myapps_session_2.json",
SecretKey: []byte("a different Secretkey"),
Debug: true,
})

Expand All @@ -168,6 +173,7 @@ func main() {
Password: "examplePassword3",
UserAgent: "myApps (Go)",
SessionFilePath: "myapps_session_3.json",
SecretKey: []byte("another one"),
Debug: true,
})

Expand Down
7 changes: 4 additions & 3 deletions connection/myapps.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
Expand All @@ -17,6 +16,7 @@ import (
"time"

"github.com/gorilla/websocket"
"github.com/ricoschulte/go-myapps/encryption"
)

const session_length_usr = 32
Expand All @@ -37,6 +37,7 @@ type Config struct {
Username string `yaml:"username"` // Username of the pbx
Password string `yaml:"password"` // Password to the Username
SessionFilePath string `yaml:"sessionfilepath"` // Filename to a local JSON file to store the session. Will be created if it not exists
SecretKey []byte `yaml:"-"` // the key to encrypt local files
UserAgent string `yaml:"useragent"` // the User Agnent shown in the list of current sessions in the user profile
Handler MessageHandlerRegister // list of message handler on the session
RedirectHost string // is set, when the user is located not in the master and should open a connection to the secondary pbx
Expand Down Expand Up @@ -113,7 +114,7 @@ func (myappconfig *Config) GetSessionKeys() (string, string, error) {
}

// read file
file, err := ioutil.ReadFile(myappconfig.SessionFilePath)
file, err := encryption.DecryptFileSha256AES256(myappconfig.SecretKey, myappconfig.SessionFilePath)
if err != nil {
return "", "", err
}
Expand Down Expand Up @@ -161,7 +162,7 @@ func (myappconfig *Config) SaveSessionKeys(usr, pwd string) error {
}

// write file
if err := ioutil.WriteFile(myappconfig.SessionFilePath, file, 0600); err != nil {
if err := encryption.EncryptFileSha256AES256(myappconfig.SecretKey, file, myappconfig.SessionFilePath, 0600); err != nil {
return err
}

Expand Down
3 changes: 3 additions & 0 deletions examples/multiuser/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func main() {
InsecureSkipVerify: false,
UserAgent: "myApps Go client",
SessionFilePath: "myapps_session.json",
SecretKey: []byte("Secretkey to encrypt myapps sessionkeys on local disk"),
Debug: true,
})

Expand All @@ -30,6 +31,7 @@ func main() {
Password: "examplePassword2",
UserAgent: "myBot (Go)",
SessionFilePath: "myapps_session_2.json",
SecretKey: []byte("Secretkey to encrypt myapps sessionkeys on local disk"),
Debug: true,
})

Expand All @@ -40,6 +42,7 @@ func main() {
Password: "examplePassword3",
UserAgent: "myApps (Go)",
SessionFilePath: "myapps_session_3.json",
SecretKey: []byte("Secretkey to encrypt myapps sessionkeys on local disk"),
Debug: true,
})

Expand Down
1 change: 1 addition & 0 deletions examples/singleuser/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func main() {
InsecureSkipVerify: true,
UserAgent: "myApps Go client",
SessionFilePath: "myapps_session.json",
SecretKey: []byte("Secretkey to encrypt myapps sessionkeys on local disk"),
Debug: true,
}

Expand Down

0 comments on commit dd9aec1

Please sign in to comment.