@@ -20,6 +20,7 @@ import (
|
||||
|
||||
"github.com/crewjam/saml"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/mail"
|
||||
)
|
||||
|
||||
func ExtractAttributeValue(assertion *saml.Assertion, attributeName string) (string, error) {
|
||||
@@ -98,25 +99,32 @@ func isValidRole(role string) bool {
|
||||
func ExtractUserAttributes(
|
||||
assertion *saml.Assertion,
|
||||
attributeEmail, attributeFirstname, attributeLastname, attributeRole string,
|
||||
) (email, fullname, role string, err error) {
|
||||
) (email mail.Addr, fullname, role string, err error) {
|
||||
if len(assertion.AttributeStatements) == 0 {
|
||||
if assertion.Subject != nil && assertion.Subject.NameID != nil {
|
||||
email = assertion.Subject.NameID.Value
|
||||
fullname = email
|
||||
email, err = mail.ParseAddr(assertion.Subject.NameID.Value)
|
||||
if err != nil {
|
||||
return mail.Nil, "", "", fmt.Errorf("invalid nameID as email address")
|
||||
}
|
||||
fullname = email.String()
|
||||
role = ""
|
||||
return email, fullname, role, nil
|
||||
}
|
||||
return "", "", "", fmt.Errorf("no attribute statement and no NameID in assertion")
|
||||
return mail.Nil, "", "", fmt.Errorf("no attribute statement and no NameID in assertion")
|
||||
}
|
||||
|
||||
email, err = ExtractAttributeValue(assertion, attributeEmail)
|
||||
emailString, err := ExtractAttributeValue(assertion, attributeEmail)
|
||||
if err != nil {
|
||||
if assertion.Subject != nil && assertion.Subject.NameID != nil {
|
||||
email = assertion.Subject.NameID.Value
|
||||
emailString = assertion.Subject.NameID.Value
|
||||
} else {
|
||||
return "", "", "", fmt.Errorf("cannot extract email: %w", err)
|
||||
return mail.Nil, "", "", fmt.Errorf("cannot extract email: %w", err)
|
||||
}
|
||||
}
|
||||
email, err = mail.ParseAddr(emailString)
|
||||
if err != nil {
|
||||
return mail.Nil, "", "", fmt.Errorf("invalid attribute email")
|
||||
}
|
||||
|
||||
firstname, err := ExtractAttributeValue(assertion, attributeFirstname)
|
||||
if err != nil {
|
||||
@@ -135,7 +143,7 @@ func ExtractUserAttributes(
|
||||
} else if lastname != "" {
|
||||
fullname = lastname
|
||||
} else {
|
||||
fullname = email
|
||||
fullname = email.String()
|
||||
}
|
||||
|
||||
role, err = ExtractAttributeValue(assertion, attributeRole)
|
||||
|
||||
@@ -32,6 +32,7 @@ import (
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/crypto/cipher"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/mail"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -372,7 +373,7 @@ func (s *SAMLService) InitiateSAMLLogin(
|
||||
}
|
||||
|
||||
type SAMLUserInfo struct {
|
||||
Email string
|
||||
Email mail.Addr
|
||||
FullName string
|
||||
Role *coredata.MembershipRole
|
||||
SAMLSubject string
|
||||
@@ -516,13 +517,8 @@ func (s *SAMLService) HandleSAMLAssertion(
|
||||
return nil, ErrCannotExtractUserAttributes{Err: err}
|
||||
}
|
||||
|
||||
actualEmailDomain, err := ExtractEmailDomain(email)
|
||||
if err != nil {
|
||||
return nil, ErrCannotExtractUserAttributes{Err: fmt.Errorf("cannot extract domain from email: %w", err)}
|
||||
}
|
||||
|
||||
if actualEmailDomain != config.EmailDomain {
|
||||
return nil, fmt.Errorf("email domain mismatch: assertion contains email with domain %s but SAML config is for domain %s", actualEmailDomain, config.EmailDomain)
|
||||
if email.Domain() != config.EmailDomain {
|
||||
return nil, fmt.Errorf("email domain mismatch: assertion contains email with domain %s but SAML config is for domain %s", email.Domain(), config.EmailDomain)
|
||||
}
|
||||
|
||||
systemRole := MapSAMLRoleToSystemRole(samlRole)
|
||||
|
||||
@@ -21,7 +21,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/mail"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
@@ -32,6 +31,7 @@ import (
|
||||
"go.probo.inc/probo/pkg/crypto/cipher"
|
||||
"go.probo.inc/probo/pkg/crypto/passwdhash"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/mail"
|
||||
"go.probo.inc/probo/pkg/statelesstoken"
|
||||
)
|
||||
|
||||
@@ -102,8 +102,8 @@ type (
|
||||
ErrSAMLAutoSignupDisabled struct{}
|
||||
|
||||
EmailConfirmationData struct {
|
||||
UserID gid.GID `json:"uid"`
|
||||
Email string `json:"email"`
|
||||
UserID gid.GID `json:"uid"`
|
||||
Email mail.Addr `json:"email"`
|
||||
}
|
||||
|
||||
InvitationData struct {
|
||||
@@ -113,7 +113,7 @@ type (
|
||||
CreatePeople bool `json:"create_people"`
|
||||
}
|
||||
PasswordResetData struct {
|
||||
Email string `json:"email"`
|
||||
Email mail.Addr `json:"email"`
|
||||
}
|
||||
|
||||
ErrSAMLAuthRequired struct {
|
||||
@@ -262,7 +262,7 @@ func (s Service) CanCreateOrganization(ctx context.Context, userID gid.GID) erro
|
||||
|
||||
func (s Service) ForgetPassword(
|
||||
ctx context.Context,
|
||||
email string,
|
||||
email mail.Addr,
|
||||
) error {
|
||||
// Always generate a new token to avoid timing attacks and leaking information
|
||||
// about existing emails
|
||||
@@ -329,7 +329,7 @@ func (s Service) ForgetPassword(
|
||||
|
||||
func (s Service) SignUp(
|
||||
ctx context.Context,
|
||||
emailAddress string,
|
||||
emailAddress mail.Addr,
|
||||
password string,
|
||||
fullName string,
|
||||
) (*coredata.User, *coredata.Session, error) {
|
||||
@@ -337,11 +337,6 @@ func (s Service) SignUp(
|
||||
return nil, nil, &ErrSignupDisabled{}
|
||||
}
|
||||
|
||||
emailAddress2, err := mail.ParseAddress(emailAddress)
|
||||
if err != nil {
|
||||
return nil, nil, &ErrInvalidEmail{emailAddress}
|
||||
}
|
||||
|
||||
if len(password) < 8 || len(password) > 128 {
|
||||
return nil, nil, &ErrInvalidPassword{minLength: 8, maxLength: 128}
|
||||
}
|
||||
@@ -358,7 +353,7 @@ func (s Service) SignUp(
|
||||
now := time.Now()
|
||||
user := &coredata.User{
|
||||
ID: gid.New(gid.NilTenant, coredata.UserEntityType),
|
||||
EmailAddress: emailAddress2.Address,
|
||||
EmailAddress: emailAddress,
|
||||
HashedPassword: hashedPassword,
|
||||
EmailAddressVerified: false,
|
||||
FullName: fullName,
|
||||
@@ -453,16 +448,12 @@ func (s Service) ProvisionSAMLUser(
|
||||
ctx context.Context,
|
||||
samlConfigID gid.GID,
|
||||
organizationID gid.GID,
|
||||
emailAddress string,
|
||||
emailAddress mail.Addr,
|
||||
fullName string,
|
||||
samlSubject string,
|
||||
existingSession *coredata.Session,
|
||||
sessionDuration time.Duration,
|
||||
) (*coredata.Session, *coredata.User, error) {
|
||||
if _, err := mail.ParseAddress(emailAddress); err != nil {
|
||||
return nil, nil, &ErrInvalidEmail{emailAddress}
|
||||
}
|
||||
|
||||
if fullName == "" {
|
||||
return nil, nil, &ErrInvalidFullName{fullName}
|
||||
}
|
||||
@@ -566,14 +557,10 @@ func (s Service) ProvisionSAMLUser(
|
||||
|
||||
func (s Service) SignIn(
|
||||
ctx context.Context,
|
||||
emailAddress string,
|
||||
emailAddress mail.Addr,
|
||||
password string,
|
||||
existingSession *coredata.Session,
|
||||
) (*coredata.Session, *coredata.User, error) {
|
||||
if _, err := mail.ParseAddress(emailAddress); err != nil {
|
||||
return nil, nil, &ErrInvalidCredentials{"invalid email or password"}
|
||||
}
|
||||
|
||||
user := &coredata.User{}
|
||||
session := &coredata.Session{}
|
||||
|
||||
@@ -868,10 +855,6 @@ func (s Service) SignupFromInvitation(
|
||||
return nil, nil, &ErrInvalidPassword{minLength: 8, maxLength: 128}
|
||||
}
|
||||
|
||||
if _, err := mail.ParseAddress(payload.Data.Email); err != nil {
|
||||
return nil, nil, &ErrInvalidEmail{payload.Data.Email}
|
||||
}
|
||||
|
||||
if fullName == "" {
|
||||
fullName = payload.Data.FullName
|
||||
}
|
||||
@@ -980,7 +963,7 @@ func (s *TenantAuthService) GetUserAuthMethod(
|
||||
}
|
||||
|
||||
// User has SAML subject - check if there's SAML config for this org + user's domain
|
||||
domain := extractDomain(user.EmailAddress)
|
||||
domain := user.EmailAddress.Domain()
|
||||
if domain == "" {
|
||||
authMethod = coredata.UserAuthMethodPassword
|
||||
return nil
|
||||
@@ -1005,20 +988,6 @@ func (s *TenantAuthService) GetUserAuthMethod(
|
||||
return authMethod, err
|
||||
}
|
||||
|
||||
func extractDomain(email string) string {
|
||||
atIndex := -1
|
||||
for i := 0; i < len(email); i++ {
|
||||
if email[i] == '@' {
|
||||
atIndex = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if atIndex == -1 || atIndex == len(email)-1 {
|
||||
return ""
|
||||
}
|
||||
return email[atIndex+1:]
|
||||
}
|
||||
|
||||
// CheckOrganizationAccess checks access to multiple organizations in a single database query
|
||||
// Always uses batch processing to avoid N+1 query problems
|
||||
func (s Service) CheckOrganizationAccess(
|
||||
@@ -1029,7 +998,7 @@ func (s Service) CheckOrganizationAccess(
|
||||
) (map[gid.GID]AccessResult, error) {
|
||||
results := make(map[gid.GID]AccessResult, len(organizationIDs))
|
||||
|
||||
domain := extractDomain(user.EmailAddress)
|
||||
domain := user.EmailAddress.Domain()
|
||||
if domain == "" {
|
||||
// All organizations fail with invalid email
|
||||
for _, orgID := range organizationIDs {
|
||||
|
||||
Reference in New Issue
Block a user