godash/handlers/auth.handlers.go

40 lines
1.1 KiB
Go
Raw Normal View History

2024-03-18 21:54:35 +01:00
package handlers
import (
2024-03-27 22:43:36 +01:00
"context"
2024-03-18 21:54:35 +01:00
"log/slog"
2024-03-27 22:43:36 +01:00
"os"
2024-03-18 21:54:35 +01:00
"gitlab.unjx.de/flohoss/godash/internal/env"
2024-03-27 22:43:36 +01:00
"github.com/zitadel/oidc/v3/pkg/oidc"
"github.com/zitadel/zitadel-go/v3/pkg/authentication"
openid "github.com/zitadel/zitadel-go/v3/pkg/authentication/oidc"
"github.com/zitadel/zitadel-go/v3/pkg/zitadel"
2024-03-18 21:54:35 +01:00
)
func NewAuthHandler(env *env.Config) *AuthHandler {
2024-03-27 22:43:36 +01:00
a := AuthHandler{
2024-03-18 21:54:35 +01:00
env: env,
}
2024-03-27 22:43:36 +01:00
if env.SSODomain != "" {
ctx := context.Background()
authN, err := authentication.New(ctx, zitadel.New(env.SSODomain), env.SSOKey,
openid.DefaultAuthentication(env.SSOClientId, env.PublicUrl+"/auth/callback", env.SSOKey),
2024-03-18 21:54:35 +01:00
)
2024-03-27 22:43:36 +01:00
if err != nil {
slog.Error("zitadel sdk could not initialize", "error", err)
os.Exit(1)
2024-03-18 21:54:35 +01:00
}
2024-03-27 22:43:36 +01:00
a.authN = authN
a.middleware = authentication.Middleware(authN)
2024-03-18 21:54:35 +01:00
}
2024-03-27 22:43:36 +01:00
return &a
2024-03-18 21:54:35 +01:00
}
2024-03-27 22:43:36 +01:00
type AuthHandler struct {
env *env.Config
authN *authentication.Authenticator[*openid.UserInfoContext[*oidc.IDTokenClaims, *oidc.UserInfo]]
middleware *authentication.Interceptor[*openid.UserInfoContext[*oidc.IDTokenClaims, *oidc.UserInfo]]
2024-03-18 21:54:35 +01:00
}