Simplify login and token refresh (#763)

Thanks for the input @andig !
This commit is contained in:
Andreas Linde 2021-03-15 12:45:08 +01:00 • committed by GitHub
parent 6776d13908
commit 6e875d2f2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 14 deletions

View file

@ -58,8 +58,7 @@ type MobileConnect struct {
uri string
password string
token string
tokenValid time.Time
tokenRefresh time.Time
tokenExpiry time.Time
cableInformation MCCCurrentCableInformation
}
@ -109,10 +108,7 @@ func (mcc *MobileConnect) fetchToken(request *http.Request) error {
mcc.token = tr.Token
// According to the Web Interface, the token is valid for 2 minutes
mcc.tokenValid = time.Now().Add(2 * time.Minute)
// the web interface updates the token every 2 minutes, so lets do the same here
mcc.tokenRefresh = time.Now().Add(2 * time.Minute)
mcc.tokenExpiry = time.Now().Add(2 * time.Minute)
}
return err
@ -147,6 +143,7 @@ func (mcc *MobileConnect) refresh() error {
return err
}
req.Header.Set("Referer", fmt.Sprintf("%s/login", mcc.uri))
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", mcc.token))
return mcc.fetchToken(req)
@ -155,14 +152,14 @@ func (mcc *MobileConnect) refresh() error {
// creates a http request that contains the auth token
func (mcc *MobileConnect) request(method, uri string) (*http.Request, error) {
// do we need to login?
if mcc.token == "" || time.Since(mcc.tokenValid) > 0 {
if mcc.token == "" {
if err := mcc.login(mcc.password); err != nil {
return nil, err
}
}
// is it time to refresh the token?
if time.Since(mcc.tokenRefresh) > 0 {
if time.Until(mcc.tokenExpiry) < 10*time.Second {
if err := mcc.refresh(); err != nil {
return nil, err
}

View file

@ -38,12 +38,11 @@ func NewTestClient(fn roundTripFunc) *http.Client {
// NewTestMobileConnect .
func NewTestMobileConnect(t *testing.T, responses []apiResponse) *MobileConnect {
mcc := &MobileConnect{
Helper: request.NewHelper(util.NewLogger("foo")),
uri: "http://192.0.2.2:502",
password: "none",
token: "token",
tokenValid: time.Now().Add(10 * time.Minute),
tokenRefresh: time.Now().Add(10 * time.Minute),
Helper: request.NewHelper(util.NewLogger("foo")),
uri: "http://192.0.2.2:502",
password: "none",
token: "token",
tokenExpiry: time.Now().Add(10 * time.Minute),
}
mcc.Client = NewTestClient(func(req *http.Request) *http.Response {