mirror of
https://github.com/khairul169/vaulterm.git
synced 2026-09-15 17:03:30 +07:00
fix: native gitlab & github login
This commit is contained in:
@@ -27,28 +27,26 @@ func getGithubConfig() *oauth2.Config {
|
||||
ClientID: os.Getenv("GITHUB_CLIENT_ID"),
|
||||
ClientSecret: os.Getenv("GITHUB_CLIENT_SECRET"),
|
||||
Endpoint: github.Endpoint,
|
||||
// RedirectURL: "http://localhost:3000/auth/oauth/github/callback",
|
||||
RedirectURL: "http://localhost:8081",
|
||||
Scopes: []string{"read:user"},
|
||||
RedirectURL: "vaulterm://auth/login",
|
||||
Scopes: []string{"read:user"},
|
||||
}
|
||||
return githubCfg
|
||||
}
|
||||
|
||||
func githubRedir(c *fiber.Ctx) error {
|
||||
// Redirect to GitHub login page
|
||||
url := getGithubConfig().AuthCodeURL("state", oauth2.AccessTypeOffline)
|
||||
return c.Redirect(url)
|
||||
}
|
||||
|
||||
func githubCallback(c *fiber.Ctx) error {
|
||||
code := c.Query("code")
|
||||
if code == "" {
|
||||
var body struct {
|
||||
Code string `json:"code"`
|
||||
}
|
||||
if err := c.BodyParser(&body); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).SendString("Failed to parse request body")
|
||||
}
|
||||
if body.Code == "" {
|
||||
return c.Status(fiber.StatusBadRequest).SendString("Missing code")
|
||||
}
|
||||
|
||||
// Exchange code for a token
|
||||
cfg := getGithubConfig()
|
||||
token, err := cfg.Exchange(c.Context(), code)
|
||||
token, err := cfg.Exchange(c.Context(), body.Code)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).SendString("Failed to exchange token")
|
||||
}
|
||||
@@ -61,10 +59,10 @@ func githubCallback(c *fiber.Ctx) error {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
data, _ := io.ReadAll(resp.Body)
|
||||
if resp.StatusCode != 200 {
|
||||
return c.Status(fiber.StatusInternalServerError).
|
||||
SendString(fmt.Sprintf("GitHub API error: %s", string(body)))
|
||||
SendString(fmt.Sprintf("GitHub API error: %s", string(data)))
|
||||
}
|
||||
|
||||
// Parse user info
|
||||
@@ -76,7 +74,7 @@ func githubCallback(c *fiber.Ctx) error {
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(body, &user); err != nil {
|
||||
if err := json.Unmarshal(data, &user); err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).SendString("Failed to parse user info")
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
@@ -17,61 +16,39 @@ import (
|
||||
"rul.sh/vaulterm/server/utils"
|
||||
)
|
||||
|
||||
type GitlabCfg struct {
|
||||
oauth2.Config
|
||||
verifier string
|
||||
challenge string
|
||||
}
|
||||
var gitlabCfg *oauth2.Config
|
||||
|
||||
var gitlabCfg *GitlabCfg
|
||||
|
||||
func getGitlabConfig() *GitlabCfg {
|
||||
func getGitlabConfig() *oauth2.Config {
|
||||
if gitlabCfg != nil {
|
||||
return gitlabCfg
|
||||
}
|
||||
|
||||
oauthCfg := oauth2.Config{
|
||||
gitlabCfg = &oauth2.Config{
|
||||
ClientID: os.Getenv("GITLAB_CLIENT_ID"),
|
||||
ClientSecret: os.Getenv("GITLAB_CLIENT_SECRET"),
|
||||
Endpoint: gitlab.Endpoint,
|
||||
// RedirectURL: "http://localhost:3000/auth/oauth/gitlab/callback",
|
||||
RedirectURL: "http://localhost:8081",
|
||||
Scopes: []string{"read_user"},
|
||||
}
|
||||
verifier := oauth2.GenerateVerifier()
|
||||
challenge := oauth2.S256ChallengeFromVerifier(verifier)
|
||||
|
||||
gitlabCfg = &GitlabCfg{
|
||||
Config: oauthCfg,
|
||||
verifier: verifier,
|
||||
challenge: challenge,
|
||||
RedirectURL: "vaulterm://auth/login",
|
||||
Scopes: []string{"read_user"},
|
||||
}
|
||||
return gitlabCfg
|
||||
}
|
||||
|
||||
func gitlabRedir(c *fiber.Ctx) error {
|
||||
// Redirect to Gitlab login page
|
||||
url := getGitlabConfig().
|
||||
AuthCodeURL("login", oauth2.S256ChallengeOption(getGitlabConfig().verifier))
|
||||
return c.Redirect(url)
|
||||
}
|
||||
|
||||
func gitlabCallback(c *fiber.Ctx) error {
|
||||
cfg := getGitlabConfig()
|
||||
code := c.Query("code")
|
||||
verifier := c.Query("verifier")
|
||||
|
||||
if code == "" {
|
||||
return c.Status(fiber.StatusBadRequest).SendString("Missing code")
|
||||
var body struct {
|
||||
Code string `json:"code"`
|
||||
Verifier string `json:"verifier"`
|
||||
}
|
||||
if verifier == "" {
|
||||
verifier = cfg.verifier
|
||||
if err := c.BodyParser(&body); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).SendString("Failed to parse request body")
|
||||
}
|
||||
if body.Code == "" || body.Verifier == "" {
|
||||
return c.Status(fiber.StatusBadRequest).SendString("Missing code or verifier")
|
||||
}
|
||||
|
||||
// Exchange code for a token
|
||||
token, err := cfg.Exchange(c.Context(), code, oauth2.VerifierOption(verifier))
|
||||
cfg := getGitlabConfig()
|
||||
token, err := cfg.Exchange(c.Context(), body.Code, oauth2.VerifierOption(body.Verifier))
|
||||
if err != nil {
|
||||
log.Println(token, err)
|
||||
return c.Status(fiber.StatusInternalServerError).SendString("Failed to exchange token")
|
||||
}
|
||||
|
||||
@@ -83,10 +60,10 @@ func gitlabCallback(c *fiber.Ctx) error {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
data, _ := io.ReadAll(resp.Body)
|
||||
if resp.StatusCode != 200 {
|
||||
return c.Status(fiber.StatusInternalServerError).
|
||||
SendString(fmt.Sprintf("Gitlab API error: %s", string(body)))
|
||||
SendString(fmt.Sprintf("Gitlab API error: %s", string(data)))
|
||||
}
|
||||
|
||||
// Parse user info
|
||||
@@ -98,7 +75,7 @@ func gitlabCallback(c *fiber.Ctx) error {
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(body, &user); err != nil {
|
||||
if err := json.Unmarshal(data, &user); err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).SendString("Failed to parse user info")
|
||||
}
|
||||
|
||||
|
||||
@@ -16,11 +16,8 @@ func Router(app *fiber.App) {
|
||||
router.Post("/register", register)
|
||||
router.Post("/logout", middleware.Protected(), logout)
|
||||
|
||||
oauth := router.Group("/oauth")
|
||||
oauth.Get("/github", githubRedir)
|
||||
oauth.Get("/github/callback", githubCallback)
|
||||
oauth.Get("/gitlab", gitlabRedir)
|
||||
oauth.Get("/gitlab/callback", gitlabCallback)
|
||||
router.Post("/github", githubCallback)
|
||||
router.Post("/gitlab", gitlabCallback)
|
||||
}
|
||||
|
||||
func login(c *fiber.Ctx) error {
|
||||
|
||||
Reference in New Issue
Block a user