Search in sources :

Example 1 with JWTAuthenticationToken

use of org.libresonic.player.security.JWTAuthenticationToken in project libresonic by Libresonic.

the class ExternalPlayerController method getSongs.

private List<MediaFileWithUrlInfo> getSongs(HttpServletRequest request, Share share, Player player) throws IOException {
    Date expires = null;
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication instanceof JWTAuthenticationToken) {
        DecodedJWT token = jwtSecurityService.verify((String) authentication.getCredentials());
        expires = token.getExpiresAt();
    }
    Date finalExpires = expires;
    List<MediaFileWithUrlInfo> result = new ArrayList<>();
    List<MusicFolder> musicFolders = settingsService.getMusicFoldersForUser(player.getUsername());
    if (share != null) {
        for (MediaFile file : shareService.getSharedFiles(share.getId(), musicFolders)) {
            if (file.getFile().exists()) {
                if (file.isDirectory()) {
                    List<MediaFile> childrenOf = mediaFileService.getChildrenOf(file, true, false, true);
                    result.addAll(childrenOf.stream().map(mf -> addUrlInfo(request, player, mf, finalExpires)).collect(Collectors.toList()));
                } else {
                    result.add(addUrlInfo(request, player, file, finalExpires));
                }
            }
        }
    }
    return result;
}
Also used : Authentication(org.springframework.security.core.Authentication) JWTAuthenticationToken(org.libresonic.player.security.JWTAuthenticationToken) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT)

Example 2 with JWTAuthenticationToken

use of org.libresonic.player.security.JWTAuthenticationToken in project libresonic by Libresonic.

