From 8d08382aea2c1402312efac769383972db81c26c Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Tue, 15 Oct 2019 21:56:47 +0000 Subject: [PATCH] feat: skip username validation for SSO users (#2627) --- .../app/handlers/api/auth/local/signup.ts | 1 + src/core/server/app/handlers/api/install.ts | 1 + .../middleware/passport/strategies/facebook.ts | 1 + .../middleware/passport/strategies/google.ts | 1 + .../passport/strategies/oidc/index.ts | 1 + .../passport/strategies/verifiers/sso.ts | 1 + src/core/server/services/users/users.ts | 18 ++++++++++++++---- 7 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/core/server/app/handlers/api/auth/local/signup.ts b/src/core/server/app/handlers/api/auth/local/signup.ts index 82877906b..f43d3d66c 100644 --- a/src/core/server/app/handlers/api/auth/local/signup.ts +++ b/src/core/server/app/handlers/api/auth/local/signup.ts @@ -93,6 +93,7 @@ export const signupHandler = ({ // start with. role: GQLUSER_ROLE.COMMENTER, }, + {}, now ); diff --git a/src/core/server/app/handlers/api/install.ts b/src/core/server/app/handlers/api/install.ts index b38c10376..ac6a87b5f 100644 --- a/src/core/server/app/handlers/api/install.ts +++ b/src/core/server/app/handlers/api/install.ts @@ -133,6 +133,7 @@ export const installHandler = ({ profile, role: GQLUSER_ROLE.ADMIN, }, + {}, req.coral.now ); diff --git a/src/core/server/app/middleware/passport/strategies/facebook.ts b/src/core/server/app/middleware/passport/strategies/facebook.ts index 60212e098..b2d975662 100644 --- a/src/core/server/app/middleware/passport/strategies/facebook.ts +++ b/src/core/server/app/middleware/passport/strategies/facebook.ts @@ -82,6 +82,7 @@ export default class FacebookStrategy extends OAuth2Strategy< avatar, profile, }, + {}, now ); } diff --git a/src/core/server/app/middleware/passport/strategies/google.ts b/src/core/server/app/middleware/passport/strategies/google.ts index 7bd0a829e..fe41df1a4 100644 --- a/src/core/server/app/middleware/passport/strategies/google.ts +++ b/src/core/server/app/middleware/passport/strategies/google.ts @@ -81,6 +81,7 @@ export default class GoogleStrategy extends OAuth2Strategy< avatar, profile, }, + {}, now ); } diff --git a/src/core/server/app/middleware/passport/strategies/oidc/index.ts b/src/core/server/app/middleware/passport/strategies/oidc/index.ts index bc1efa665..99031bec5 100644 --- a/src/core/server/app/middleware/passport/strategies/oidc/index.ts +++ b/src/core/server/app/middleware/passport/strategies/oidc/index.ts @@ -193,6 +193,7 @@ export async function findOrCreateOIDCUser( avatar: picture, profile, }, + {}, now ); } diff --git a/src/core/server/app/middleware/passport/strategies/verifiers/sso.ts b/src/core/server/app/middleware/passport/strategies/verifiers/sso.ts index a06dc94b0..cdd02db23 100644 --- a/src/core/server/app/middleware/passport/strategies/verifiers/sso.ts +++ b/src/core/server/app/middleware/passport/strategies/verifiers/sso.ts @@ -139,6 +139,7 @@ export async function findOrCreateSSOUser( emailVerified: true, profile, }, + { skipUsernameValidation: true }, now ); } else if (iat && needsSSOUpdate(decodedToken.user, user)) { diff --git a/src/core/server/services/users/users.ts b/src/core/server/services/users/users.ts index 556af36c2..42f968902 100644 --- a/src/core/server/services/users/users.ts +++ b/src/core/server/services/users/users.ts @@ -86,8 +86,11 @@ import { } from "./download/token"; import { validateEmail, validatePassword, validateUsername } from "./helpers"; -function validateFindOrCreateUserInput(input: FindOrCreateUser) { - if (input.username) { +function validateFindOrCreateUserInput( + input: FindOrCreateUser, + options: FindOrCreateUserOptions +) { + if (input.username && !options.skipUsernameValidation) { validateUsername(input.username); } @@ -108,14 +111,19 @@ function validateFindOrCreateUserInput(input: FindOrCreateUser) { export type FindOrCreateUser = FindOrCreateUserInput; +export interface FindOrCreateUserOptions { + skipUsernameValidation?: boolean; +} + export async function findOrCreate( mongo: Db, tenant: Tenant, input: FindOrCreateUser, + options: FindOrCreateUserOptions, now: Date ) { // Validate the input. - validateFindOrCreateUserInput(input); + validateFindOrCreateUserInput(input, options); const user = await findOrCreateUser(mongo, tenant.id, input, now); @@ -125,15 +133,17 @@ export async function findOrCreate( } export type CreateUser = FindOrCreateUserInput; +export type CreateUserOptions = FindOrCreateUserOptions; export async function create( mongo: Db, tenant: Tenant, input: CreateUser, + options: CreateUserOptions, now: Date ) { // Validate the input. - validateFindOrCreateUserInput(input); + validateFindOrCreateUserInput(input, options); if (input.id) { // Try to check to see if there is a user with the same ID before we try to