Skip to content

Commit

Permalink
feat: add optional path
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandra-miernik-stp committed Jan 23, 2025
1 parent 7f24fef commit 89badcb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
13 changes: 12 additions & 1 deletion __tests__/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import SDKError from '../src/SDKError.js';

import Identity from '../identity.js';
import { Identity, createSessionServiceUrl } from '../identity.js';
import { compareUrls, Fixtures } from './utils.js';
import { URL } from 'url';
import { URL as u } from 'whatwg-url';
Expand Down Expand Up @@ -1245,3 +1245,14 @@ describe('Identity', () => {
})
})
});

describe('Utils', () => {
describe('createSessionServiceUrl()', () => {
test('should create URL without path', () => {
expect(createSessionServiceUrl('http://test.example.com')).toEqual('http://test.example.com');
});
test('should create URL with path', () => {
expect(createSessionServiceUrl('http://example.com', 'test')).toEqual('http://example.com/test');
});
});
})
2 changes: 1 addition & 1 deletion identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
* See LICENSE.md in the project root.
*/

export { default, Identity } from './src/identity.js';
export { default, Identity, createSessionServiceUrl } from './src/identity.js';
6 changes: 5 additions & 1 deletion src/identity.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class Identity extends TinyEmitter {
* @param {object} options
* @param {string} options.clientId - Example: "1234567890abcdef12345678"
* @param {string} options.sessionDomain - Example: "https://id.site.com"
* @param {string} [options.sessionDomainPath] - Example: "id"
* @param {string} options.redirectUri - Example: "https://site.com"
* @param {string} [options.env=PRE] - Schibsted account environment: `PRE`, `PRO`, `PRO_NO`, `PRO_FI` or `PRO_DK`
* @param {function} [options.log] - A function that receives debug log information. If not set,
Expand All @@ -16,9 +17,10 @@ export class Identity extends TinyEmitter {
* @param {function} [options.callbackBeforeRedirect] - callback triggered before session refresh redirect happen
* @throws {SDKError} - If any of options are invalid
*/
constructor({ clientId, redirectUri, sessionDomain, env, log, window, callbackBeforeRedirect }: {
constructor({ clientId, redirectUri, sessionDomain, sessionDomainPath, env, log, window, callbackBeforeRedirect }: {
clientId: string;
sessionDomain: string;
sessionDomainPath?: string;
redirectUri: string;
env?: string;
log?: Function;
Expand All @@ -35,6 +37,7 @@ export class Identity extends TinyEmitter {
log: Function;
callbackBeforeRedirect: Function;
_sessionDomain: string;
_sessionDomainPath?: string;
_enableSessionCaching: boolean;
_session: {};

Expand Down Expand Up @@ -93,6 +96,7 @@ export class Identity extends TinyEmitter {
* Set site-specific session-service domain
* @private
* @param {string} domain - real URL — (**not** 'PRE' style env key)
* @param {string} [path]
* @returns {void}
*/
private _setSessionServiceUrl;
Expand Down
25 changes: 21 additions & 4 deletions src/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,26 @@ const TAB_ID_TTL = 1000 * 60 * 60 * 24 * 30;
const globalWindow = () => window;

/**
* Provides Identity functionalty to a web page
* Utils for Identity
*/

/**
* Creates a Session Service URL
* @param {string} domain
* @param {string} [path]
* @returns {string}
*/
export const createSessionServiceUrl = (domain, path) => `${domain}${path ? `/${path}` : ''}`.trim();

/**
* Provides Identity functionality to a web page
*/
export class Identity extends EventEmitter {
/**
* @param {object} options
* @param {string} options.clientId - Example: "1234567890abcdef12345678"
* @param {string} options.sessionDomain - Example: "https://id.site.com"
* @param {string} [options.sessionDomainPath=""] - Example: "id"
* @param {string} options.redirectUri - Example: "https://site.com"
* @param {string} [options.env=PRE] - Schibsted account environment: `PRE`, `PRO`, `PRO_NO`, `PRO_FI` or `PRO_DK`
* @param {function} [options.log] - A function that receives debug log information. If not set,
Expand All @@ -174,6 +187,7 @@ export class Identity extends EventEmitter {
clientId,
redirectUri,
sessionDomain,
sessionDomainPath = '',
env = 'PRE',
log,
window = globalWindow(),
Expand All @@ -196,14 +210,15 @@ export class Identity extends EventEmitter {
this.log = log;
this.callbackBeforeRedirect = callbackBeforeRedirect;
this._sessionDomain = sessionDomain;
this._sessionDomainPath = sessionDomainPath;

// Internal hack: set to false to always refresh from hassession
this._enableSessionCaching = true;

// Old session
this._session = {};

this._setSessionServiceUrl(sessionDomain);
this._setSessionServiceUrl(sessionDomain, sessionDomainPath);
this._setSpidServerUrl(env);
this._setBffServerUrl(env);
this._setOauthServerUrl(env);
Expand Down Expand Up @@ -314,13 +329,14 @@ export class Identity extends EventEmitter {
* Set site-specific session-service domain
* @private
* @param {string} domain - real URL — (**not** 'PRE' style env key)
* @param {string} [path]
* @returns {void}
*/
_setSessionServiceUrl(domain) {
_setSessionServiceUrl(domain, path) {
assert(isStr(domain), `domain parameter is invalid: ${domain}`);
const client_sdrn = `sdrn:${NAMESPACE[this.env]}:client:${this.clientId}`;
this._sessionService = new RESTClient({
serverUrl: domain,
serverUrl: createSessionServiceUrl(domain,path),
log: this.log,
defaultParams: { client_sdrn, redirect_uri: this.redirectUri, sdk_version: version },
});
Expand Down Expand Up @@ -530,6 +546,7 @@ export class Identity extends EventEmitter {
redirectUri: this.redirectUri,
env: this.env,
sessionDomain: this._sessionDomain,
sessionDomainPath: this._sessionDomainPath,
sdkVersion: version
}

Expand Down

0 comments on commit 89badcb

Please sign in to comment.