Skip to content

Commit

Permalink
Store authentication data in cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrylyzo committed Apr 11, 2021
1 parent 4da6676 commit 814acf8
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 6 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"version": "1.6.0",
"description": "API client for Jellyfin",
"main": "dist/jellyfin-apiclient.js",
"dependencies": {},
"dependencies": {
"js-cookie": "^2.2.1"
},
"devDependencies": {
"@babel/core": "^7.9.6",
"@babel/preset-env": "^7.9.6",
Expand Down
8 changes: 6 additions & 2 deletions src/apiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,9 @@ class ApiClient {
* Authenticates a user
* @param {String} name
* @param {String} password
* @param {String} rememberMe
*/
authenticateUserByName(name, password) {
authenticateUserByName(name, password, rememberMe) {
if (!name) {
return Promise.reject();
}
Expand All @@ -525,6 +526,7 @@ class ApiClient {
};

if (this.onAuthenticated) {
result.enableAutoLogin = rememberMe;
this.onAuthenticated(this, result).then(afterOnAuthenticated);
} else {
afterOnAuthenticated();
Expand All @@ -537,8 +539,9 @@ class ApiClient {
/**
* Authenticates a user using quick connect
* @param {String} token
* @param {String} rememberMe
*/
quickConnect(token) {
quickConnect(token, rememberMe) {
if (!token) {
return Promise.reject();
}
Expand All @@ -564,6 +567,7 @@ class ApiClient {
};

if (this.onAuthenticated) {
result.enableAutoLogin = rememberMe;
this.onAuthenticated(this, result).then(afterOnAuthenticated);
} else {
afterOnAuthenticated();
Expand Down
9 changes: 7 additions & 2 deletions src/connectionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export default class ConnectionManager {

apiClient.serverInfo(existingServer);

apiClient.onAuthenticated = (instance, result) => onAuthenticated(instance, result, {}, true);
apiClient.onAuthenticated = (instance, result) => onAuthenticated(instance, result, {enableAutoLogin: result.enableAutoLogin}, true);

if (!existingServers.length) {
const credentials = credentialProvider.credentials();
Expand Down Expand Up @@ -291,7 +291,7 @@ export default class ConnectionManager {
apiClient.serverInfo(server);

apiClient.onAuthenticated = (instance, result) => {
return onAuthenticated(instance, result, {}, true);
return onAuthenticated(instance, result, {enableAutoLogin: result.enableAutoLogin}, true);
};

events.trigger(self, 'apiclientcreated', [apiClient]);
Expand Down Expand Up @@ -333,6 +333,9 @@ export default class ConnectionManager {
server.AccessToken = null;
}

// set to true to store authentication data
server.EnableAutoLogin = !!options.enableAutoLogin;

credentialProvider.addOrUpdateServer(credentials.Servers, server);
credentialProvider.credentials(credentials);

Expand Down Expand Up @@ -460,6 +463,8 @@ export default class ConnectionManager {
server.AccessToken = null;
server.ExchangeToken = null;
}

credentialProvider.credentials(credentials);
});
};

Expand Down
61 changes: 60 additions & 1 deletion src/credentials.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,76 @@
import events from './events';
import appStorage from './appStorage';
import cookies from 'js-cookie';

function readSession(key, server) {
const value = cookies.get(`${key}-${server.Id}`);

if (value) {
try {
const { UserId, AccessToken } = JSON.parse(value);
server.UserId = UserId || server.UserId;
server.AccessToken = AccessToken || server.AccessToken;
} catch (e) {
console.error(e);
}
}
}

function saveSession(key, server) {
cookies.set(`${key}-${server.Id}`, JSON.stringify({
UserId: server.UserId,
AccessToken: server.AccessToken
}));
}

function removeSession(key) {
const prefix = `${key}-`;

for (const cookie in cookies.get()) {
if (cookie.startsWith(prefix)) {
cookies.remove(cookie);
}
}
}

function initialize(appStorage, key) {
const json = appStorage.getItem(key) || '{}';

console.log(`Stored JSON credentials: ${json}`);
let credentials = JSON.parse(json);
credentials.Servers = credentials.Servers || [];

for (const server of credentials.Servers) {
readSession(key, server);
}

return credentials;
}

function set(instance, data) {
if (data) {
instance._credentials = data;
instance.appStorage.setItem(instance.key, JSON.stringify(data));

const dataCopy = JSON.parse(JSON.stringify(data));

// Remove session data so we don't leave removed servers
removeSession(instance.key);

for (const server of dataCopy.Servers || []) {
if (server.UserId && server.AccessToken) {
saveSession(instance.key, server);
} else {
delete server.EnableAutoLogin;
}

if (!server.EnableAutoLogin) {
delete server.UserId;
delete server.AccessToken;
delete server.EnableAutoLogin;
}
}

instance.appStorage.setItem(instance.key, JSON.stringify(dataCopy));
} else {
instance.clear();
}
Expand All @@ -29,6 +86,7 @@ export default class Credentials {
}

clear() {
removeSession(this.key);
this._credentials = null;
this.appStorage.removeItem(this.key);
}
Expand Down Expand Up @@ -57,6 +115,7 @@ export default class Credentials {
if (server.AccessToken) {
existing.AccessToken = server.AccessToken;
existing.UserId = server.UserId;
existing.EnableAutoLogin = server.EnableAutoLogin;
}
if (server.ExchangeToken) {
existing.ExchangeToken = server.ExchangeToken;
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3975,6 +3975,11 @@ jest@^26.0.1:
import-local "^3.0.2"
jest-cli "^26.6.3"

js-cookie@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/js-cookie/-/js-cookie-2.2.1.tgz#69e106dc5d5806894562902aa5baec3744e9b2b8"
integrity sha512-HvdH2LzI/EAZcUwA8+0nKNtWHqS+ZmijLA30RwZA0bo7ToCckjK5MkGhjED9KoRcXO6BaGI3I9UIzSA1FKFPOQ==

js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
Expand Down

0 comments on commit 814acf8

Please sign in to comment.