This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
231 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
class AuthScopes { | ||
public static SCOPE_DELIMITER = ','; | ||
|
||
private compressedScopes: Set<string>; | ||
private expandedScopes: Set<string>; | ||
|
||
constructor(scopes: string | string[]) { | ||
let scopesArray: string[] = []; | ||
if (typeof scopes === 'string') { | ||
scopesArray = scopes.split(new RegExp(`${AuthScopes.SCOPE_DELIMITER}\\s*`)); | ||
} else { | ||
scopesArray = scopes; | ||
} | ||
|
||
scopesArray = scopesArray.map((scope) => scope.trim()).filter((scope) => scope.length); | ||
|
||
const impliedScopes = this.getImpliedScopes(scopesArray); | ||
|
||
const scopeSet = new Set(scopesArray); | ||
const impliedSet = new Set(impliedScopes); | ||
|
||
this.compressedScopes = new Set([...scopeSet].filter((x) => !impliedSet.has(x))); | ||
this.expandedScopes = new Set([...scopeSet, ...impliedSet]); | ||
} | ||
|
||
public has(scope: string | string[] | AuthScopes) { | ||
let other: AuthScopes; | ||
|
||
if (scope instanceof AuthScopes) { | ||
other = scope; | ||
} else { | ||
other = new AuthScopes(scope); | ||
} | ||
|
||
return other.toArray().filter((x) => !this.expandedScopes.has(x)).length === 0; | ||
} | ||
|
||
public equals(otherScopes: string | string[] | AuthScopes) { | ||
let other: AuthScopes; | ||
|
||
if (otherScopes instanceof AuthScopes) { | ||
other = otherScopes; | ||
} else { | ||
other = new AuthScopes(otherScopes); | ||
} | ||
|
||
return ( | ||
this.compressedScopes.size === other.compressedScopes.size && | ||
this.toArray().filter((x) => !other.has(x)).length === 0 | ||
); | ||
} | ||
|
||
public toString() { | ||
return this.toArray().join(AuthScopes.SCOPE_DELIMITER); | ||
} | ||
|
||
public toArray() { | ||
return [...this.compressedScopes]; | ||
} | ||
|
||
private getImpliedScopes(scopesArray: string[]): string[] { | ||
return scopesArray.reduce((array: string[], current: string) => { | ||
const matches = current.match(/^(unauthenticated_)?write_(.*)$/); | ||
if (matches) { | ||
array.push(`${matches[1] ? matches[1] : ''}read_${matches[2]}`); | ||
} | ||
|
||
return array; | ||
}, []); | ||
} | ||
} | ||
|
||
export {AuthScopes}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import '../../../test/test_helper'; | ||
|
||
import {AuthScopes} from '../index'; | ||
|
||
describe('AuthScopes', () => { | ||
it('can parse and trim string scopes', () => { | ||
const scopeString = ' read_products, read_orders,,write_customers '; | ||
const scopes = new AuthScopes(scopeString); | ||
|
||
expect(scopes.toString()).toEqual('read_products,read_orders,write_customers'); | ||
}); | ||
|
||
it('can parse and trim array scopes', () => { | ||
const scopeString = [' read_products', 'read_orders', '', 'unauthenticated_write_customers ']; | ||
const scopes = new AuthScopes(scopeString); | ||
|
||
expect(scopes.toString()).toEqual('read_products,read_orders,unauthenticated_write_customers'); | ||
}); | ||
|
||
it('trims implied scopes', () => { | ||
const scopeString = 'read_customers,write_customers,read_products'; | ||
const scopes = new AuthScopes(scopeString); | ||
|
||
expect(scopes.toString()).toEqual('write_customers,read_products'); | ||
}); | ||
|
||
it('trims implied unauthenticated scopes', () => { | ||
const scopeString = 'unauthenticated_read_customers,unauthenticated_write_customers,unauthenticated_read_products'; | ||
const scopes = new AuthScopes(scopeString); | ||
|
||
expect(scopes.toString()).toEqual('unauthenticated_write_customers,unauthenticated_read_products'); | ||
}); | ||
}); | ||
|
||
describe('AuthScopes.equals', () => { | ||
it('returns true for equivalent sets', () => { | ||
const scopes1 = new AuthScopes('write_customers,read_products'); | ||
const scopes2 = new AuthScopes(['write_customers', 'read_products']); | ||
|
||
expect(scopes1.equals(scopes2)).toBeTruthy(); | ||
expect(scopes2.equals(scopes1)).toBeTruthy(); | ||
}); | ||
|
||
it('returns false for different sets', () => { | ||
const scopes1 = new AuthScopes('write_customers,read_products'); | ||
const scopes2 = new AuthScopes(['write_customers', 'write_orders']); | ||
|
||
expect(scopes1.equals(scopes2)).toBeFalsy(); | ||
expect(scopes2.equals(scopes1)).toBeFalsy(); | ||
}); | ||
|
||
it('returns true if there are implied scopes', () => { | ||
const scopes1 = new AuthScopes('write_customers,read_products,write_products'); | ||
const scopes2 = new AuthScopes(['write_customers', 'write_products']); | ||
|
||
expect(scopes1.equals(scopes2)).toBeTruthy(); | ||
expect(scopes2.equals(scopes1)).toBeTruthy(); | ||
}); | ||
|
||
it('returns false if current set is a subset of other', () => { | ||
const scopes1 = new AuthScopes('write_customers,read_products,write_products'); | ||
const scopes2 = new AuthScopes(['write_customers', 'write_products', 'write_orders']); | ||
|
||
expect(scopes1.equals(scopes2)).toBeFalsy(); | ||
expect(scopes2.equals(scopes1)).toBeFalsy(); | ||
}); | ||
|
||
it('allows comparing against strings', () => { | ||
const scopes1 = new AuthScopes('write_customers,read_products,write_products'); | ||
|
||
expect(scopes1.equals('write_customers,read_products,write_products')).toBeTruthy(); | ||
}); | ||
|
||
it('allows comparing against string arrays', () => { | ||
const scopes1 = new AuthScopes('write_customers,read_products,write_products'); | ||
|
||
expect(scopes1.equals(['write_customers', 'read_products', 'write_products'])).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe('AuthScopes.has', () => { | ||
it('returns true for subset string', () => { | ||
const scopes1 = new AuthScopes('write_customers,read_products'); | ||
|
||
expect(scopes1.has('write_customers')).toBeTruthy(); | ||
}); | ||
|
||
it('returns true for subset string array', () => { | ||
const scopes1 = new AuthScopes('write_customers,read_products'); | ||
|
||
expect(scopes1.has(['write_customers'])).toBeTruthy(); | ||
}); | ||
|
||
it('returns true for subset scopes object', () => { | ||
const scopes1 = new AuthScopes('write_customers,read_products'); | ||
const scopes2 = new AuthScopes(['write_customers']); | ||
|
||
expect(scopes1.has(scopes2)).toBeTruthy(); | ||
}); | ||
|
||
it('returns true for equal string', () => { | ||
const scopes1 = new AuthScopes('write_customers,read_products'); | ||
|
||
expect(scopes1.has('write_customers,read_products')).toBeTruthy(); | ||
}); | ||
|
||
it('returns true for equal string array', () => { | ||
const scopes1 = new AuthScopes('write_customers,read_products'); | ||
|
||
expect(scopes1.has(['write_customers', 'read_products'])).toBeTruthy(); | ||
}); | ||
|
||
it('returns true for equal scopes object', () => { | ||
const scopes1 = new AuthScopes('write_customers,read_products'); | ||
const scopes2 = new AuthScopes(['write_customers', 'read_products']); | ||
|
||
expect(scopes1.has(scopes2)).toBeTruthy(); | ||
}); | ||
|
||
it('returns false for superset string', () => { | ||
const scopes1 = new AuthScopes('write_customers,read_products'); | ||
|
||
expect(scopes1.has('write_customers,read_products,read_orders')).toBeFalsy(); | ||
}); | ||
|
||
it('returns false for superset string array', () => { | ||
const scopes1 = new AuthScopes('write_customers,read_products'); | ||
|
||
expect(scopes1.has(['write_customers', 'read_products', 'read_orders'])).toBeFalsy(); | ||
}); | ||
|
||
it('returns false for superset scopes object', () => { | ||
const scopes1 = new AuthScopes('write_customers,read_products'); | ||
const scopes2 = new AuthScopes(['write_customers', 'read_products', 'read_orders']); | ||
|
||
expect(scopes1.has(scopes2)).toBeFalsy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters