Skip to content

[msal-extensions] Extensions #6 #1849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 130 additions & 1 deletion extensions/msal-node-extensions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion extensions/msal-node-extensions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
"tsdx": "^0.13.2",
"tslib": "^2.0.0",
"typescript": "^3.9.3",
"node-gyp": "^7.0.0"
"node-gyp": "^7.0.0"
}
}
13 changes: 8 additions & 5 deletions extensions/msal-node-extensions/src/lock/CrossPlatformLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,17 @@ export class CrossPlatformLock {
/**
* Locks cache from read or writes by creating file with same path and name as
* cache file but with .lockfile extension. If another process has already created
* the lockfile, will retry again based on configuration settings set by CrossPlatformLockOptions
* the lockfile, will back off and retry based on configuration settings set by CrossPlatformLockOptions
*/
public async lock(): Promise<void> {
const processId = pid.toString();
for (let tryCount = 0; tryCount < this.retryNumber; tryCount++)
for (let tryCount = 0; tryCount < this.retryNumber; tryCount++) {
try {
this.logger.info(`Pid ${pid} trying to acquire lock`);
this.lockFileHandle = await fs.open(this.lockFilePath, "wx+");

this.logger.info(`Pid ${pid} acquired lock`);
await fs.write(this.lockFileHandle, processId);
break;
await this.lockFileHandle.write(pid.toString());
return;
} catch (err) {
if (err.code == Constants.EEXIST_ERROR) {
this.logger.info(err);
Expand All @@ -52,6 +51,10 @@ export class CrossPlatformLock {
throw PersistenceError.createCrossPlatformLockError(err.code, err.message);
}
}
}
throw PersistenceError.createCrossPlatformLockError(
"Exceeded retry options",
"Not able to acquire lock. Exceeded amount of retries set in options");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class KeychainPersistence implements IPersistence {

public async delete(): Promise<boolean> {
try {
await this.filePersistence.delete();
return await deletePassword(this.serviceName, this.accountName);
} catch (err) {
throw PersistenceError.createKeychainPersistenceError(err.code, err.message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class LibSecretPersistence implements IPersistence {
fileLocation: string,
serviceName: string,
accountName: string,
loggerOptions: LoggerOptions): Promise<LibSecretPersistence> {
loggerOptions?: LoggerOptions): Promise<LibSecretPersistence> {

const persistence = new LibSecretPersistence(serviceName, accountName);
persistence.filePersistence = await FilePersistence.create(fileLocation, loggerOptions);
Expand All @@ -58,6 +58,7 @@ export class LibSecretPersistence implements IPersistence {

public async delete(): Promise<boolean> {
try {
await this.filePersistence.delete();
return await deletePassword(this.serviceName, this.accountName);
} catch (err) {
throw PersistenceError.createLibSecretError(err.code, err.message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Logger } from "@azure/msal-common";
* - FilePersistence: Writes and reads from an unencrypted file. Can be used on Windows,
* macOs, or Linux.
* - FilePersistenceWithDataProtection: Used on Windows, writes and reads from file encrypted
* with windows dpapi.
* with windows dpapi-addon.
* - KeychainPersistence: Used on macOs, writes and reads from keychain.
* - LibSecretPersistence: Used on linux, writes and reads from secret service API. Requires
* libsecret be installed.
Expand All @@ -36,16 +36,16 @@ export class PersistenceCachePlugin {
constructor(persistence: IPersistence, lockOptions?: CrossPlatformLockOptions) {
this.persistence = persistence;

// initialize logger
this.logger = persistence.getLogger();

// create file lock
this.lockFilePath = `${this.persistence.getFilePath()}.lockfile`;
this.crossPlatformLock = new CrossPlatformLock(this.lockFilePath, lockOptions);
this.crossPlatformLock = new CrossPlatformLock(this.lockFilePath, this.logger, lockOptions);

// initialize default values
this.lastSync = 0;
this.currentCache = null;

// initialize logger
this.logger = persistence.getLogger();
}

/**
Expand Down
39 changes: 39 additions & 0 deletions extensions/msal-node-extensions/test/dpapi-addon/Dpapi.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

import Dpapi from "../../src/dpapi-addon/Dpapi";
import { DataProtectionScope } from "../../src";
import { platform } from "process"

// DPAPI is only available on windows
if(platform === "win32"){
describe('Test DPAPI addon', () => {
test('Protect and Unprotect data', () => {
const data = Buffer.from("DPAPITestString");

const encryptedData = Dpapi.protectData(data, null, DataProtectionScope.CurrentUser);
const decryptedData = Dpapi.unprotectData(encryptedData, null, DataProtectionScope.CurrentUser);
expect(decryptedData).toEqual(data);
});

test('Protect and Unprotect data with entropy', () => {
const data = Buffer.from("DPAPITestString");
const entropy = Buffer.from("entropy");

const encryptedData = Dpapi.protectData(data, entropy, DataProtectionScope.CurrentUser);
const decryptedData = Dpapi.unprotectData(encryptedData, entropy, DataProtectionScope.CurrentUser);
expect(decryptedData).toEqual(data);
});

test('Protect and Unprotect data with local machine scope', () => {
const data = Buffer.from("DPAPITestString");

const encryptedData = Dpapi.protectData(data, null, DataProtectionScope.LocalMachine);
const decryptedData = Dpapi.unprotectData(encryptedData, null, DataProtectionScope.LocalMachine);
expect(decryptedData).toEqual(data);
});
});
}

Loading