Fixed #15 again, now check authorization also using query param

This commit is contained in:
Eliot Berriot 2017-06-29 02:27:35 +02:00
commit 3ccb70d0a8
9 changed files with 98 additions and 8 deletions

View file

@ -5,6 +5,8 @@ import Audio from '@/audio'
import backend from '@/audio/backend'
import radios from '@/radios'
import Vue from 'vue'
import url from '@/utils/url'
import auth from '@/auth'
class Queue {
constructor (options = {}) {
@ -181,7 +183,17 @@ class Queue {
if (!file) {
return this.next()
}
this.audio = new Audio(backend.absoluteUrl(file.path), {
let path = backend.absoluteUrl(file.path)
if (auth.user.authenticated) {
// we need to send the token directly in url
// so authentication can be checked by the backend
// because for audio files we cannot use the regular Authentication
// header
path = url.updateQueryString(path, 'jwt', auth.getAuthToken())
}
this.audio = new Audio(path, {
preload: true,
autoplay: true,
rate: 1,

View file

@ -50,7 +50,7 @@ export default {
checkAuth () {
logger.default.info('Checking authentication...')
var jwt = cache.get('token')
var jwt = this.getAuthToken()
var username = cache.get('username')
if (jwt) {
this.user.authenticated = true
@ -63,9 +63,13 @@ export default {
}
},
getAuthToken () {
return cache.get('token')
},
// The object to be passed as a header for authenticated requests
getAuthHeader () {
return 'JWT ' + cache.get('token')
return 'JWT ' + this.getAuthToken()
},
fetchProfile () {

View file

@ -61,6 +61,8 @@
<script>
import auth from '@/auth'
import url from '@/utils/url'
import logger from '@/logging'
import backend from '@/audio/backend'
import PlayButton from '@/components/audio/PlayButton'
@ -121,7 +123,11 @@ export default {
},
downloadUrl () {
if (this.track.files.length > 0) {
return backend.absoluteUrl(this.track.files[0].path)
let u = backend.absoluteUrl(this.track.files[0].path)
if (auth.user.authenticated) {
u = url.updateQueryString(u, 'jwt', auth.getAuthToken())
}
return u
}
},
lyricsSearchUrl () {

11
front/src/utils/url.js Normal file
View file

@ -0,0 +1,11 @@
export default {
updateQueryString (uri, key, value) {
var re = new RegExp('([?&])' + key + '=.*?(&|$)', 'i')
var separator = uri.indexOf('?') !== -1 ? '&' : '?'
if (uri.match(re)) {
return uri.replace(re, '$1' + key + '=' + value + '$2')
} else {
return uri + separator + key + '=' + value
}
}
}