This commit is contained in:
@@ -1,76 +0,0 @@
|
||||
package apple
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestAppleLogin(t *testing.T) {
|
||||
t.Skipf("Skip TestAppleLogin test")
|
||||
router := gin.Default()
|
||||
router.LoadHTMLGlob("./*")
|
||||
router.GET("/apple", func(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "apple.html", gin.H{
|
||||
"title": "Gin HTML Example",
|
||||
"message": "Hello, Gin!",
|
||||
})
|
||||
})
|
||||
router.POST("/auth/apple/callback", func(c *gin.Context) {
|
||||
var req CallbackRequest
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request data"})
|
||||
return
|
||||
}
|
||||
handleAppleCallBack(c, req)
|
||||
})
|
||||
_ = router.RunTLS(":8443", "certificate.crt", "private.key")
|
||||
}
|
||||
|
||||
func handleAppleCallBack(ctx context.Context, request CallbackRequest) {
|
||||
fmt.Printf("request: %+v\n", request)
|
||||
// validate the token
|
||||
client, err := New(Config{
|
||||
TeamID: TeamID,
|
||||
ClientID: ClientID,
|
||||
KeyID: KeyID,
|
||||
ClientSecret: ClientSecret,
|
||||
RedirectURI: "https://test.ppanel.dev:8443/auth/apple/callback",
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println("error creating apple client: " + err.Error())
|
||||
return
|
||||
}
|
||||
resp, err := client.VerifyWebToken(ctx, request.Code)
|
||||
if err != nil {
|
||||
fmt.Println("error verifying token: " + err.Error())
|
||||
return
|
||||
}
|
||||
if resp.Error != "" {
|
||||
fmt.Printf("apple returned an error: %s - %s\n", resp.Error, resp.ErrorDescription)
|
||||
return
|
||||
}
|
||||
|
||||
// Get the unique user ID
|
||||
unique, err := GetUniqueID(resp.IDToken)
|
||||
if err != nil {
|
||||
fmt.Println("error getting unique id: " + err.Error())
|
||||
return
|
||||
}
|
||||
// Get the email
|
||||
claim, err := GetClaims(resp.IDToken)
|
||||
if err != nil {
|
||||
fmt.Println("failed to get claims: " + err.Error())
|
||||
return
|
||||
}
|
||||
email := (*claim)["email"]
|
||||
emailVerified := (*claim)["email_verified"]
|
||||
isPrivateEmail := (*claim)["is_private_email"]
|
||||
|
||||
// Voila!
|
||||
log.Printf("\n unique: %s \n email: %s \n email_verified: %v \n is_private_email: %v", unique, email, emailVerified, isPrivateEmail)
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
package google
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
func TestGoogleOAuth(t *testing.T) {
|
||||
t.Skipf("Skip TestGoogleOAuth test")
|
||||
http.HandleFunc("/", handleMain)
|
||||
http.HandleFunc("/login", handleLogin)
|
||||
http.HandleFunc("/auth", handleCallback)
|
||||
http.HandleFunc("/user", handleAuth)
|
||||
|
||||
fmt.Println("Server is running on http://localhost:3001")
|
||||
log.Fatal(http.ListenAndServe(":3001", nil))
|
||||
}
|
||||
|
||||
func handleMain(w http.ResponseWriter, r *http.Request) {
|
||||
html := `<html>
|
||||
<body>
|
||||
<a href="/login">Log in with Google</a>
|
||||
</body>
|
||||
</html>`
|
||||
fmt.Fprint(w, html)
|
||||
}
|
||||
|
||||
func handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
oauthConfig := New(&Config{
|
||||
ClientID: "",
|
||||
ClientSecret: "",
|
||||
RedirectURL: "http://localhost:3001/auth",
|
||||
})
|
||||
url := oauthConfig.AuthCodeURL("randomstate", oauth2.AccessTypeOffline)
|
||||
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
|
||||
}
|
||||
|
||||
func handleCallback(w http.ResponseWriter, r *http.Request) {
|
||||
if r.FormValue("state") != "randomstate" {
|
||||
http.Error(w, "State is invalid", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("url: %v", r.URL)
|
||||
|
||||
oauthConfig := New(&Config{
|
||||
ClientID: "",
|
||||
ClientSecret: "Key",
|
||||
RedirectURL: "http://localhost:3001/auth",
|
||||
})
|
||||
code := r.FormValue("code")
|
||||
token, err := oauthConfig.Exchange(context.Background(), code)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to exchange token", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/user?token="+token.AccessToken, http.StatusTemporaryRedirect)
|
||||
}
|
||||
|
||||
func handleAuth(w http.ResponseWriter, r *http.Request) {
|
||||
token := r.FormValue("token")
|
||||
client := New(&Config{
|
||||
ClientID: "Id",
|
||||
ClientSecret: "Key",
|
||||
RedirectURL: "http://localhost:3001/auth",
|
||||
})
|
||||
userInfo, err := client.GetUserInfo(token)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to get user info", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "Hello, %s", userInfo.Name)
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestOAuth(t *testing.T) {
|
||||
t.Skipf("Skip TestOAuth test")
|
||||
router := gin.Default()
|
||||
router.LoadHTMLGlob("./*")
|
||||
router.GET("/telegram", func(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "telegram.html", gin.H{
|
||||
"title": "Gin HTML Example",
|
||||
"message": "Hello, Gin!",
|
||||
})
|
||||
})
|
||||
router.GET("/auth/telegram/callback", func(c *gin.Context) {
|
||||
|
||||
})
|
||||
_ = router.RunTLS(":443", "server.crt", "server.key")
|
||||
}
|
||||
|
||||
func TestBase64(t *testing.T) {
|
||||
text := "eyJpZCI6ODI0NjI2ODAzLCJmaXJzdF9uYW1lIjoiQ2hhbmcgbHVlIiwibGFzdF9uYW1lIjoiVHNlbiIsInVzZXJuYW1lIjoidGVuc2lvbl9jIiwicGhvdG9fdXJsIjoiaHR0cHM6XC9cL3QubWVcL2lcL3VzZXJwaWNcLzMyMFwvYU1LNkhEc0pqc2V1YldRYmt2NGlYOHZCRUF6N0hWU3g3dkFuRDBLZ0tFVS5qcGciLCJhdXRoX2RhdGUiOjE3Mzc4MTkwNzQsImhhc2giOiI5M2I1ZDg3Zjc3NjE2YjBjMTM0OTAxYmYwMDg3MTc4YjJiYmZlYzA1MTlkMWVmMDJhZjFjMGNlOTAzM2ZiNGFlIn0"
|
||||
var token = "7651491571:AAEVQma6niHhtqEYDowAEpPo6Fq69BWvRU8"
|
||||
|
||||
data, err := ParseAndValidateBase64([]byte(text), token)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
t.Log(*data.Id)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user