Skip to content
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

Add optional scope normalization #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "passport-snapchat",
"version": "1.0.0",
"version": "1.0.1",
"description": "Snapchat (OAuth 2.0) authorization strategy for Passport.",
"keywords": [
"passport",
Expand Down
16 changes: 14 additions & 2 deletions src/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ interface SnapchatStrategyOptions {
* @default ' '
*/
scopeSeparator?: string;
/**
* @optional
* Determines whether Snapchat's scope strings are normalized according to Snapchat's
* convention of prepending the scope URL prefix to the scope string.
*
* @default true
*/
normalizeScope?: boolean;
/**
* @optional
*
Expand Down Expand Up @@ -254,7 +262,7 @@ function getGraphFieldForNormalizedFieldName(field: string): string {
/**
* @hidden
*/
function normalizeScope(scope: string): string {
function applyScopeNormalization(scope: string): string {
return scope.startsWith('https:')
? scope
: config.OAUTH_SCOPE_URL_PREFIX + scope;
Expand All @@ -273,14 +281,18 @@ function normalizeOptions(
? scopesStringOrArray.split(scopeSeparator)
: scopesStringOrArray;
const profileFields = options.profileFields || [];
const shouldNormalizeScope =
options.normalizeScope !== undefined ? options.normalizeScope : true;
return {
...options,
authorizationURL: options.authorizationURL || config.SNAP_ACCOUNTS_AUTH_URL,
profileFields: profileFields
.map(getGraphFieldForNormalizedFieldName)
.filter(Boolean),
profileURL: options.profileURL || `${config.SNAP_KIT_API_URL}/me`,
scope: scopes.map(normalizeScope).filter(Boolean),
scope: shouldNormalizeScope
? scopes.map(applyScopeNormalization).filter(Boolean)
: scopes,
scopeSeparator,
tokenURL: options.tokenURL || config.SNAP_ACCOUNTS_TOKEN_URL,
};
Expand Down
17 changes: 16 additions & 1 deletion test/strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,22 @@ describe('Strategy', function() {
expect(strategy._scope[0]).to.equal(config.OAUTH_SCOPE_URL_PREFIX + 'user.display_name');
expect(strategy._scope[1]).to.equal(config.OAUTH_SCOPE_URL_PREFIX + 'user.bitmoji.avatar');
});
})
});
describe('constructed with normalize scope option set to false', function () {
var strategy = new SnapchatStrategy({
callbackURL: '',
clientID: 'ABC123',
clientSecret: 'secret',
scope: ['user.display_name', 'user.bitmoji.avatar'],
normalizeScope: false
},
function () { });

it('should not have fully qualified scopes', function () {
expect(strategy._scope[0]).to.equal('user.display_name');
expect(strategy._scope[1]).to.equal('user.bitmoji.avatar');
});
});

describe('constructed with undefined options', function() {
it('should throw', function() {
Expand Down