Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

review adjustments

Kevin Papstec5ad962819f
diff --git a/app/actions/signin.go b/app/actions/signin.go
index c5f4bf61f..bc4056842 100644
--- a/app/actions/signin.go
+++ b/app/actions/signin.go
@@ -133,8 +133,8 @@ func NewSignInByEmailWithName() *SignInByEmailWithName {
// IsAuthorized returns true if current user is authorized to perform this action
func (action *SignInByEmailWithName) IsAuthorized(ctx context.Context, user *entity.User) bool {
tenant := ctx.Value(app.TenantCtxKey).(*entity.Tenant)
- // Reject before any user lookup, so the response doesn't reveal which emails are registered
- return tenant.IsSignUpAllowed()
+ // Reject new-user requests before the handler looks up the user
+ return tenant.CanCreateAccountByEmail(action.GetKind())
}
// Validate if current model is valid
diff --git a/app/handlers/oauth.go b/app/handlers/oauth.go
index b23c0d077..2bc046246 100644
--- a/app/handlers/oauth.go
+++ b/app/handlers/oauth.go
@@ -274,8 +274,7 @@ func OAuthToken() web.HandlerFunc {
}
if err != nil {
if errors.Cause(err) == app.ErrNotFound {
- isTrusted := customConfig != nil && customConfig.IsTrusted
- if !canCreateAccount(c, isTrusted) {
+ if !c.Tenant().CanCreateAccountByOAuth(customConfig) {
return c.Redirect("/not-invited")
}
diff --git a/app/handlers/signin.go b/app/handlers/signin.go
index e633483c7..a1b3de671 100644
--- a/app/handlers/signin.go
+++ b/app/handlers/signin.go
@@ -70,13 +70,6 @@ func AccessDeniedPage() web.HandlerFunc {
}
}
-// canCreateAccount is the single rule for every self-service account creation.
-// Invited users and users from trusted OAuth providers can always create an account,
-// everyone else only if the site allows sign-ups (see entity.Tenant.IsSignUpAllowed).
-func canCreateAccount(c *web.Context, isInvitedOrTrusted bool) bool {
- return isInvitedOrTrusted || c.Tenant().IsSignUpAllowed()
-}
-
// SignInByEmail checks if user exists and sends code only for existing users
func SignInByEmail() web.HandlerFunc {
return func(c *web.Context) error {
@@ -112,7 +105,8 @@ func SignInByEmail() web.HandlerFunc {
}
return c.Ok(web.Map{
- "userExists": userExists,
+ "userExists": userExists,
+ "signUpAllowed": c.Tenant().CanCreateAccountByEmail(action.GetKind()),
})
}
}
@@ -217,7 +211,7 @@ func VerifySignInCode() web.HandlerFunc {
if err != nil {
if errors.Cause(err) == app.ErrNotFound {
// User doesn't exist
- if !canCreateAccount(c, false) {
+ if !c.Tenant().CanCreateAccountByEmail(result.Kind) {
return c.Forbidden()
}
@@ -281,7 +275,7 @@ func ResendSignInCode() web.HandlerFunc {
}
// Unknown email addresses only get a code if they are allowed to create an account
- if !canCreateAccount(c, false) {
+ if !c.Tenant().CanCreateAccountByEmail(action.GetKind()) {
if err := bus.Dispatch(c, &query.GetUserByEmail{Email: action.Email}); err != nil {
if errors.Cause(err) == app.ErrNotFound {
return c.Ok(web.Map{})
@@ -327,7 +321,7 @@ func VerifySignInKey(kind enum.EmailVerificationKind) web.HandlerFunc {
err = bus.Dispatch(c, userByEmail)
if err != nil {
if errors.Cause(err) == app.ErrNotFound {
- if !canCreateAccount(c, kind == enum.EmailVerificationKindUserInvitation) {
+ if !c.Tenant().CanCreateAccountByEmail(kind) {
return NotInvitedPage()(c)
}
@@ -399,7 +393,7 @@ func CompleteSignInProfile() web.HandlerFunc {
return c.BadRequest(web.Map{})
}
- if !canCreateAccount(c, action.Kind == enum.EmailVerificationKindUserInvitation) {
+ if !c.Tenant().CanCreateAccountByEmail(action.Kind) {
return c.Forbidden()
}
diff --git a/app/handlers/signin_test.go b/app/handlers/signin_test.go
index 5b1a961ce..18fe22c9b 100644
--- a/app/handlers/signin_test.go
+++ b/app/handlers/signin_test.go
@@ -82,6 +82,27 @@ func TestSignInByEmailHandler_NewUser(t *testing.T) {
Expect(code).Equals(http.StatusOK)
Expect(response.Body.String()).ContainsSubstring(`"userExists":false`)
+ Expect(response.Body.String()).ContainsSubstring(`"signUpAllowed":true`)
+}
+
+func TestSignInByEmailHandler_NewUser_SignUpDisabled(t *testing.T) {
+ RegisterT(t)
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetUserByEmail) error {
+ return app.ErrNotFound
+ })
+
+ server := mock.NewServer()
+ mock.DemoTenant.IsSignUpDisabled = true
+
+ code, response := server.
+ OnTenant(mock.DemoTenant).
+ ExecutePost(handlers.SignInByEmail(), `{ "email": "[email protected]" }`)
+
+ Expect(code).Equals(http.StatusOK)
+ Expect(response.Body.String()).ContainsSubstring(`"userExists":false`)
+ Expect(response.Body.String()).ContainsSubstring(`"signUpAllowed":false`)
+ ExpectHandler(&cmd.SaveVerificationKey{}).CalledTimes(0)
}
func TestSignInByEmailWithNameHandler_NewUser(t *testing.T) {
@@ -1398,3 +1419,56 @@ func TestCompleteSignInProfileHandler_OtherVerificationKinds(t *testing.T) {
ExpectHandler(&cmd.RegisterUser{}).CalledTimes(0)
}
}
+
+func TestSignInByEmailWithNameHandler_EmailAuthNotAllowed(t *testing.T) {
+ RegisterT(t)
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetUserByEmail) error {
+ return app.ErrNotFound
+ })
+
+ server := mock.NewServer()
+ mock.DemoTenant.IsEmailAuthAllowed = false
+
+ code, _ := server.
+ OnTenant(mock.DemoTenant).
+ ExecutePost(handlers.SignInByEmailWithName(), `{ "email": "[email protected]", "name": "New User" }`)
+
+ Expect(code).Equals(http.StatusForbidden)
+ ExpectHandler(&cmd.SaveVerificationKey{}).CalledTimes(0)
+}
+
+func TestVerifySignInCodeHandler_CorrectCode_NewUser_EmailAuthNotAllowed(t *testing.T) {
+ RegisterT(t)
+
+ server := mock.NewServer()
+ mock.DemoTenant.IsEmailAuthAllowed = false
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetActiveVerificationByEmail) error {
+ q.Result = &entity.EmailVerification{
+ Email: "[email protected]",
+ Name: "New User",
+ Kind: enum.EmailVerificationKindSignIn,
+ Key: "some-long-link-key",
+ Code: "123456",
+ CreatedAt: time.Now(),
+ ExpiresAt: time.Now().Add(15 * time.Minute),
+ }
+ return nil
+ })
+
+ bus.AddHandler(func(ctx context.Context, c *cmd.IncrementVerificationAttempts) error {
+ return nil
+ })
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetUserByEmail) error {
+ return app.ErrNotFound
+ })
+
+ code, _ := server.
+ OnTenant(mock.DemoTenant).
+ ExecutePost(handlers.VerifySignInCode(), `{ "email": "[email protected]", "code": "123456" }`)
+
+ Expect(code).Equals(http.StatusForbidden)
+ ExpectHandler(&cmd.RegisterUser{}).CalledTimes(0)
+}
diff --git a/app/models/entity/tenant.go b/app/models/entity/tenant.go
index 5b61e217f..20aee4143 100644
--- a/app/models/entity/tenant.go
+++ b/app/models/entity/tenant.go
@@ -42,10 +42,29 @@ func (t *Tenant) IsDisabled() bool {
return t.Status == enum.TenantDisabled
}
-// IsSignUpAllowed returns true if visitors can create a new account on their own,
-// e.g. by signing in with an unknown email address or an untrusted OAuth provider.
-// Invitations by administrators are always allowed.
-func (t *Tenant) IsSignUpAllowed() bool {
+// CanCreateAccountByEmail returns true if completing an email verification of the given kind
+// may create a new account. Invited users can always create their account, everyone else
+// only if the site allows sign-ups and email login is turned on.
+func (t *Tenant) CanCreateAccountByEmail(kind enum.EmailVerificationKind) bool {
+ if kind == enum.EmailVerificationKindUserInvitation {
+ return true
+ }
+ return t.IsEmailAuthAllowed && t.isSignUpAllowed()
+}
+
+// CanCreateAccountByOAuth returns true if signing in with the given OAuth provider may create
+// a new account. Users from trusted providers can always create their account, everyone else
+// only if the site allows sign-ups. Built-in providers have no config and are never trusted.
+func (t *Tenant) CanCreateAccountByOAuth(config *OAuthConfig) bool {
+ if config != nil && config.IsTrusted {
+ return true
+ }
+ return t.isSignUpAllowed()
+}
+
+// isSignUpAllowed returns true if visitors can create a new account on their own,
+// which is neither the case on private sites nor when sign-ups are disabled.
+func (t *Tenant) isSignUpAllowed() bool {
return !t.IsPrivate && !t.IsSignUpDisabled
}
diff --git a/public/components/common/SignInControl.tsx b/public/components/common/SignInControl.tsx
index 519990a02..dd0b04231 100644
--- a/public/components/common/SignInControl.tsx
+++ b/public/components/common/SignInControl.tsx
@@ -61,11 +61,10 @@ export const SignInControl: React.FunctionComponent<SignInControlProps> = (props
const result = await actions.signIn(email)
if (result.ok) {
setError(undefined)
- const data = result.data as { userExists?: boolean } | undefined
+ const data = result.data
if (data && data.userExists === false) {
- const tenant = fider.session.tenant
- if (tenant && (tenant.isPrivate || tenant.isSignUpDisabled)) {
- // New users are not allowed to create an account on their own
+ if (!data.signUpAllowed) {
+ // The server decided this email address can't be used to create a new account
setError({
errors: [
{
diff --git a/public/services/actions/tenant.ts b/public/services/actions/tenant.ts
index 3fe76a758..215c1553e 100644
--- a/public/services/actions/tenant.ts
+++ b/public/services/actions/tenant.ts
@@ -56,8 +56,13 @@ export const checkAvailability = async (subdomain: string): Promise<Result<Check
return await http.get<CheckAvailabilityResponse>(`/_api/tenants/${subdomain}/availability`)
}
-export const signIn = async (email: string): Promise<Result> => {
- return await http.post("/_api/signin", { email })
+export interface SignInResponse {
+ userExists: boolean
+ signUpAllowed: boolean
+}
+
+export const signIn = async (email: string): Promise<Result<SignInResponse>> => {
+ return await http.post<SignInResponse>("/_api/signin", { email })
}
export const signInNewUser = async (email: string, name: string): Promise<Result> => {