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

Token support for cookies #4010

Open
wants to merge 3 commits into
base: develop
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
1 change: 1 addition & 0 deletions docs/usage/search-results/tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Tokens related to connected Web Parts in the Search Results.
|**{PageContext.&lt;PropertyName&gt;}** | Resolves current SPFx page context related tokens. You can use deep paths here to access properties. Ex: `{PageContext.site.absoluteUrl}`. Use the debug template in the web part to see all tokens available under the `context` node.<br/>
| **{LegacyPageContext.&lt;PropertyName&gt;}** | Resolves current SPFx legacy page context related tokens. You can use deep paths here to access properties. Ex: `{LegacyPageContext.aadTenantId}`. <br/>
|**{QueryString.&lt;ParameterName&gt;}** <br/> | A value from a query string in the URL of the current page. For example, if the URL of the current page contains a query string such as ItemNumber=567, you could obtain the value 567 by specifying `{QueryString.ItemNumber}`. <br/> Use `{?QueryString.Parameter}` if you want the part to be omitted in case the query string parameter is not present.|
|**{Cookie.&lt;CookieName&gt;}** <br/> | The value from a cookie. For example, if a cookie called Current-Language contains a value such as en-us, you could obtain the value en-us by specifying `{Cookie.Current-Language}`. <br/> Use `{?Cookie.CookieName}` if you want the part to be omitted in case the cookie is empty or is not present.|
|**{CurrentDisplayLanguage}** <br/> |The current display language based on MUI in _ll-cc format_. <br/> |
|**{CurrentDisplayLCID}** <br/> |Numeric value of the current display language based on MUI in _ll-cc format_. <br/> |
|**{TenantUrl}** <br/> |URL of the tenant (root site)<br/> |
Expand Down
42 changes: 42 additions & 0 deletions search-parts/src/services/tokenService/TokenService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export class TokenService implements ITokenService {
inputString = this.replaceGroupTokens(inputString);
inputString = this.replaceLegacyPageContextTokens(inputString);
inputString = await this.replaceHubTokens(inputString);
inputString = this.replaceCookieTokens(inputString);

inputString = inputString.replace(/\{TenantUrl\}/gi, `https://` + window.location.host);

Expand Down Expand Up @@ -631,6 +632,47 @@ export class TokenService implements ITokenService {
return inputString;
}

/**
* Resolve current cookie related tokens
* @param inputString the input string containing tokens
*/
private replaceCookieTokens(inputString: string): string {

const cookieRegExp = /\{(?:\?){0,1}(?:Cookie)\.(.*?)\}/gi;
let modifiedString = inputString;
let matches = cookieRegExp.exec(inputString);

while (matches !== null) {
const cookieName = matches[1];
const cookieValue = this.getCookieValue(cookieName);
if (cookieValue) {
modifiedString = modifiedString.replace(matches[0], cookieValue);
}
else if (matches[0].includes("?")) {
// If Cookie Token is specified like this, {?Cookie.CookieName}, it is removed if the Cookie doesn't exist
modifiedString = modifiedString.replace(matches[0], "");
}
matches = cookieRegExp.exec(inputString);
}

return modifiedString;
}

/**
* Return the cookie value
* @param cookieName name of the cookie
*/
private getCookieValue(cookieName: string): string {
const name = cookieName + "=";
const decodedCookie = decodeURIComponent(document.cookie);
const ca = decodedCookie.split(';');
let val = ca.filter(cookie => cookie.includes(name))[0]?.split("=")[1];
if (!val) {
val = '';
}
return val;
}

private replaceAndOrOperator(inputString: string) {

// Example match: {|owstaxidmetadataalltagsinfo:{Page.<TaxnomyProperty>.TermID}}
Expand Down