Commit 09696a6e authored by Mickaël Desfrênes's avatar Mickaël Desfrênes
Browse files

upgrade the cookie/CSRF logic

parent 14948d4a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
{
  "name": "@pdn-certic/vue-jama",
  "version": "0.8.9",
  "version": "0.8.10",
  "scripts": {
    "serve": "rimraf -rf ./node_modules/.cache/vue-loader && vue-cli-service serve",
    "build": "vue-cli-service build",
+9 −13
Original line number Diff line number Diff line
import JamacRpcClient from './JamaRpcClient';
import Misc from "./Misc";
import {createAPIHeaders} from "./RpcClient";


const CHUNK_SIZE = 1024 * 1024;
@@ -43,22 +44,20 @@ class ChunksUploader {
     */
    uploadChunk(chunkNum, path, projectId) {
        let data = this.file.slice(this.chunk_indexes[chunkNum].start, this.chunk_indexes[chunkNum].end);
        let headers = createAPIHeaders(this.api_key);
        headers.set("X-file-chunk", (chunkNum + 1) + "/" + this.nb_chunks);
        headers.set("X-file-hash", this.id);
        headers.set("X-file-name", window.btoa(encodeURIComponent(this.file.name)));
        headers.set("X-Project", projectId);
        let options =
            {
                //url: this.endpoint + 'upload/partial/',
                method: 'POST',
                body: data,
                headers: {
                    "X-Api-Key": this.api_key,
                    "X-file-chunk": (chunkNum + 1) + "/" + this.nb_chunks,
                    "X-file-hash": this.id,
                    "X-file-name": window.btoa(encodeURIComponent(this.file.name)),
                    "X-Project": projectId,
                    //"X-debug-file-name" : this.file.name
                }
                headers: headers
            }
        if (path)
            options.headers['X-origin-dir'] = window.btoa(encodeURIComponent(path))
            options.headers.set('X-origin-dir', window.btoa(encodeURIComponent(path)))
        return fetch(this.endpoint + 'upload/partial/', options)
    }

@@ -292,9 +291,7 @@ class JamaClient extends JamacRpcClient {


    getMediaAsBlob(mediaId) {
        let headers = {
            "X-Api-Key": this.apiKey
        }
        let headers = createAPIHeaders(this.apiKey);
        return fetch(this.endPoint + "download/" + mediaId, {headers}).then(response => response.blob())
    }

@@ -305,4 +302,3 @@ class JamaClient extends JamacRpcClient {
}

export {JamaClient, upload_events}
+31 −3
Original line number Diff line number Diff line
function getCookie(name) {
    if (typeof document === 'undefined' || !document.cookie) {
        return '';
    }

    let cookieName = name + '=';
    let cookies = document.cookie.split(';');
    for (let i = 0; i < cookies.length; i++) {
        let cookie = cookies[i].trim();
        if (cookie.indexOf(cookieName) === 0) {
            return decodeURIComponent(cookie.substring(cookieName.length));
        }
    }
    return '';
}

function createAPIHeaders(apiKey = '') {
    let headers = new Headers();
    if (apiKey !== '' && apiKey !== null && apiKey !== undefined) {
        headers.set('X-Api-Key', apiKey);
    }
    let csrfToken = getCookie('csrftoken');
    if (csrfToken !== '') {
        headers.set('X-CSRFToken', csrfToken);
    }
    return headers;
}

class RpcClient {

    constructor(endPoint = '', apiKey ='', rpcMethodPrefix = '') {
@@ -21,8 +49,7 @@ class RpcClient {
            params: params
        };
        let body = JSON.stringify(rpc_request);
        let headers = new Headers();
        headers.set('X-Api-Key', this.apiKey);
        let headers = createAPIHeaders(this.apiKey);
        let response = await fetch(this.endPoint, {
            method: 'POST',
            headers: headers,
@@ -55,4 +82,5 @@ class RpcClient {
    }
}

export {createAPIHeaders}
export default RpcClient