the class StreamController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    TransferStatus status = null;
    PlayQueueInputStream in = null;
    Player player = playerService.getPlayer(request, response, false, true);
    User user = securityService.getUserByName(player.getUsername());
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    try {
        if (!(authentication instanceof JWTAuthenticationToken) && !user.isStreamRole()) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN, "Streaming is forbidden for user " + user.getUsername());
            return null;
        }
        // If "playlist" request parameter is set, this is a Podcast request. In that case, create a separate
        // play queue (in order to support multiple parallel Podcast streams).
        Integer playlistId = ServletRequestUtils.getIntParameter(request, "playlist");
        boolean isPodcast = playlistId != null;
        if (isPodcast) {
            PlayQueue playQueue = new PlayQueue();
            playQueue.addFiles(false, playlistService.getFilesInPlaylist(playlistId));
            player.setPlayQueue(playQueue);
            Util.setContentLength(response, playQueue.length());
            LOG.info("Incoming Podcast request for playlist " + playlistId);
        }
        response.setHeader("Access-Control-Allow-Origin", "*");
        String contentType = StringUtil.getMimeType(request.getParameter("suffix"));
        response.setContentType(contentType);
        String preferredTargetFormat = request.getParameter("format");
        Integer maxBitRate = ServletRequestUtils.getIntParameter(request, "maxBitRate");
        if (Integer.valueOf(0).equals(maxBitRate)) {
            maxBitRate = null;
        }
        VideoTranscodingSettings videoTranscodingSettings = null;
        // Is this a request for a single file (typically from the embedded Flash player)?
        // In that case, create a separate playlist (in order to support multiple parallel streams).
        // Also, enable partial download (HTTP byte range).
        MediaFile file = getSingleFile(request);
        boolean isSingleFile = file != null;
        HttpRange range = null;
        if (isSingleFile) {
            if (!(authentication instanceof JWTAuthenticationToken) && !securityService.isFolderAccessAllowed(file, user.getUsername())) {
                response.sendError(HttpServletResponse.SC_FORBIDDEN, "Access to file " + file.getId() + " is forbidden for user " + user.getUsername());
                return null;
            }
            // Update the index of the currently playing media file. At
            // this point we haven't yet modified the play queue to support
            // multiple streams, so the current play queue is the real one.
            int currentIndex = player.getPlayQueue().getFiles().indexOf(file);
            player.getPlayQueue().setIndex(currentIndex);
            // Create a new, fake play queue that only contains the
            // currently playing media file, in case multiple streams want
            // to use the same player.
            PlayQueue playQueue = new PlayQueue();
            playQueue.addFiles(true, file);
            player.setPlayQueue(playQueue);
            if (!file.isVideo()) {
                response.setIntHeader("ETag", file.getId());
                response.setHeader("Accept-Ranges", "bytes");
            }
            TranscodingService.Parameters parameters = transcodingService.getParameters(file, player, maxBitRate, preferredTargetFormat, null);
            long fileLength = getFileLength(parameters);
            boolean isConversion = parameters.isDownsample() || parameters.isTranscode();
            boolean estimateContentLength = ServletRequestUtils.getBooleanParameter(request, "estimateContentLength", false);
            boolean isHls = ServletRequestUtils.getBooleanParameter(request, "hls", false);
            range = getRange(request, file);
            if (range != null && !file.isVideo()) {
                LOG.info("Got HTTP range: " + range);
                response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
                Util.setContentLength(response, range.isClosed() ? range.size() : fileLength - range.getFirstBytePos());
                long lastBytePos = range.getLastBytePos() != null ? range.getLastBytePos() : fileLength - 1;
                response.setHeader("Content-Range", "bytes " + range.getFirstBytePos() + "-" + lastBytePos + "/" + fileLength);
            } else if (!isHls && (!isConversion || estimateContentLength)) {
                Util.setContentLength(response, fileLength);
            }
            if (isHls) {
                // HLS is always MPEG TS.
                response.setContentType(StringUtil.getMimeType("ts"));
            } else {
                String transcodedSuffix = transcodingService.getSuffix(player, file, preferredTargetFormat);
                boolean sonos = SonosHelper.LIBRESONIC_CLIENT_ID.equals(player.getClientId());
                response.setContentType(StringUtil.getMimeType(transcodedSuffix, sonos));
                setContentDuration(response, file);
            }
            if (file.isVideo() || isHls) {
                videoTranscodingSettings = createVideoTranscodingSettings(file, request);
            }
        }
        if (request.getMethod().equals("HEAD")) {
            return null;
        }
        // Terminate any other streams to this player.
        if (!isPodcast && !isSingleFile) {
            for (TransferStatus streamStatus : statusService.getStreamStatusesForPlayer(player)) {
                if (streamStatus.isActive()) {
                    streamStatus.terminate();
                }
            }
        }
        status = statusService.createStreamStatus(player);
        in = new PlayQueueInputStream(player, status, maxBitRate, preferredTargetFormat, videoTranscodingSettings, transcodingService, audioScrobblerService, mediaFileService, searchService);
        OutputStream out = RangeOutputStream.wrap(response.getOutputStream(), range);
        // Enabled SHOUTcast, if requested.
        boolean isShoutCastRequested = "1".equals(request.getHeader("icy-metadata"));
        if (isShoutCastRequested && !isSingleFile) {
            response.setHeader("icy-metaint", "" + ShoutCastOutputStream.META_DATA_INTERVAL);
            response.setHeader("icy-notice1", "This stream is served using Libresonic");
            response.setHeader("icy-notice2", "Libresonic - Free media streamer - libresonic.org");
            response.setHeader("icy-name", "Libresonic");
            response.setHeader("icy-genre", "Mixed");
            response.setHeader("icy-url", "http://libresonic.org/");
            out = new ShoutCastOutputStream(out, player.getPlayQueue(), settingsService);
        }
        final int BUFFER_SIZE = 2048;
        byte[] buf = new byte[BUFFER_SIZE];
        while (true) {
            // Check if stream has been terminated.
            if (status.terminated()) {
                return null;
            }
            if (player.getPlayQueue().getStatus() == PlayQueue.Status.STOPPED) {
                if (isPodcast || isSingleFile) {
                    break;
                } else {
                    sendDummy(buf, out);
                }
            } else {
                int n = in.read(buf);
                if (n == -1) {
                    if (isPodcast || isSingleFile) {
                        break;
                    } else {
                        sendDummy(buf, out);
                    }
                } else {
                    out.write(buf, 0, n);
                }
            }
        }
    } finally {
        if (status != null) {
            securityService.updateUserByteCounts(user, status.getBytesTransfered(), 0L, 0L);
            statusService.removeStreamStatus(status);
        }
        IOUtils.closeQuietly(in);
    }
    return null;
}
Also used : ShoutCastOutputStream(org.libresonic.player.io.ShoutCastOutputStream) OutputStream(java.io.OutputStream) RangeOutputStream(org.libresonic.player.io.RangeOutputStream) ShoutCastOutputStream(org.libresonic.player.io.ShoutCastOutputStream) PlayQueueInputStream(org.libresonic.player.io.PlayQueueInputStream) Authentication(org.springframework.security.core.Authentication) JWTAuthenticationToken(org.libresonic.player.security.JWTAuthenticationToken) HttpRange(org.libresonic.player.util.HttpRange) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

JWTAuthenticationToken (org.libresonic.player.security.JWTAuthenticationToken)2 Authentication (org.springframework.security.core.Authentication)2 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)1 OutputStream (java.io.OutputStream)1 PlayQueueInputStream (org.libresonic.player.io.PlayQueueInputStream)1 RangeOutputStream (org.libresonic.player.io.RangeOutputStream)1 ShoutCastOutputStream (org.libresonic.player.io.ShoutCastOutputStream)1 HttpRange (org.libresonic.player.util.HttpRange)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1