mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-18 07:23:03 +00:00
[UXP-10] token passing in header
This commit is contained in:

committed by
Sufiyan Shaikh

parent
2ef701a231
commit
fcad492a25
@@ -9,6 +9,8 @@ import { createSuccessfulRemoteDataObject } from '../../shared/remote-data.utils
|
|||||||
import { of as observableOf } from 'rxjs';
|
import { of as observableOf } from 'rxjs';
|
||||||
import { TestScheduler } from 'rxjs/testing';
|
import { TestScheduler } from 'rxjs/testing';
|
||||||
import { RequestEntry } from './request-entry.model';
|
import { RequestEntry } from './request-entry.model';
|
||||||
|
import { HttpHeaders } from '@angular/common/http';
|
||||||
|
import { HttpOptions } from '../dspace-rest/dspace-rest.service';
|
||||||
|
|
||||||
describe('EpersonRegistrationService', () => {
|
describe('EpersonRegistrationService', () => {
|
||||||
let testScheduler;
|
let testScheduler;
|
||||||
@@ -79,8 +81,23 @@ describe('EpersonRegistrationService', () => {
|
|||||||
it('should send an email registration', () => {
|
it('should send an email registration', () => {
|
||||||
|
|
||||||
const expected = service.registerEmail('test@mail.org');
|
const expected = service.registerEmail('test@mail.org');
|
||||||
|
let headers = new HttpHeaders();
|
||||||
|
const options: HttpOptions = Object.create({});
|
||||||
|
options.headers = headers;
|
||||||
|
|
||||||
expect(requestService.send).toHaveBeenCalledWith(new PostRequest('request-id', 'rest-url/registrations', registration));
|
expect(requestService.send).toHaveBeenCalledWith(new PostRequest('request-id', 'rest-url/registrations', registration, options));
|
||||||
|
expect(expected).toBeObservable(cold('(a|)', { a: rd }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should send an email registration with captcha', () => {
|
||||||
|
|
||||||
|
const expected = service.registerEmail('test@mail.org', 'afreshcaptchatoken');
|
||||||
|
let headers = new HttpHeaders();
|
||||||
|
const options: HttpOptions = Object.create({});
|
||||||
|
headers = headers.append('X-Recaptcha-Token', 'afreshcaptchatoken');
|
||||||
|
options.headers = headers;
|
||||||
|
|
||||||
|
expect(requestService.send).toHaveBeenCalledWith(new PostRequest('request-id', 'rest-url/registrations', registration, options));
|
||||||
expect(expected).toBeObservable(cold('(a|)', { a: rd }));
|
expect(expected).toBeObservable(cold('(a|)', { a: rd }));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@@ -12,6 +12,8 @@ import { GenericConstructor } from '../shared/generic-constructor';
|
|||||||
import { RegistrationResponseParsingService } from './registration-response-parsing.service';
|
import { RegistrationResponseParsingService } from './registration-response-parsing.service';
|
||||||
import { RemoteData } from './remote-data';
|
import { RemoteData } from './remote-data';
|
||||||
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
|
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
|
||||||
|
import { HttpOptions } from '../dspace-rest/dspace-rest.service';
|
||||||
|
import { HttpHeaders } from '@angular/common/http';
|
||||||
|
|
||||||
@Injectable(
|
@Injectable(
|
||||||
{
|
{
|
||||||
@@ -57,18 +59,22 @@ export class EpersonRegistrationService {
|
|||||||
registerEmail(email: string, captchaToken: string = null): Observable<RemoteData<Registration>> {
|
registerEmail(email: string, captchaToken: string = null): Observable<RemoteData<Registration>> {
|
||||||
const registration = new Registration();
|
const registration = new Registration();
|
||||||
registration.email = email;
|
registration.email = email;
|
||||||
if (captchaToken) {
|
|
||||||
registration.captchaToken = captchaToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
const requestId = this.requestService.generateRequestId();
|
const requestId = this.requestService.generateRequestId();
|
||||||
|
|
||||||
const href$ = this.getRegistrationEndpoint();
|
const href$ = this.getRegistrationEndpoint();
|
||||||
|
|
||||||
|
const options: HttpOptions = Object.create({});
|
||||||
|
let headers = new HttpHeaders();
|
||||||
|
if (captchaToken) {
|
||||||
|
headers = headers.append('X-Recaptcha-Token', captchaToken);
|
||||||
|
}
|
||||||
|
options.headers = headers;
|
||||||
|
|
||||||
href$.pipe(
|
href$.pipe(
|
||||||
find((href: string) => hasValue(href)),
|
find((href: string) => hasValue(href)),
|
||||||
map((href: string) => {
|
map((href: string) => {
|
||||||
const request = new PostRequest(requestId, href, registration);
|
const request = new PostRequest(requestId, href, registration, options);
|
||||||
this.requestService.send(request);
|
this.requestService.send(request);
|
||||||
})
|
})
|
||||||
).subscribe();
|
).subscribe();
|
||||||
|
@@ -5,6 +5,8 @@ import { isNotEmpty } from '../../shared/empty.util';
|
|||||||
import { DOCUMENT } from '@angular/common';
|
import { DOCUMENT } from '@angular/common';
|
||||||
import { ConfigurationDataService } from '../data/configuration-data.service';
|
import { ConfigurationDataService } from '../data/configuration-data.service';
|
||||||
import { RemoteData } from '../data/remote-data';
|
import { RemoteData } from '../data/remote-data';
|
||||||
|
import { map } from 'rxjs/operators';
|
||||||
|
import { combineLatest } from 'rxjs';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A GoogleRecaptchaService used to send action and get a token from REST
|
* A GoogleRecaptchaService used to send action and get a token from REST
|
||||||
@@ -13,6 +15,9 @@ import { RemoteData } from '../data/remote-data';
|
|||||||
export class GoogleRecaptchaService {
|
export class GoogleRecaptchaService {
|
||||||
|
|
||||||
private renderer: Renderer2;
|
private renderer: Renderer2;
|
||||||
|
/**
|
||||||
|
* A Google Recaptcha site key
|
||||||
|
*/
|
||||||
captchaSiteKey: string;
|
captchaSiteKey: string;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@@ -21,12 +26,21 @@ export class GoogleRecaptchaService {
|
|||||||
private configService: ConfigurationDataService,
|
private configService: ConfigurationDataService,
|
||||||
) {
|
) {
|
||||||
this.renderer = rendererFactory.createRenderer(null, null);
|
this.renderer = rendererFactory.createRenderer(null, null);
|
||||||
this.configService.findByPropertyName('google.recaptcha.key').pipe(
|
const registrationVerification$ = this.configService.findByPropertyName('registration.verification.enabled').pipe(
|
||||||
getFirstCompletedRemoteData(),
|
getFirstCompletedRemoteData(),
|
||||||
).subscribe((res: RemoteData<ConfigurationProperty>) => {
|
map((res: RemoteData<ConfigurationProperty>) => {
|
||||||
if (res.hasSucceeded && isNotEmpty(res?.payload?.values[0])) {
|
return res.hasSucceeded && res.payload && isNotEmpty(res.payload.values) && res.payload.values[0].toLowerCase() === 'true';
|
||||||
this.captchaSiteKey = res?.payload?.values[0];
|
})
|
||||||
this.loadScript(this.buildCaptchaUrl(res?.payload?.values[0]));
|
);
|
||||||
|
const recaptchaKey$ = this.configService.findByPropertyName('google.recaptcha.key.site').pipe(
|
||||||
|
getFirstCompletedRemoteData(),
|
||||||
|
);
|
||||||
|
combineLatest(registrationVerification$, recaptchaKey$).subscribe(([registrationVerification, recaptchaKey]) => {
|
||||||
|
if (registrationVerification) {
|
||||||
|
if (recaptchaKey.hasSucceeded && isNotEmpty(recaptchaKey?.payload?.values[0])) {
|
||||||
|
this.captchaSiteKey = recaptchaKey?.payload?.values[0];
|
||||||
|
this.loadScript(this.buildCaptchaUrl(recaptchaKey?.payload?.values[0]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -45,7 +59,7 @@ export class GoogleRecaptchaService {
|
|||||||
* @param key contains a secret key of a google captchas
|
* @param key contains a secret key of a google captchas
|
||||||
* @returns string which has google captcha url with google captchas key
|
* @returns string which has google captcha url with google captchas key
|
||||||
*/
|
*/
|
||||||
buildCaptchaUrl(key: string) {
|
buildCaptchaUrl(key: string) {
|
||||||
return `https://www.google.com/recaptcha/api.js?render=${key}`;
|
return `https://www.google.com/recaptcha/api.js?render=${key}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,7 +69,7 @@ export class GoogleRecaptchaService {
|
|||||||
* @param url contains a script url which will be loaded into page
|
* @param url contains a script url which will be loaded into page
|
||||||
* @returns A promise
|
* @returns A promise
|
||||||
*/
|
*/
|
||||||
private loadScript(url) {
|
private loadScript(url) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const script = this.renderer.createElement('script');
|
const script = this.renderer.createElement('script');
|
||||||
script.type = 'text/javascript';
|
script.type = 'text/javascript';
|
||||||
|
@@ -33,9 +33,4 @@ export class Registration implements UnCacheableObject {
|
|||||||
* The token linked to the registration
|
* The token linked to the registration
|
||||||
*/
|
*/
|
||||||
groups: string[];
|
groups: string[];
|
||||||
|
|
||||||
/**
|
|
||||||
* The captcha token linked to the registration
|
|
||||||
*/
|
|
||||||
captchaToken: string;
|
|
||||||
}
|
}
|
||||||
|
@@ -76,21 +76,21 @@ export class RegisterEmailFormComponent implements OnInit {
|
|||||||
if (this.registrationVerification) {
|
if (this.registrationVerification) {
|
||||||
const token = await this.googleRecaptchaService.getRecaptchaToken('register_email');
|
const token = await this.googleRecaptchaService.getRecaptchaToken('register_email');
|
||||||
if (isNotEmpty(token)) {
|
if (isNotEmpty(token)) {
|
||||||
this.registeration(token);
|
this.registration(token);
|
||||||
} else {
|
} else {
|
||||||
this.notificationService.error(this.translateService.get(`${this.MESSAGE_PREFIX}.error.head`),
|
this.notificationService.error(this.translateService.get(`${this.MESSAGE_PREFIX}.error.head`),
|
||||||
this.translateService.get(`${this.MESSAGE_PREFIX}.error.recaptcha`, {email: this.email.value}));
|
this.translateService.get(`${this.MESSAGE_PREFIX}.error.recaptcha`, {email: this.email.value}));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.registeration();
|
this.registration();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register an email address
|
* Registration of an email address
|
||||||
*/
|
*/
|
||||||
registeration(captchaToken = null) {
|
registration(captchaToken = null) {
|
||||||
let registerEmail$;
|
let registerEmail$;
|
||||||
if (captchaToken) {
|
if (captchaToken) {
|
||||||
registerEmail$ = this.epersonRegistrationService.registerEmail(this.email.value, captchaToken);
|
registerEmail$ = this.epersonRegistrationService.registerEmail(this.email.value, captchaToken);
|
||||||
|
Reference in New Issue
Block a user