get token from OAuth provider
This commit is contained in:
@@ -1,2 +1,3 @@
|
|||||||
config.json
|
config.json
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
token.json
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import (
|
|||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var tokenStore TokenStore
|
||||||
|
|
||||||
func generateState() (string, error) {
|
func generateState() (string, error) {
|
||||||
b := make([]byte, 32)
|
b := make([]byte, 32)
|
||||||
if _, err := rand.Read(b); err != nil {
|
if _, err := rand.Read(b); err != nil {
|
||||||
|
|||||||
@@ -28,4 +28,5 @@ func CreateTestOAuth() {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
OAuthConfigs = append(OAuthConfigs, oauthConfig)
|
OAuthConfigs = append(OAuthConfigs, oauthConfig)
|
||||||
|
tokenStore = TokenStore{filePath: "token.json"}
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-2
@@ -1,8 +1,11 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"context"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"golang.org/x/oauth2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// this is usually served at /callback
|
// this is usually served at /callback
|
||||||
@@ -27,5 +30,24 @@ func OAuthCallback(w http.ResponseWriter, r *http.Request) {
|
|||||||
MaxAge: -1,
|
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