Add custom session
This commit is contained in:
parent
992ba29848
commit
e0ebf2e6bb
@ -2,17 +2,21 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/joho/godotenv"
|
||||||
"gothtest/internal/server"
|
"gothtest/internal/server"
|
||||||
"gothtest/internal/auth"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
err := godotenv.Load()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Error loading .env file")
|
||||||
|
}
|
||||||
|
|
||||||
server := server.NewServer()
|
server := server.NewServer()
|
||||||
auth.NewAuth()
|
|
||||||
|
|
||||||
fmt.Println("Server start")
|
fmt.Println("Server start")
|
||||||
err := server.ListenAndServe()
|
err = server.ListenAndServe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("cannot start server: %s", err))
|
panic(fmt.Sprintf("cannot start server: %s", err))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,40 +1,29 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/gorilla/sessions"
|
||||||
|
"github.com/markbates/goth"
|
||||||
|
"github.com/markbates/goth/gothic"
|
||||||
|
"github.com/markbates/goth/providers/openidConnect"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
//"github.com/gorilla/sessions"
|
|
||||||
"github.com/joho/godotenv"
|
|
||||||
"github.com/markbates/goth"
|
|
||||||
//"github.com/markbates/goth/gothic"
|
|
||||||
"github.com/markbates/goth/providers/openidConnect"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
func NewAuth(sessionStore *sessions.Store) {
|
||||||
key = "iebdyjckwoevhdixnwgwunrvxuqobetgy"
|
|
||||||
MaxAge = 86400 * 30
|
|
||||||
IsProd = false
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewAuth() {
|
|
||||||
err := godotenv.Load()
|
|
||||||
if err != nil {log.Fatal("Error loading .env file")}
|
|
||||||
|
|
||||||
oidcId := os.Getenv("OIDC_ID")
|
oidcId := os.Getenv("OIDC_ID")
|
||||||
oidcSec := os.Getenv("OIDC_SECRET")
|
oidcSec := os.Getenv("OIDC_SECRET")
|
||||||
oidcDiscUrl := os.Getenv("OIDC_DISC_URL")
|
oidcDiscUrl := os.Getenv("OIDC_DISC_URL")
|
||||||
oidcRedirectUrl := "http://localhost:3003/auth/openid-connect/callback"
|
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 {
|
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"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/sessions"
|
||||||
_ "github.com/joho/godotenv/autoload"
|
_ "github.com/joho/godotenv/autoload"
|
||||||
|
|
||||||
|
"gothtest/internal/auth"
|
||||||
"gothtest/internal/database"
|
"gothtest/internal/database"
|
||||||
|
"gothtest/internal/session"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
port int
|
port int
|
||||||
|
|
||||||
db database.Service
|
db database.Service
|
||||||
|
store sessions.Store
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer() *http.Server {
|
func NewServer() *http.Server {
|
||||||
@ -24,7 +28,9 @@ func NewServer() *http.Server {
|
|||||||
port: port,
|
port: port,
|
||||||
|
|
||||||
db: database.New(),
|
db: database.New(),
|
||||||
|
store: session.New(),
|
||||||
}
|
}
|
||||||
|
auth.NewAuth(&NewServer.store)
|
||||||
|
|
||||||
// Declare Server config
|
// Declare Server config
|
||||||
server := &http.Server{
|
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