Skip to content

Commit

Permalink
Add tentative basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffvli committed Feb 5, 2023
1 parent d2e553a commit 99532c8
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/renderer/api/jellyfin.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,16 @@ const authenticate = async (
password: string;
username: string;
},
basicAuth?: { password: string; username: string },
): Promise<AuthenticationResponse> => {
const cleanServerUrl = url.replace(/\/$/, '');

const data = await ky
.post(`${cleanServerUrl}/users/authenticatebyname`, {
headers: {
Authorization: basicAuth
? `Basic ${window.btoa(`${basicAuth?.username}:${basicAuth?.password}`)}`
: undefined,
'X-Emby-Authorization': `MediaBrowser Client="Feishin", Device="PC", DeviceId="Feishin", Version="${packageJson.version}"`,
},
json: {
Expand Down
8 changes: 8 additions & 0 deletions src/renderer/api/navidrome.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,19 @@ const api = ky.create({
const authenticate = async (
url: string,
body: { password: string; username: string },
basicAuth?: { password: string; username: string },
): Promise<AuthenticationResponse> => {
const cleanServerUrl = url.replace(/\/$/, '');

const headers = new Headers({
Authorization: `Basic ${window.btoa(`${basicAuth?.username}:${basicAuth?.password}`)}`,
'Content-Type': 'application/json',
});

const data = await ky
.post(`${cleanServerUrl}/auth/login`, {
credentials: 'include',
headers: basicAuth ? headers : undefined,
json: {
password: body.password,
username: body.username,
Expand Down
10 changes: 9 additions & 1 deletion src/renderer/api/subsonic.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,16 @@ const authenticate = async (
password: string;
username: string;
},
basicAuth?: { password: string; username: string },
): Promise<AuthenticationResponse> => {
let credential;
const cleanServerUrl = url.replace(/\/$/, '');

const headers = new Headers({
Authorization: `Basic ${window.btoa(`${basicAuth?.username}:${basicAuth?.password}`)}`,
'Content-Type': 'application/json',
});

if (body.legacy) {
credential = `u=${body.username}&p=${body.password}`;
} else {
Expand All @@ -139,7 +145,9 @@ const authenticate = async (
credential = `u=${body.username}&s=${salt}&t=${hash}`;
}

await ky.get(`${cleanServerUrl}/rest/ping.view?v=1.13.0&c=Feishin&f=json&${credential}`);
await ky.get(`${cleanServerUrl}/rest/ping.view?v=1.13.0&c=Feishin&f=json&${credential}`, {
headers,
});

return {
credential,
Expand Down
55 changes: 47 additions & 8 deletions src/renderer/features/servers/components/add-server-form.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { useState } from 'react';
import { Stack, Group, Checkbox } from '@mantine/core';
import { Button, PasswordInput, SegmentedControl, TextInput, toast } from '/@/renderer/components';
import {
Button,
Paper,
PasswordInput,
SegmentedControl,
Switch,
TextInput,
toast,
} from '/@/renderer/components';
import { useForm } from '@mantine/form';
import { useFocusTrap } from '@mantine/hooks';
import { closeAllModals } from '@mantine/modals';
Expand All @@ -10,7 +18,7 @@ import { navidromeApi } from '/@/renderer/api/navidrome.api';
import { subsonicApi } from '/@/renderer/api/subsonic.api';
import { AuthenticationResponse } from '/@/renderer/api/types';
import { useAuthStore, useAuthStoreActions } from '/@/renderer/store';
import { ServerType } from '/@/renderer/types';
import { ServerListItem, ServerType } from '/@/renderer/types';

const SERVER_TYPES = [
{ label: 'Jellyfin', value: ServerType.JELLYFIN },
Expand All @@ -36,6 +44,9 @@ export const AddServerForm = ({ onCancel }: AddServerFormProps) => {

const form = useForm({
initialValues: {
basicAuth: false,
basicPassword: '',
basicUsername: '',
legacyAuth: false,
name: '',
password: '',
Expand All @@ -57,13 +68,20 @@ export const AddServerForm = ({ onCancel }: AddServerFormProps) => {

try {
setIsLoading(true);
const data: AuthenticationResponse = await authFunction(values.url, {
legacy: values.legacyAuth,
password: values.password,
username: values.username,
});
const data: AuthenticationResponse = await authFunction(
values.url,
{
legacy: values.legacyAuth,
password: values.password,
username: values.username,
},
{ password: values.basicPassword, username: values.basicUsername },
);

const serverItem = {
const serverItem: ServerListItem = {
basicAuth: values.basicAuth,
basicPassword: values.basicPassword,
basicUsername: values.basicUsername,
credential: data.credential,
id: nanoid(),
name: values.name,
Expand Down Expand Up @@ -97,6 +115,7 @@ export const AddServerForm = ({ onCancel }: AddServerFormProps) => {
<Stack
ref={focusTrapRef}
m={5}
spacing="md"
>
<SegmentedControl
data={SERVER_TYPES}
Expand Down Expand Up @@ -127,6 +146,26 @@ export const AddServerForm = ({ onCancel }: AddServerFormProps) => {
{...form.getInputProps('legacyAuth', { type: 'checkbox' })}
/>
)}
<Paper p="1rem">
<Stack>
<Switch
label="Use basic authentication"
{...form.getInputProps('basicAuth', { type: 'checkbox' })}
/>
{form.values.basicAuth && (
<>
<TextInput
label="Username"
{...form.getInputProps('basicUsername')}
/>
<PasswordInput
label="Password"
{...form.getInputProps('basicPassword')}
/>
</>
)}
</Stack>
</Paper>
<Group position="right">
<Button
variant="subtle"
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export enum ServerType {
}

export type ServerListItem = {
basicAuth?: boolean;
basicPassword?: string;
basicUsername?: string;
credential: string;
id: string;
name: string;
Expand Down

0 comments on commit 99532c8

Please sign in to comment.