Merge pull request #7162 from morgan55555/dev

LDAP auth fixes: do not store LDAP password, use default user role on first auth, allow ldap only form.
This commit is contained in:
Timothy Jaeryang Baek 2024-11-21 08:08:47 -08:00 committed by GitHub
commit 1fcde2272b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 8 deletions

View File

@ -238,10 +238,20 @@ async def ldap_auth(request: Request, response: Response, form_data: LdapForm):
user = Users.get_user_by_email(mail) user = Users.get_user_by_email(mail)
if not user: if not user:
try: try:
hashed = get_password_hash(form_data.password) role = (
user = Auths.insert_new_auth(mail, hashed, cn) "admin"
if Users.get_num_users() == 0
else request.app.state.config.DEFAULT_USER_ROLE
)
user = Auths.insert_new_auth(
mail,
str(uuid.uuid4()),
cn,
None,
role,
)
if not user: if not user:
raise HTTPException( raise HTTPException(
@ -253,7 +263,7 @@ async def ldap_auth(request: Request, response: Response, form_data: LdapForm):
except Exception as err: except Exception as err:
raise HTTPException(500, detail=ERROR_MESSAGES.DEFAULT(err)) raise HTTPException(500, detail=ERROR_MESSAGES.DEFAULT(err))
user = Auths.authenticate_user(mail, password=str(form_data.password)) user = Auths.authenticate_user_by_trusted_header(mail)
if user: if user:
token = create_token( token = create_token(

View File

@ -203,7 +203,7 @@
{/if} {/if}
</div> </div>
{#if $config?.features.enable_login_form} {#if $config?.features.enable_login_form || $config?.features.enable_ldap}
<div class="flex flex-col mt-4"> <div class="flex flex-col mt-4">
{#if mode === 'signup'} {#if mode === 'signup'}
<div class="mb-2"> <div class="mb-2">
@ -227,6 +227,7 @@
type="text" type="text"
class="my-0.5 w-full text-sm outline-none bg-transparent" class="my-0.5 w-full text-sm outline-none bg-transparent"
autocomplete="username" autocomplete="username"
name="username"
placeholder={$i18n.t('Enter Your Username')} placeholder={$i18n.t('Enter Your Username')}
required required
/> />
@ -239,6 +240,7 @@
type="email" type="email"
class="my-0.5 w-full text-sm outline-none bg-transparent" class="my-0.5 w-full text-sm outline-none bg-transparent"
autocomplete="email" autocomplete="email"
name="email"
placeholder={$i18n.t('Enter Your Email')} placeholder={$i18n.t('Enter Your Email')}
required required
/> />
@ -254,13 +256,14 @@
class="my-0.5 w-full text-sm outline-none bg-transparent" class="my-0.5 w-full text-sm outline-none bg-transparent"
placeholder={$i18n.t('Enter Your Password')} placeholder={$i18n.t('Enter Your Password')}
autocomplete="current-password" autocomplete="current-password"
name="current-password"
required required
/> />
</div> </div>
</div> </div>
{/if} {/if}
<div class="mt-5"> <div class="mt-5">
{#if $config?.features.enable_login_form} {#if $config?.features.enable_login_form || $config?.features.enable_ldap}
{#if mode === 'ldap'} {#if mode === 'ldap'}
<button <button
class="bg-gray-700/5 hover:bg-gray-700/10 dark:bg-gray-100/5 dark:hover:bg-gray-100/10 dark:text-gray-300 dark:hover:text-white transition w-full rounded-full font-medium text-sm py-2.5" class="bg-gray-700/5 hover:bg-gray-700/10 dark:bg-gray-100/5 dark:hover:bg-gray-100/10 dark:text-gray-300 dark:hover:text-white transition w-full rounded-full font-medium text-sm py-2.5"
@ -309,7 +312,7 @@
{#if Object.keys($config?.oauth?.providers ?? {}).length > 0} {#if Object.keys($config?.oauth?.providers ?? {}).length > 0}
<div class="inline-flex items-center justify-center w-full"> <div class="inline-flex items-center justify-center w-full">
<hr class="w-32 h-px my-4 border-0 dark:bg-gray-100/10 bg-gray-700/10" /> <hr class="w-32 h-px my-4 border-0 dark:bg-gray-100/10 bg-gray-700/10" />
{#if $config?.features.enable_login_form} {#if $config?.features.enable_login_form || $config?.features.enable_ldap}
<span <span
class="px-3 text-sm font-medium text-gray-900 dark:text-white bg-transparent" class="px-3 text-sm font-medium text-gray-900 dark:text-white bg-transparent"
>{$i18n.t('or')}</span >{$i18n.t('or')}</span
@ -401,7 +404,7 @@
</div> </div>
{/if} {/if}
{#if $config?.features.enable_ldap} {#if $config?.features.enable_ldap && $config?.features.enable_login_form}
<div class="mt-2"> <div class="mt-2">
<button <button
class="flex justify-center items-center text-xs w-full text-center underline" class="flex justify-center items-center text-xs w-full text-center underline"