Skip to content

Commit

Permalink
added more routes
Browse files Browse the repository at this point in the history
  • Loading branch information
RabbITCybErSeC committed Sep 22, 2024
1 parent 5eb2a4c commit dae7dce
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 14 deletions.
6 changes: 3 additions & 3 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ type Authenticator struct {
verifierProvider *oidc.Provider
}

func SetupAuthHandler() *Authenticator {
func SetupOIDCAuthHandler() *Authenticator {
providerLink := utils.GetEnv("OIDC_PROVIDER", "")
clientID := utils.GetEnv("OIDC_CLIENT_ID", "")
clientSecret := utils.GetEnv("OIDC_CLIENT_SECRET", "")
redirectURL := utils.GetEnv("OIDC_REDIRECT_URL", "")
cookieJarSecret := utils.GetEnv("COOKIE_SECRET", "")
cookieJarSecret := utils.GetEnv("COOKIE_SECRET_KEY", "")
if providerLink == "" {
log.Fatal("invalid provider link for the env: OIDC_PROVIDER")
}
Expand All @@ -36,7 +36,7 @@ func SetupAuthHandler() *Authenticator {
log.Fatal("invalid redirect URL for the env: OIDC_REDIRECT_URL")
}
if cookieJarSecret == "" || len(cookieJarSecret) < 32 {
log.Fatal("invalid cookie secret key for the env: COOKIE_SECRET_KEY. Note: should be at leat 32 characters")
log.Fatal("invalid cookie secret key for the env: COOKIE_SECRET_KEY. Note: should be at least 32 characters")
}

ctx := context.Background()
Expand Down
1 change: 0 additions & 1 deletion auth/cookies/cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
type ICookieJar interface {
SetCallBackCookie(*gin.Context, string, string)
}

type CookieJar struct {
store sessions.Store
}
Expand Down
2 changes: 1 addition & 1 deletion handlers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package handlers
import (
"errors"
"net/http"
"soarca-gui/utils"
"strings"

"soarca-gui/utils"
authviews "soarca-gui/views/auth"

"github.com/gin-gonic/gin"
Expand Down
27 changes: 27 additions & 0 deletions handlers/oidc_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package handlers

import (
"net/http"
"soarca-gui/auth"
"soarca-gui/utils"

authviews "soarca-gui/views/auth"

"github.com/gin-gonic/gin"
)

type OIDCAuthHandler struct {
authenticator *auth.Authenticator
}

func NewOIDCAuthHanlder(authenticator *auth.Authenticator) *OIDCAuthHandler {
return &OIDCAuthHandler{authenticator: authenticator}
}

func (a *OIDCAuthHandler) OIDCAuthPageHandler(context *gin.Context) {
render := utils.NewTempl(context, http.StatusOK, authviews.OIDCLoginIndex())
context.Render(http.StatusOK, render)
}

func (a *OIDCAuthHandler) OIDCLoginHandler(context *gin.Context) {
}
36 changes: 29 additions & 7 deletions routes/routes.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package routes

import (
"fmt"
"log"
"net/http"

"soarca-gui/auth"
"soarca-gui/backend"
"soarca-gui/backend/soarca"
"soarca-gui/handlers"
"soarca-gui/public"
"soarca-gui/utils"
"strconv"

"github.com/gin-gonic/gin"
)
Expand All @@ -19,28 +22,47 @@ func Setup(app *gin.Engine) {
})

reporter := soarca.NewReport(utils.GetEnv("SOARCA_URI", "http://localhost:8080"), &http.Client{})

status := soarca.NewStatus(utils.GetEnv("SOARCA_URI", "http://localhost:8080"), &http.Client{})

authEnabledStr := utils.GetEnv("AUTH_ENABLED", "false")
authEnabled, err := strconv.ParseBool(authEnabledStr)
publicRoutes := app.Group("/")

PublicRoutes(publicRoutes)
fmt.Println(authEnabled)
if err != nil {
log.Fatal("AUTH_ENABLED flag could not be parsed properly should be 'true' | 'false'")
}
if authEnabled {
PublicOIDCRoutes(publicRoutes)
} else {
PublicRoutes(publicRoutes)
}
ReportingRoutes(reporter, publicRoutes)
// PublicRoutes(publicRoutes)
StatusRoutes(status, publicRoutes)
SettingsRoutes(publicRoutes)
}

func PublicOIDCRoutes(app *gin.RouterGroup) {
auth := auth.SetupOIDCAuthHandler()
authHandler := handlers.NewOIDCAuthHanlder(auth)
publicRoute := app.Group("/")
{
publicRoute.GET("/", authHandler.OIDCAuthPageHandler)
publicRoute.POST("/login-redirect", authHandler.OIDCLoginHandler)
publicRoute.GET("/dashboard", handlers.HomeDashboard)

}
publicRoute.StaticFS("/public", public.GetPublicAssetsFileSystem())
}

func PublicRoutes(app *gin.RouterGroup) {
authHandler := handlers.AuthHandler{}

publicRoute := app.Group("/")
{
publicRoute.GET("/", authHandler.AuthPage)
publicRoute.POST("/login", authHandler.Login)
publicRoute.GET("/dashboard", handlers.HomeDashboard)

}

publicRoute.StaticFS("/public", public.GetPublicAssetsFileSystem())
}

Expand Down
4 changes: 2 additions & 2 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"

"soarca-gui/internal/status"
"soarca-gui/routes"
"soarca-gui/utils"
Expand All @@ -21,8 +20,9 @@ var (
func main() {
fmt.Println("Version: ", Version)
fmt.Println("Buildtime ", Buildtime)
errenv := godotenv.Load(".env")
// errenv := godotenv.Load(".env")

errenv := godotenv.Load(".env.example")
if errenv != nil {
fmt.Println("Failed to read env variable, but will continue")
}
Expand Down
68 changes: 68 additions & 0 deletions views/auth/oidc_login.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package authviews

import (
"soarca-gui/views/layouts"
)


templ OIDCLoginIndex() {
@OIDCLoginBaseLayout() {
@OIDCLoginForm()
}
}

templ OIDCLoginBaseLayout() {
@layouts.BaseLayout() {
<body class="min-h-screen bg-gray-100 dark:bg-slate-800 flex flex-col justify-center sm:py-12 font-family-sans-serif">
<div class="p-10 xs:p-0 mx-auto md:w-full md:max-w-md">
<a href={ templ.URL(homeLink) } class="text-white text-3xl font-semibold uppercase hover:text-gray-300">
<img src={ soarcaLogoUrlPath } class="w-30 py-3 md:py-0 g-image"/>
</a>
<div class="bg-white shadow w-full rounded-lg divide-y divide-gray-200">
<div id="extra-information" class="px-5 py-7">
// will be filled when error message
</div>
<div class="bg-white shadow w-full rounded-lg divide-y divide-gray-200">
{ children... }
</div>
<div class="py-5">
<div class="grid grid-cols-2 gap-1">
<div class="text-center sm:text-left whitespace-nowrap">
<button class="transition duration-200 mx-5 px-5 py-4 cursor-pointer font-normal text-sm rounded-lg text-gray-500 hover:bg-gray-100 focus:outline-none focus:bg-gray-200 focus:ring-2 focus:ring-gray-400 focus:ring-opacity-50 ring-inset">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" class="w-4 h-4 inline-block align-text-top">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 11V7a4 4 0 118 0m-4 8v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2z"></path>
</svg>
<span class="inline-block ml-1">Forgot Password</span>
</button>
</div>
<div class="text-center sm:text-right whitespace-nowrap">
<button class="transition duration-200 mx-5 px-5 py-4 cursor-pointer font-normal text-sm rounded-lg text-gray-500 hover:bg-gray-100 focus:outline-none focus:bg-gray-200 focus:ring-2 focus:ring-gray-400 focus:ring-opacity-50 ring-inset">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" class="w-4 h-4 inline-block align-text-bottom ">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M18.364 5.636l-3.536 3.536m0 5.656l3.536 3.536M9.172 9.172L5.636 5.636m3.536 9.192l-3.536 3.536M21 12a9 9 0 11-18 0 9 9 0 0118 0zm-5 0a4 4 0 11-8 0 4 4 0 018 0z"></path>
</svg>
<span class="inline-block ml-1">Help</span>
</button>
</div>
</div>
</div>
</div>
</div>
</body>
}
}


templ OIDCLoginForm() {
<div class="px-5 py-7">
<button
hx-get="/oidc-login"
class="transition duration-200 bg-blue-500 hover:bg-blue-600 focus:bg-blue-700 focus:shadow-sm focus:ring-4 focus:ring-blue-500 focus:ring-opacity-50 text-white w-full py-2.5 rounded-lg text-sm shadow-sm hover:shadow-md font-semibold text-center inline-block"
>
<span class="inline-block mr-2">Login with OIDC</span>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" class="w-4 h-4 inline-block">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 16l-4-4m0 0l4-4m-4 4h14m-5 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h7a3 3 0 013 3v1"></path>
</svg>
</button>
</div>
}

0 comments on commit dae7dce

Please sign in to comment.