Skip to content

Commit

Permalink
get server info
Browse files Browse the repository at this point in the history
  • Loading branch information
tamland committed Oct 18, 2024
1 parent cdd78d9 commit 9888679
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/app/About.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
</p>
<div>Build: {{ build }}</div>
<div>Build date: {{ buildDate }}</div>

<div class="mt-4">
<div>Server name: {{ auth.serverInfo?.name }}</div>
<div>Server version: {{ auth.serverInfo?.version }}</div>
<div>Server URL: {{ auth.server }}</div>
<div>OpenSubsonic: {{ auth.serverInfo?.openSubsonic ?? false }}</div>
<div v-if="auth.serverInfo?.openSubsonic">
OpenSubsonic extensions: {{ auth.serverInfo?.extensions?.join(", ") }}
</div>
</div>
</div>
<div class="d-flex justify-content-end">
<button class="btn btn-secondary" @click="$emit('close')">
Expand All @@ -23,6 +33,7 @@
<script lang="ts">
import { defineComponent } from 'vue'
import Logo from './Logo.vue'
import { useAuth } from '@/auth/service'
export default defineComponent({
components: {
Expand All @@ -31,6 +42,11 @@
props: {
visible: { type: Boolean, required: true },
},
setup() {
return {
auth: useAuth()
}
},
computed: {
build: () => process.env.VUE_APP_BUILD,
buildDate: () => process.env.VUE_APP_BUILD_DATE,
Expand Down
33 changes: 33 additions & 0 deletions src/auth/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ import { pickBy } from 'lodash-es'

type Auth = { password?: string, salt?: string, hash?: string }

interface ServerInfo {
name: string
version: string
openSubsonic: boolean
extensions: string[]
}

export class AuthService {
public server = ''
public serverInfo = null as null | ServerInfo
public username = ''
private salt = ''
private hash = ''
Expand Down Expand Up @@ -40,6 +48,7 @@ export class AuthService {
const auth = { salt: this.salt, hash: this.hash, password: this.password }
await login(this.server, this.username, auth)
this.authenticated = true
this.serverInfo = await fetchServerInfo(this.server, this.username, auth)
return true
} catch {
return false
Expand All @@ -63,6 +72,7 @@ export class AuthService {
this.server = server
this.username = username
this.authenticated = true
this.serverInfo = await fetchServerInfo(server, username, { hash, salt, password })
this.saveSession()
}

Expand Down Expand Up @@ -105,6 +115,29 @@ async function login(server: string, username: string, auth: Auth) {
})
}

async function fetchServerInfo(server: string, username: string, auth: Auth): Promise<ServerInfo> {
const qs = toQueryString(pickBy({
s: auth.salt,
t: auth.hash,
p: auth.password,
}, x => x !== undefined) as Record<string, string>)
const url = `${server}/rest/getOpenSubsonicExtensions?u=${username}&${qs}&v=1.15.0&c=app&f=json`
const response = await fetch(url)
if (response.ok) {
const body = await response.json()
const subsonicResponse = body['subsonic-response']
if (subsonicResponse?.status === 'ok') {
return {
name: subsonicResponse.type,
version: subsonicResponse.version,
openSubsonic: true,
extensions: subsonicResponse.openSubsonicExtensions.map((ext: any) => ext.name)
}
}
}
return { name: 'Subsonic', version: 'Unknown', openSubsonic: false, extensions: [] }
}

const apiSymbol = Symbol('')

export function useAuth(): AuthService {
Expand Down

0 comments on commit 9888679

Please sign in to comment.