get token from OAuth provider
This commit is contained in:
+24
-2
@@ -1,8 +1,11 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
// this is usually served at /callback
|
||||
@@ -27,5 +30,24 @@ func OAuthCallback(w http.ResponseWriter, r *http.Request) {
|
||||
MaxAge: -1,
|
||||
})
|
||||
|
||||
fmt.Printf("Handling some OAuth, %s\n", code)
|
||||
verifierCookie, err := r.Cookie("pkce_verifier")
|
||||
if err != nil {
|
||||
http.Error(w, "Missing PKCE verifier", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
verifier := verifierCookie.Value
|
||||
|
||||
token, err := OAuthConfigs[0].Exchange(context.Background(), code, oauth2.VerifierOption(verifier))
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to exchange token: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Store the token securely
|
||||
if err := tokenStore.Save(token); err != nil {
|
||||
log.Printf("Failed to save token: %v", err)
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/user", http.StatusTemporaryRedirect)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user