refactor(auth): migrate to zerolog structured logging
This commit is contained in:
@@ -10,7 +10,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -20,6 +19,8 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -246,7 +247,7 @@ func exchangeAuthCode(ctx context.Context, code, state, verifier string, port in
|
|||||||
return nil, fmt.Errorf("save credential: %w", err)
|
return nil, fmt.Errorf("save credential: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("login successful, credentials saved to %s", credPath)
|
log.Info().Str("path", credPath).Msg("login successful, credentials saved")
|
||||||
return cred, nil
|
return cred, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@@ -15,6 +14,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
tls "github.com/refraction-networking/utls"
|
tls "github.com/refraction-networking/utls"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"golang.org/x/net/http2"
|
"golang.org/x/net/http2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -200,7 +200,7 @@ func StartBackgroundRefresh(ctx context.Context, pool *Pool) {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
log.Printf("background refresh stopped")
|
log.Info().Msg("background refresh stopped")
|
||||||
return
|
return
|
||||||
case <-time.After(refreshInterval):
|
case <-time.After(refreshInterval):
|
||||||
refreshExpiring(pool)
|
refreshExpiring(pool)
|
||||||
@@ -222,6 +222,7 @@ func refreshExpiring(pool *Pool) {
|
|||||||
hasRefresh := cred.RefreshToken != ""
|
hasRefresh := cred.RefreshToken != ""
|
||||||
nextRetry := cred.nextRefreshAfter
|
nextRetry := cred.nextRefreshAfter
|
||||||
email := cred.Email
|
email := cred.Email
|
||||||
|
expiresAt := cred.ExpiresAt
|
||||||
cred.mu.Unlock()
|
cred.mu.Unlock()
|
||||||
|
|
||||||
if !hasRefresh || !needsRefresh {
|
if !hasRefresh || !needsRefresh {
|
||||||
@@ -231,21 +232,22 @@ func refreshExpiring(pool *Pool) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("refreshing token for %s (expires %s)", email, cred.ExpiresAt.Format(time.RFC3339))
|
log.Info().Str("credential", email).Time("expires_at", expiresAt).Msg("refreshing token")
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||||
err := RefreshToken(ctx, cred)
|
err := RefreshToken(ctx, cred)
|
||||||
cancel()
|
cancel()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("refresh failed for %s: %v", email, err)
|
log.Error().Err(err).Str("credential", email).Msg("token refresh failed")
|
||||||
cred.mu.Lock()
|
cred.mu.Lock()
|
||||||
cred.nextRefreshAfter = time.Now().Add(refreshBackoff)
|
cred.nextRefreshAfter = time.Now().Add(refreshBackoff)
|
||||||
cred.mu.Unlock()
|
cred.mu.Unlock()
|
||||||
} else {
|
} else {
|
||||||
log.Printf("refreshed %s, new expiry %s", email, cred.ExpiresAt.Format(time.RFC3339))
|
|
||||||
cred.mu.Lock()
|
cred.mu.Lock()
|
||||||
|
newExpiresAt := cred.ExpiresAt
|
||||||
cred.nextRefreshAfter = time.Time{}
|
cred.nextRefreshAfter = time.Time{}
|
||||||
cred.mu.Unlock()
|
cred.mu.Unlock()
|
||||||
|
log.Info().Str("credential", email).Time("new_expiry", newExpiresAt).Msg("token refreshed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user