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

CXSPA-8201: code refactor #19145

Merged
merged 7 commits into from
Aug 23, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import { UserSignUp } from '@spartacus/user/profile/root';
import { OccUserProfileAdapter } from './occ-user-profile.adapter';
import { Observable, of } from 'rxjs';
import { CaptchaApiConfig, CaptchaProvider } from '@spartacus/storefront';
import { CaptchaApiConfig, CaptchaRenderer } from '@spartacus/storefront';

export const mockOccModuleConfig: OccConfig = {
backend: {
Expand Down Expand Up @@ -68,7 +68,7 @@ const user: User = {
};

const mockToken = 'mock-token';
class MockCaptchaService implements CaptchaProvider {
class MockCaptchaService implements CaptchaRenderer {
getCaptchaConfig(): Observable<CaptchaConfig> {
return of({
enabled: true,
Expand All @@ -88,7 +88,7 @@ class MockCaptchaService implements CaptchaProvider {
const mockCaptchaApiConfig: CaptchaApiConfig = {
apiUrl: 'mock-url',
fields: { 'mock-field-key': 'mock-field-value' },
captchaProvider: MockCaptchaService,
captchaRenderer: MockCaptchaService,
};

describe('OccUserProfileAdapter', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
import { Title, UserSignUp } from '@spartacus/user/profile/root';
import { Observable } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { CaptchaApiConfig, CaptchaProvider } from '@spartacus/storefront';
import { CaptchaApiConfig, CaptchaRenderer } from '@spartacus/storefront';

const CONTENT_TYPE_JSON_HEADER = { 'Content-Type': 'application/json' };
const CONTENT_TYPE_URLENCODED_HEADER = {
Expand Down Expand Up @@ -185,9 +185,9 @@ export class OccUserProfileAdapter implements UserProfileAdapter {
}

protected appendCaptchaToken(currentHeaders: HttpHeaders): HttpHeaders {
if (this.injector && this.captchaConfig?.captchaProvider) {
const provider = this.injector.get<CaptchaProvider>(
this.captchaConfig.captchaProvider
if (this.injector && this.captchaConfig?.captchaRenderer) {
const provider = this.injector.get<CaptchaRenderer>(
this.captchaConfig.captchaRenderer
);
const isCaptchaEnabled = provider
.getCaptchaConfig()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { Injectable, Type } from '@angular/core';
import { Config } from '@spartacus/core';
import { CaptchaProvider } from './captcha.model';
import { CaptchaRenderer } from './captcha.renderer';

@Injectable({
providedIn: 'root',
Expand All @@ -18,13 +18,13 @@ import { CaptchaProvider } from './captcha.model';
* Configuration for the frontend: This class defines how to render the captcha component
* and obtain the appropriate token.
*
* @property {Type<CaptchaProvider>} captchaProvider - An implementation of the CaptchaProvider
* @property {Type<CaptchaRenderer>} - An implementation of the CaptchaRenderer
* interface that renders the UI and interacts with the user.
* @property {string} [apiUrl] - An optional parameter specifying the API endpoint for captcha validation.
* @property {Object} [fields] - An optional parameter that holds additional fields needed for the captcha configuration.
*/
export abstract class CaptchaApiConfig {
captchaProvider?: Type<CaptchaProvider>;
captchaRenderer?: Type<CaptchaRenderer>;
apiUrl?: string;
fields?: { [key: string]: string };
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Observable, of } from 'rxjs';
import { CaptchaConfig } from '@spartacus/core';
import { CaptchaComponent, CaptchaProvider } from '@spartacus/storefront';
import { CaptchaComponent, CaptchaRenderer } from '@spartacus/storefront';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { CaptchaApiConfig } from './captcha-api-config';

class MockCaptchaService implements CaptchaProvider {
class MockCaptchaService implements CaptchaRenderer {
getCaptchaConfig(): Observable<CaptchaConfig> {
return of({
enabled: true,
Expand All @@ -24,13 +24,13 @@ class MockCaptchaService implements CaptchaProvider {
const mockCaptchaApiConfig: CaptchaApiConfig = {
apiUrl: 'mock-url',
fields: { 'mock-field-key': 'mock-field-value' },
captchaProvider: MockCaptchaService,
captchaRenderer: MockCaptchaService,
};

describe('Captcha Component', () => {
let component: CaptchaComponent;
let fixture: ComponentFixture<CaptchaComponent>;
let service: CaptchaProvider;
let service: CaptchaRenderer;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { of, Subscription } from 'rxjs';
import { concatMap } from 'rxjs/operators';

import { CaptchaApiConfig } from './captcha-api-config';
import { CaptchaProvider } from './captcha.model';
import { CaptchaRenderer } from './captcha.renderer';

@Component({
selector: 'cx-captcha',
Expand All @@ -45,17 +45,17 @@ export class CaptchaComponent implements AfterViewInit, OnDestroy {
* config.
*/
ngAfterViewInit(): void {
if (this.config?.captchaProvider) {
const captchaProvider = this.injector.get<CaptchaProvider>(
this.config.captchaProvider
if (this.config?.captchaRenderer) {
const captchaRenderer = this.injector.get<CaptchaRenderer>(
this.config.captchaRenderer
);
this.subscription.add(
captchaProvider
captchaRenderer
.getCaptchaConfig()
.pipe(
concatMap((captchaConfig) => {
if (captchaConfig?.enabled) {
return captchaProvider.renderCaptcha({
return captchaRenderer.renderCaptcha({
element: this.captchaRef.nativeElement,
});
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Observable } from 'rxjs';
import { CaptchaConfig } from '@spartacus/core';

export interface RenderParams {
element?: HTMLElement | string;
publicKey?: string;
}

export interface CaptchaProvider {
/**
* Renders captcha widget
*
* @params parameters required by a provider
* @returns Observable for the key returned by a provider once user validated
*/
renderCaptcha(params: RenderParams): Observable<string>;

/**
* @returns Observable for the backend request to retrieve commerce cloud
* captcha configuration (such as public key and whether captca is enabled)
* */
getCaptchaConfig(): Observable<CaptchaConfig>;

/**
* @returns captcha token
*/
getToken(): string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import { Observable } from 'rxjs';
import { RenderParams } from './captcha.model';
import { CaptchaConfig } from '@spartacus/core';

export interface CaptchaRenderer {
/**
* Renders captcha widget
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also add something like:

Implementations must use secure DOM manipulation methods to protect against XSS.

*
* @RenderParams parameters required by a provider, includes html element to append CAPTCHA UI element.
* @returns Observable for the key returned by a provider once user validated
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it called key here? It's called token everywhere else. Or are these different things?

*/
renderCaptcha(params: RenderParams): Observable<string>;

/**
* @returns Observable for the backend request to retrieve commerce cloud
* captcha configuration (such as public key and whether captca is enabled)
* */
getCaptchaConfig(): Observable<CaptchaConfig>;

/**
* @returns captcha token
*/
getToken(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import {
} from '@spartacus/core';
import { forkJoin, map, Observable, ReplaySubject, Subscription } from 'rxjs';
import { concatMap, take } from 'rxjs/operators';
import { CaptchaProvider, RenderParams } from './captcha.model';
import { RenderParams } from './captcha.model';
import { CaptchaApiConfig } from './captcha-api-config';
import { CaptchaRenderer } from './captcha.renderer';

/**
* Global function to be passes as "onload" url param for captcha <script>, to be
Expand All @@ -31,7 +32,7 @@ declare global {
@Injectable({
providedIn: 'root',
})
export abstract class CaptchaService implements CaptchaProvider, OnDestroy {
export abstract class CaptchaService implements CaptchaRenderer, OnDestroy {
protected token: string;
protected subscription = new Subscription();
protected captchaConfigSubject$ = new ReplaySubject<CaptchaConfig>(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './captcha.component';
export * from './captcha.module';
export * from './captcha.model';
export * from './captcha-api-config';
export * from './captcha.renderer';
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import { MockCaptchaService } from '../mock-captcha.service';
import { CaptchaApiConfig } from '../../captcha-api-config';

export const MockCaptchaApiConfig: CaptchaApiConfig = {
captchaProvider: MockCaptchaService,
captchaRenderer: MockCaptchaService,
};
Loading