35 lines
1.1 KiB
Go
35 lines
1.1 KiB
Go
package auth
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
|
|
abclientstate "github.com/volatiletech/authboss-clientstate"
|
|
abrenderer "github.com/volatiletech/authboss-renderer"
|
|
"github.com/volatiletech/authboss/v3"
|
|
"github.com/volatiletech/authboss/v3/defaults"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
)
|
|
|
|
func NewAuthBoss(rootURL, cookieName, mountPath string, mongo *mongo.Client) (*authboss.Authboss, error) {
|
|
key := make([]byte, 32)
|
|
if _, err := rand.Read(key); err != nil {
|
|
return nil, fmt.Errorf(`generating pseudo-random auth key: %w`, err)
|
|
}
|
|
|
|
ab := authboss.New()
|
|
ab.Config.Storage.Server = NewMongoStorer(mongo)
|
|
ab.Config.Storage.SessionState = abclientstate.NewSessionStorer(cookieName, key, nil)
|
|
ab.Config.Storage.CookieState = abclientstate.NewCookieStorer([]byte(`key`), nil)
|
|
|
|
ab.Config.Paths.Mount = `/authboss`
|
|
ab.Config.Paths.RootURL = rootURL
|
|
|
|
ab.Config.Core.ViewRenderer = abrenderer.NewHTML(mountPath, `ab_views`)
|
|
defaults.SetCore(&ab.Config, false, false)
|
|
|
|
if err := ab.Init(); err != nil {
|
|
return nil, fmt.Errorf("initializing Authboss: %w", err)
|
|
}
|
|
return ab, nil
|
|
}
|