Skip to content
Snippets Groups Projects
Commit a0a32f6c authored by Florent Gluck's avatar Florent Gluck
Browse files

Fixed bug in token refresh in nexus-exam

parent 8b74aaa7
Branches
No related tags found
No related merge requests found
...@@ -44,7 +44,7 @@ func (cmd *Ls)Run(args []string) int { ...@@ -44,7 +44,7 @@ func (cmd *Ls)Run(args []string) int {
func (cmd *Ls)listDir(dir string) { func (cmd *Ls)listDir(dir string) {
fis, err := ioutil.ReadDir(dir) fis, err := ioutil.ReadDir(dir)
if err != nil { if err != nil {
u.Println("Failed reading \""+dir+"\" directory: "+err.Error()) u.PrintlnErr("Failed reading \""+dir+"\" directory: "+err.Error())
return return
} }
......
...@@ -89,5 +89,6 @@ require ( ...@@ -89,5 +89,6 @@ require (
golang.org/x/text v0.14.0 // indirect golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
honnef.co/go/js/dom v0.0.0-20210725211120-f030747120f2 // indirect honnef.co/go/js/dom v0.0.0-20210725211120-f030747120f2 // indirect
nexus-client/cmdToken v0.0.0-00010101000000-000000000000 // indirect
nexus-common/caps v0.0.0-00010101000000-000000000000 // indirect nexus-common/caps v0.0.0-00010101000000-000000000000 // indirect
) )
...@@ -19,6 +19,7 @@ import ( ...@@ -19,6 +19,7 @@ import (
"nexus-client/version" "nexus-client/version"
"nexus-client/defaults" "nexus-client/defaults"
"nexus-client/cmdLogin" "nexus-client/cmdLogin"
"nexus-client/cmdToken"
"fyne.io/fyne/v2" "fyne.io/fyne/v2"
"fyne.io/fyne/v2/app" "fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/theme" "fyne.io/fyne/v2/theme"
...@@ -54,6 +55,7 @@ func exit(code int) { ...@@ -54,6 +55,7 @@ func exit(code int) {
func attachVM(parent fyne.Window, hostname, cert, pwd string) { func attachVM(parent fyne.Window, hostname, cert, pwd string) {
client := g.GetInstance().Client client := g.GetInstance().Client
host := g.GetInstance().Host host := g.GetInstance().Host
setHeaderToken()
p := &params.VMAttachCreds{ Pwd: pwd } p := &params.VMAttachCreds{ Pwd: pwd }
resp, err := client.R().SetBody(p).Post(host+"/vms/spicecreds") resp, err := client.R().SetBody(p).Post(host+"/vms/spicecreds")
...@@ -133,37 +135,26 @@ func hypervisorCheck() { ...@@ -133,37 +135,26 @@ func hypervisorCheck() {
} }
} }
// Recurrently obtains a new JWT token so that the user session doesn't expire. func setHeaderToken() {
func refreshToken(parent fyne.Window) {
client := g.GetInstance().Client client := g.GetInstance().Client
host := g.GetInstance().Host client.SetHeader("Content-Type", "application/json")
client.SetHeader("Authorization", "Bearer "+token)
}
// Recurrently obtains a new JWT token so that the user session doesn't expire.
func refreshToken(parent fyne.Window) {
for { for {
resp, err := client.R().Get(host+"/token/refresh") setHeaderToken()
refreshedToken, err := cmdToken.GetToken()
if err != nil { if err != nil {
errorPopup(parent, "Failed refreshing token (code 1)") errorPopup(parent, "Failed refreshing token")
u.PrintlnErr(buildversion.BuildDate(), ": failed refreshing token (code 1)") u.PrintlnErr(time.Now(), ": failed refreshing token: "+err.Error())
break
} else { } else {
if resp.IsSuccess() { u.Println(time.Now(), ": successfully refreshed token")
type Response struct {
Token string
}
var response Response
err = json.Unmarshal(resp.Body(), &response)
if err != nil {
errorPopup(parent, "Failed refreshing token (code 2)")
u.PrintlnErr(buildversion.BuildDate(), ": failed refreshing token (code 2)")
} else {
token = response.Token
u.Println(buildversion.BuildDate(), ": successfully refreshed token")
}
} else {
// errorPopup(parent, resp.Status()+": "+resp.String())
errorPopup(parent, "Failed refreshing token (code 3)")
u.PrintlnErr(buildversion.BuildDate(), ": failed refreshing token (code 3)")
}
} }
time.Sleep(1*time.Hour) token = refreshedToken
time.Sleep(30*time.Second)
} }
} }
...@@ -229,9 +220,6 @@ func run() int { ...@@ -229,9 +220,6 @@ func run() int {
abortWindow("Failed obtaining token (network issue?)") abortWindow("Failed obtaining token (network issue?)")
} }
client.SetHeader("Content-Type", "application/json")
client.SetHeader("Authorization", "Bearer "+token)
app := app.New() app := app.New()
app.Settings().SetTheme(theme.LightTheme()) app.Settings().SetTheme(theme.LightTheme())
win := app.NewWindow(windowTitle) win := app.NewWindow(windowTitle)
......
...@@ -212,14 +212,14 @@ Type: "help" for help on commands ...@@ -212,14 +212,14 @@ Type: "help" for help on commands
quit = true quit = true
continue continue
} else { } else {
u.Println("Error reading line: ", err) u.PrintlnErr("Error reading line: ", err)
continue continue
} }
line = strings.TrimSpace(line) line = strings.TrimSpace(line)
args, err := extractArgs(line) args, err := extractArgs(line)
if err != nil { if err != nil {
u.Println("invalid command line: "+err.Error()) u.PrintlnErr("invalid command line: "+err.Error())
continue continue
} }
if args == nil { if args == nil {
...@@ -245,7 +245,7 @@ Type: "help" for help on commands ...@@ -245,7 +245,7 @@ Type: "help" for help on commands
if typedCmd == "refresh" { if typedCmd == "refresh" {
refreshedToken, err := cmdToken.GetToken() refreshedToken, err := cmdToken.GetToken()
if err != nil { if err != nil {
u.Println("refresh error: "+err.Error()) u.PrintlnErr("refresh error: "+err.Error())
break break
} }
token = refreshedToken token = refreshedToken
...@@ -256,7 +256,7 @@ Type: "help" for help on commands ...@@ -256,7 +256,7 @@ Type: "help" for help on commands
break break
} }
} else { } else {
u.Println(typedCmd+": unknown command") u.PrintlnErr(typedCmd+": unknown command")
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment