diff options
author | kali <kali@leap.se> | 2020-09-29 20:41:55 +0200 |
---|---|---|
committer | Ruben Pollan <meskio@sindominio.net> | 2020-10-13 19:08:42 +0200 |
commit | 47ac0543b9ed2d4afb8814a19e2f4dc3c30030e1 (patch) | |
tree | 1634c42bebe7d8f59c7b56cb9800aba6ea4466f6 /pkg | |
parent | 2cf32806dcce2d41920be28bd0e7d12e5d049357 (diff) |
[feat] improve error handling during login
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/backend/api.go | 7 | ||||
-rw-r--r-- | pkg/vpn/bonafide/auth_sip.go | 12 | ||||
-rw-r--r-- | pkg/vpn/bonafide/bonafide.go | 3 |
3 files changed, 16 insertions, 6 deletions
diff --git a/pkg/backend/api.go b/pkg/backend/api.go index 1d44f8a..4390fef 100644 --- a/pkg/backend/api.go +++ b/pkg/backend/api.go @@ -18,10 +18,12 @@ import ( func Login(username, password string) { success, err := ctx.bm.DoLogin(username, password) if err != nil { - log.Printf("Error on login: %v", err) - if err.Error() == "Cannot get token: Error 502" { + if err.Error() == "TokenErrTimeout" { + ctx.Errors = "bad_auth_timeout" + } else if err.Error() == "TokenErrBadStatus 502" { ctx.Errors = "bad_auth_502" } else { + log.Println("ERROR: bad login", err) ctx.Errors = "bad_auth" } } else if success { @@ -29,7 +31,6 @@ func Login(username, password string) { ctx.LoginOk = true ctx.LoginDialog = false } else { - // TODO: display login again with an err log.Printf("Failed to login as %s", username) ctx.LoginDialog = true ctx.Errors = "bad_auth" diff --git a/pkg/vpn/bonafide/auth_sip.go b/pkg/vpn/bonafide/auth_sip.go index 5da562d..cc9d967 100644 --- a/pkg/vpn/bonafide/auth_sip.go +++ b/pkg/vpn/bonafide/auth_sip.go @@ -44,15 +44,21 @@ func (a *sipAuthentication) getToken(user, password string) ([]byte, error) { } credJSON, err := formatCredentials(user, password) if err != nil { - return nil, fmt.Errorf("Cannot encode credentials: %s", err) + log.Println("ERROR: cannot encode credentials.", err) + return nil, fmt.Errorf("TokenErrBadCred") } resp, err := a.client.Post(a.authURI, "text/json", strings.NewReader(credJSON)) if err != nil { - return nil, fmt.Errorf("Error on auth request: %v", err) + log.Println("ERROR: failed auth request", err) + if os.IsTimeout(err) { + return nil, fmt.Errorf("TokenErrTimeout") + } else { + return nil, fmt.Errorf("TokenErrBadPost") + } } defer resp.Body.Close() if resp.StatusCode != 200 { - return nil, fmt.Errorf("Cannot get token: Error %d", resp.StatusCode) + return nil, fmt.Errorf("TokenErrBadStatus %d", resp.StatusCode) } token, err := ioutil.ReadAll(resp.Body) if err != nil { diff --git a/pkg/vpn/bonafide/bonafide.go b/pkg/vpn/bonafide/bonafide.go index 973416a..4203ba2 100644 --- a/pkg/vpn/bonafide/bonafide.go +++ b/pkg/vpn/bonafide/bonafide.go @@ -85,6 +85,7 @@ func New() *Bonafide { RootCAs: certs, }, }, + Timeout: time.Second * 10, } _, tzOffsetSeconds := time.Now().Zone() tzOffsetHours := tzOffsetSeconds / secondsPerHour @@ -129,6 +130,8 @@ func (b *Bonafide) DoLogin(username, password string) (bool, error) { } var err error + + log.Println("Bonafide: getting token...") b.token, err = b.auth.getToken(username, password) if err != nil { return false, err |