Fix the retarded duration parsing
This commit is contained in:
29
esi/sso.go
29
esi/sso.go
@@ -11,7 +11,6 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -280,16 +279,6 @@ func (s *SSO) processCallback(isGet bool, code, state string, writeResponse func
|
||||
}{code, state, nil}
|
||||
}
|
||||
|
||||
// parseTokenExpiryBufferSeconds parses the token expiry buffer from Go duration format to seconds
|
||||
func parseTokenExpiryBufferSeconds() int {
|
||||
duration, err := time.ParseDuration(options.GlobalOptions.TokenExpiryBuffer)
|
||||
if err != nil {
|
||||
logger.Error("Invalid token expiry buffer duration %s: %v", options.GlobalOptions.TokenExpiryBuffer, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return int(duration.Seconds())
|
||||
}
|
||||
|
||||
func (s *SSO) waitForCallback() (code, state string, err error) {
|
||||
// Parse SSO callback timeout ONCE at initialization
|
||||
callbackTimeout, err := time.ParseDuration(options.GlobalOptions.SSOCallbackTimeout)
|
||||
@@ -349,11 +338,19 @@ func (s *SSO) exchangeCodeForToken(ctx context.Context, code, verifier string) (
|
||||
name, _ := parseTokenCharacter(tr.AccessToken)
|
||||
logger.Info("Successfully exchanged code for token, character: %s", name)
|
||||
|
||||
expiryBuffer, err := time.ParseDuration(options.GlobalOptions.TokenExpiryBuffer)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid token expiry buffer duration %s: %w", options.GlobalOptions.TokenExpiryBuffer, err)
|
||||
}
|
||||
|
||||
expiresIn := time.Duration(tr.ExpiresIn) * time.Second
|
||||
expiresAt := time.Now().Add(expiresIn).Add(-expiryBuffer)
|
||||
|
||||
return &types.Character{
|
||||
CharacterName: name,
|
||||
AccessToken: tr.AccessToken,
|
||||
RefreshToken: tr.RefreshToken,
|
||||
ExpiresAt: time.Now().Add(time.Duration(tr.ExpiresIn-parseTokenExpiryBufferSeconds()) * time.Second),
|
||||
ExpiresAt: expiresAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -394,7 +391,13 @@ func (s *SSO) refreshToken(ctx context.Context, char *types.Character) error {
|
||||
char.RefreshToken = tr.RefreshToken
|
||||
}
|
||||
if tr.ExpiresIn > 0 {
|
||||
char.ExpiresAt = time.Now().Add(time.Duration(tr.ExpiresIn-parseTokenExpiryBufferSeconds()) * time.Second)
|
||||
expiryBuffer, err := time.ParseDuration(options.GlobalOptions.TokenExpiryBuffer)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid token expiry buffer duration %s: %w", options.GlobalOptions.TokenExpiryBuffer, err)
|
||||
}
|
||||
|
||||
expiresIn := time.Duration(tr.ExpiresIn) * time.Second
|
||||
char.ExpiresAt = time.Now().Add(expiresIn).Add(-expiryBuffer)
|
||||
}
|
||||
|
||||
logger.Debug("Saving refreshed token to database for character %s", char.CharacterName)
|
||||
|
||||
Reference in New Issue
Block a user