diff options
| author | Guillermo Serrahima <dev@vylion.com> | 2026-03-12 18:25:21 +0100 |
|---|---|---|
| committer | Guillermo Serrahima <dev@vylion.com> | 2026-03-13 11:16:03 +0100 |
| commit | 7eacda993ac98fc4b3ee7bb443ae3f230761e022 (patch) | |
| tree | 001309e7347f4d3ae0218ffa949288f8758c8c44 /backend/main.go | |
| parent | 5c5028eff2681946fe1c3b330a89c65de6f134b5 (diff) | |
| download | llostre-7eacda993ac98fc4b3ee7bb443ae3f230761e022.tar.xz llostre-7eacda993ac98fc4b3ee7bb443ae3f230761e022.zip | |
Refresh token logic
Diffstat (limited to 'backend/main.go')
| -rw-r--r-- | backend/main.go | 45 |
1 files changed, 34 insertions, 11 deletions
diff --git a/backend/main.go b/backend/main.go index 6598266..032c6e9 100644 --- a/backend/main.go +++ b/backend/main.go @@ -9,6 +9,7 @@ import ( "os" "strings" "sync" + "time" "github.com/joho/godotenv" ) @@ -175,6 +176,7 @@ func callbackHandler(w http.ResponseWriter, r *http.Request) { PDS: oauthState.PDS, AccessToken: tokens.AccessToken, RefreshToken: tokens.RefreshToken, + ExpiresAt: time.Now().Add(time.Duration(tokens.ExpiresIn) * time.Second), KeyPair: oauthState.KeyPair, } delete(sessions, state) // remove the partial oauth state from login @@ -243,21 +245,42 @@ func postsHandler(w http.ResponseWriter, r *http.Request) { sessionsMutex.RLock() session, exists := sessions[cookie.Value] + if !exists { + sessionsMutex.RUnlock() + http.Error(w, "invalid session", http.StatusUnauthorized) + return + } + fmt.Printf("Session exists: %v\n", exists) - if exists { - fmt.Printf("Session DID: %s\n", session.DID) - fmt.Printf("Session PDS: %s\n", session.PDS) - fmt.Printf("Session AccessToken length: %d\n", len(session.AccessToken)) - fmt.Printf("Session KeyPair: %p\n", session.KeyPair) - if session.KeyPair != nil { - fmt.Printf("KeyPair PublicKey: %p\n", session.KeyPair.PublicKey) - } + fmt.Printf("Session DID: %s\n", session.DID) + fmt.Printf("Session PDS: %s\n", session.PDS) + fmt.Printf("Session AccessToken length: %d\n", len(session.AccessToken)) + fmt.Printf("Session KeyPair: %p\n", session.KeyPair) + if session.KeyPair != nil { + fmt.Printf("KeyPair PublicKey: %p\n", session.KeyPair.PublicKey) } + + needsRefresh := time.Now().After(session.ExpiresAt) + pds := session.PDS + refreshToken := session.RefreshToken + keypair := session.KeyPair sessionsMutex.RUnlock() - if !exists { - http.Error(w, "invalid session", http.StatusUnauthorized) - return + if needsRefresh { + newTokens, err := auth.RefreshAccessToken(pds, refreshToken, keypair) + if err != nil { + sessionsMutex.Lock() + delete(sessions, cookie.Value) + sessionsMutex.Unlock() + http.Error(w, "session expired, please login again", http.StatusUnauthorized) + return + } + + sessionsMutex.Lock() + session.AccessToken = newTokens.AccessToken + session.RefreshToken = newTokens.RefreshToken + session.ExpiresAt = time.Now().Add(time.Duration(newTokens.ExpiresIn) * time.Second) + sessionsMutex.Unlock() } posts, err := fetchers.FetchAppBskyFeedPosts(session.DID, session.PDS, session.AccessToken, session.KeyPair) |
