/*
* Copyright (C) 2024 Jonni Liljamo <jonni@liljamo.com>
*
* This file is licensed under GPL-3.0-or-later, see NOTICE and LICENSE for
* more information.
*/
// Package auth provides authentication related types and mechanisms.
package auth
import (
"context"
"fmt"
"git.src.quest/~liljamo/emerwen-web/internal/config"
"github.com/coreos/go-oidc/v3/oidc"
"golang.org/x/oauth2"
)
// Auth holds the OIDC provider and OAuth2 config.
type Auth struct {
Provider *oidc.Provider
Config oauth2.Config
}
// New constructs a new Auth struct.
// Panics if OIDC provider can't be constructed.
func New(c *config.Config) *Auth {
provider, err := oidc.NewProvider(context.Background(), c.OIDCProvider)
if err != nil {
panic(fmt.Sprintf("failed to create OIDC provider: %s", err))
}
config := oauth2.Config{
ClientID: c.OIDCClientID,
ClientSecret: c.OIDCClientSecret,
RedirectURL: c.OIDCRedirectURL,
Endpoint: provider.Endpoint(),
Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
}
return &Auth{
Provider: provider,
Config: config,
}
}