42 lines
1.5 KiB
Go
42 lines
1.5 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
)
|
|
|
|
func (s *Store) AuthenticationSettings(ctx context.Context) (identity.AuthenticationSettings, error) {
|
|
var value identity.AuthenticationSettings
|
|
err := s.pool.QueryRow(ctx, `SELECT public_url,oidc_issuer,oidc_audience,oidc_client_id FROM authentication_settings WHERE singleton`).Scan(&value.PublicURL, &value.OIDCIssuer, &value.OIDCAudience, &value.OIDCClientID)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return value, nil
|
|
}
|
|
return value, err
|
|
}
|
|
|
|
func (s *Store) SaveAuthenticationSettings(ctx context.Context, value identity.AuthenticationSettings) error {
|
|
p, ok := access.PrincipalFromContext(ctx)
|
|
if !ok || !p.InstanceAdmin() {
|
|
return identity.ErrCredentialRejected
|
|
}
|
|
value.PublicURL = strings.TrimRight(value.PublicURL, "/")
|
|
if err := value.Validate(); err != nil {
|
|
return err
|
|
}
|
|
// The relying-party origin cannot silently change and orphan enrolled keys.
|
|
command, err := s.pool.Exec(ctx, `UPDATE authentication_settings SET public_url=$1,oidc_issuer=$2,oidc_audience=$3,oidc_client_id=$4 WHERE singleton AND (public_url=$1 OR NOT EXISTS (SELECT 1 FROM passkeys))`, value.PublicURL, value.OIDCIssuer, value.OIDCAudience, value.OIDCClientID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if command.RowsAffected() != 1 {
|
|
return identity.ErrInvalid
|
|
}
|
|
return nil
|
|
}
|