-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Request-a-copy improv: Altcha recaptcha component and service
- Loading branch information
Showing
4 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Observable } from 'rxjs'; | ||
import { map } from 'rxjs/operators'; | ||
|
||
import { HALEndpointService } from '../shared/hal-endpoint.service'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class ProofOfWorkCaptchaDataService { | ||
|
||
private linkPath = 'captcha'; | ||
|
||
constructor( | ||
private halService: HALEndpointService) { | ||
} | ||
|
||
public getChallengeHref(): Observable<string> { | ||
return this.getEndpoint().pipe( | ||
map((endpoint) => endpoint + '/challenge'), | ||
); | ||
} | ||
|
||
protected getEndpoint(): Observable<string> { | ||
return this.halService.getEndpoint(this.linkPath); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/app/item-page/bitstreams/request-a-copy/altcha-captcha.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<altcha-widget | ||
id="altcha-widget" | ||
auto="{{ autoload }}" | ||
expire="100000" | ||
workers=16 | ||
challengeurl="{{ challengeUrl }}"></altcha-widget> |
46 changes: 46 additions & 0 deletions
46
src/app/item-page/bitstreams/request-a-copy/altcha-captcha.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { | ||
ComponentFixture, | ||
TestBed, | ||
} from '@angular/core/testing'; | ||
import { TranslateModule } from '@ngx-translate/core'; | ||
|
||
import { AltchaCaptchaComponent } from './altcha-captcha.component'; | ||
|
||
describe('AltchaCaptchaComponent', () => { | ||
let component: AltchaCaptchaComponent; | ||
let fixture: ComponentFixture<AltchaCaptchaComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [ | ||
TranslateModule.forRoot(), | ||
AltchaCaptchaComponent, | ||
], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(AltchaCaptchaComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create component successfully', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it('should emit payload when verification is successful', () => { | ||
const testPayload = 'test-payload'; | ||
const payloadSpy = jasmine.createSpy('payloadSpy'); | ||
component.payload.subscribe(payloadSpy); | ||
|
||
const event = new CustomEvent('statechange', { | ||
detail: { | ||
state: 'verified', | ||
payload: testPayload, | ||
}, | ||
}); | ||
|
||
document.querySelector('#altcha-widget').dispatchEvent(event); | ||
|
||
expect(payloadSpy).toHaveBeenCalledWith(testPayload); | ||
}); | ||
}); |
50 changes: 50 additions & 0 deletions
50
src/app/item-page/bitstreams/request-a-copy/altcha-captcha.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
AsyncPipe, | ||
NgIf, | ||
} from '@angular/common'; | ||
import { | ||
Component, | ||
CUSTOM_ELEMENTS_SCHEMA, | ||
EventEmitter, | ||
Input, | ||
OnInit, | ||
Output, | ||
} from '@angular/core'; | ||
import { ReactiveFormsModule } from '@angular/forms'; | ||
import { RouterLink } from '@angular/router'; | ||
import { TranslateModule } from '@ngx-translate/core'; | ||
|
||
import { VarDirective } from '../../../shared/utils/var.directive'; | ||
|
||
@Component({ | ||
selector: 'ds-altcha-captcha', | ||
templateUrl: './altcha-captcha.component.html', | ||
imports: [ | ||
TranslateModule, | ||
RouterLink, | ||
AsyncPipe, | ||
ReactiveFormsModule, | ||
NgIf, | ||
VarDirective, | ||
], | ||
schemas: [CUSTOM_ELEMENTS_SCHEMA], | ||
standalone: true, | ||
}) | ||
export class AltchaCaptchaComponent implements OnInit { | ||
|
||
@Input() challengeUrl: string; | ||
@Input() autoload: string; | ||
@Input() debug: boolean; | ||
@Output() payload = new EventEmitter<string>; | ||
|
||
ngOnInit(): void { | ||
document.querySelector('#altcha-widget').addEventListener('statechange', (ev: any) => { | ||
// state can be: unverified, verifying, verified, error | ||
if (ev.detail.state === 'verified') { | ||
// payload contains base64 encoded data for the server | ||
this.payload.emit(ev.detail.payload); | ||
} | ||
}); | ||
} | ||
|
||
} |