Add custom session
This commit is contained in:
parent
992ba29848
commit
e0ebf2e6bb
@ -2,17 +2,21 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/joho/godotenv"
|
||||
"gothtest/internal/server"
|
||||
"gothtest/internal/auth"
|
||||
"log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
|
||||
server := server.NewServer()
|
||||
auth.NewAuth()
|
||||
|
||||
fmt.Println("Server start")
|
||||
err := server.ListenAndServe()
|
||||
fmt.Println("Server start")
|
||||
err = server.ListenAndServe()
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("cannot start server: %s", err))
|
||||
}
|
||||
|
||||
@ -1,40 +1,29 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
//"github.com/gorilla/sessions"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/markbates/goth"
|
||||
//"github.com/markbates/goth/gothic"
|
||||
"github.com/markbates/goth/providers/openidConnect"
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/markbates/goth"
|
||||
"github.com/markbates/goth/gothic"
|
||||
"github.com/markbates/goth/providers/openidConnect"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
key = "iebdyjckwoevhdixnwgwunrvxuqobetgy"
|
||||
MaxAge = 86400 * 30
|
||||
IsProd = false
|
||||
)
|
||||
func NewAuth(sessionStore *sessions.Store) {
|
||||
oidcId := os.Getenv("OIDC_ID")
|
||||
oidcSec := os.Getenv("OIDC_SECRET")
|
||||
oidcDiscUrl := os.Getenv("OIDC_DISC_URL")
|
||||
oidcRedirectUrl := "http://localhost:3003/auth/openid-connect/callback"
|
||||
|
||||
func NewAuth() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {log.Fatal("Error loading .env file")}
|
||||
|
||||
oidcId := os.Getenv("OIDC_ID")
|
||||
oidcSec := os.Getenv("OIDC_SECRET")
|
||||
oidcDiscUrl := os.Getenv("OIDC_DISC_URL")
|
||||
oidcRedirectUrl := "http://localhost:3003/auth/openid-connect/callback"
|
||||
/*
|
||||
store := sessions.NewCookieStore([]byte(key))
|
||||
store.MaxAge(MaxAge)
|
||||
store.Options.Path = "/"
|
||||
store.Options.HttpOnly = true
|
||||
store.Options.Secure = IsProd
|
||||
|
||||
gothic.Store = store
|
||||
*/
|
||||
openidConnect, err := openidConnect.New(oidcId, oidcSec, oidcRedirectUrl, oidcDiscUrl)
|
||||
openidConnect, err := openidConnect.New(oidcId, oidcSec, oidcRedirectUrl, oidcDiscUrl)
|
||||
if openidConnect == nil || err != nil {
|
||||
log.Fatal("Error setting up oidc")
|
||||
log.Fatal("Error setting up oidc")
|
||||
}
|
||||
goth.UseProviders(openidConnect)
|
||||
goth.UseProviders(openidConnect)
|
||||
|
||||
if sessionStore != nil {
|
||||
gothic.Store = *sessionStore
|
||||
} else {
|
||||
log.Println("No auth session store set. Falling back to default gothic setting.")
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,15 +7,19 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
|
||||
"gothtest/internal/auth"
|
||||
"gothtest/internal/database"
|
||||
"gothtest/internal/session"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
port int
|
||||
|
||||
db database.Service
|
||||
db database.Service
|
||||
store sessions.Store
|
||||
}
|
||||
|
||||
func NewServer() *http.Server {
|
||||
@ -23,8 +27,10 @@ func NewServer() *http.Server {
|
||||
NewServer := &Server{
|
||||
port: port,
|
||||
|
||||
db: database.New(),
|
||||
db: database.New(),
|
||||
store: session.New(),
|
||||
}
|
||||
auth.NewAuth(&NewServer.store)
|
||||
|
||||
// Declare Server config
|
||||
server := &http.Server{
|
||||
|
||||
25
internal/session/session.go
Normal file
25
internal/session/session.go
Normal file
@ -0,0 +1,25 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"github.com/gorilla/sessions"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxAge = 86400 * 30
|
||||
IsProd = false
|
||||
)
|
||||
|
||||
func New() sessions.Store {
|
||||
key := os.Getenv("SESSION_SECRET")
|
||||
if key == "" {
|
||||
return nil
|
||||
}
|
||||
store := sessions.NewCookieStore([]byte(key))
|
||||
store.MaxAge(MaxAge)
|
||||
store.Options.Path = "/"
|
||||
store.Options.HttpOnly = true
|
||||
store.Options.Secure = IsProd
|
||||
|
||||
return store
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user