Client key and secret Oauth in config

This commit is contained in:
Thomas Miceli
2023-04-17 19:11:32 +02:00
parent a6c5696ceb
commit 4008b7ce38
5 changed files with 64 additions and 16 deletions

View File

@ -6,11 +6,15 @@ import (
"errors"
"fmt"
"github.com/labstack/echo/v4"
"github.com/markbates/goth"
"github.com/markbates/goth/gothic"
"github.com/markbates/goth/providers/gitea"
"github.com/markbates/goth/providers/github"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
"io"
"net/http"
"opengist/internal/config"
"opengist/internal/models"
"strings"
)
@ -213,6 +217,39 @@ func oauthCallback(ctx echo.Context) error {
func oauth(ctx echo.Context) error {
provider := ctx.Param("provider")
httpProtocol := "http"
if ctx.Request().TLS != nil || ctx.Request().Header.Get("X-Forwarded-Proto") == "https" {
httpProtocol = "https"
}
httpDomain := httpProtocol + "://" + ctx.Request().Host
giteaUrl := config.C.GiteaUrl
// remove trailing slash
if giteaUrl[len(giteaUrl)-1] == '/' {
giteaUrl = giteaUrl[:len(giteaUrl)-1]
}
switch provider {
case "github":
goth.UseProviders(
github.New(
config.C.GithubClientKey,
config.C.GithubSecret,
httpDomain+"/oauth/github/callback"),
)
case "gitea":
goth.UseProviders(
gitea.NewCustomisedURL(
config.C.GiteaClientKey,
config.C.GiteaSecret,
httpDomain+"/oauth/gitea/callback",
giteaUrl+"/login/oauth/authorize",
giteaUrl+"/login/oauth/access_token",
giteaUrl+"/api/v1/user"),
)
}
currUser := getUserLogged(ctx)
if currUser != nil {
isDelete := false
@ -221,12 +258,12 @@ func oauth(ctx echo.Context) error {
case "github":
if currUser.GithubID != "" {
isDelete = true
err = currUser.DeleteProvider(provider)
err = currUser.DeleteProviderID(provider)
}
case "gitea":
if currUser.GiteaID != "" {
isDelete = true
err = currUser.DeleteProvider(provider)
err = currUser.DeleteProviderID(provider)
}
}

View File

@ -8,10 +8,7 @@ import (
"github.com/gorilla/sessions"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/markbates/goth"
"github.com/markbates/goth/gothic"
"github.com/markbates/goth/providers/gitea"
"github.com/markbates/goth/providers/github"
"github.com/rs/zerolog/log"
"html/template"
"io"
@ -101,16 +98,7 @@ func (t *Template) Render(w io.Writer, name string, data interface{}, _ echo.Con
func Start() {
store = sessions.NewCookieStore([]byte("opengist"))
gothic.Store = store
goth.UseProviders(
github.New("d92c7e165383b2804407", "ffc450216b9776a752cdb0e533f953f65ce632a3", "http://localhost:6157/oauth/github/callback"),
gitea.NewCustomisedURL(
"efdd0fed-6972-42ce-8f9f-e65b9fd0ca09",
"gto_dwilh6ia4nic4f4dt5owv4h7rvuss5ajw2ctqqa44xcpwevyg6wq",
"http://localhost:6157/oauth/gitea/callback",
"http://localhost:3000/login/oauth/authorize",
"http://localhost:3000/login/oauth/access_token",
"http://localhost:3000/api/v1/user"),
)
assetsFS := echo.MustSubFS(EmbedFS, "public/assets")
e := echo.New()