actually do some OAuth
This commit is contained in:
+42
-1
@@ -1,18 +1,59 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"astraltech.xyz/calendar/v2/webserver"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
func generateState() (string, error) {
|
||||
b := make([]byte, 32)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString(b), nil
|
||||
}
|
||||
|
||||
func HandlePasswordAuth(authData webserver.PasswordAuthData) bool {
|
||||
fmt.Printf("%s, %s\n", authData.Username, authData.Password)
|
||||
return false
|
||||
}
|
||||
|
||||
func HandleOAuth(authData webserver.OAuthAuthData) bool {
|
||||
fmt.Printf("doing an OAuth")
|
||||
state, err := generateState()
|
||||
if err != nil {
|
||||
http.Error(*authData.ResponseWriter, "Failed to generate state", http.StatusInternalServerError)
|
||||
return false
|
||||
}
|
||||
verifier := oauth2.GenerateVerifier()
|
||||
|
||||
http.SetCookie(*authData.ResponseWriter, &http.Cookie{
|
||||
Name: "oauth_state",
|
||||
Value: state,
|
||||
Path: "/",
|
||||
MaxAge: 300,
|
||||
HttpOnly: true,
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
})
|
||||
|
||||
http.SetCookie(*authData.ResponseWriter, &http.Cookie{
|
||||
Name: "pkce_verifier",
|
||||
Value: verifier,
|
||||
Path: "/",
|
||||
HttpOnly: true,
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
})
|
||||
|
||||
url := OAuthConfigs[0].AuthCodeURL(
|
||||
state,
|
||||
oauth2.S256ChallengeOption(verifier),
|
||||
)
|
||||
http.Redirect(*authData.ResponseWriter, authData.Request, url, http.StatusFound)
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user