11import { HttpHeaders } from '@angular/common/http' ;
2- import {
3- Inject ,
4- Injectable ,
5- Optional ,
6- } from '@angular/core' ;
2+ import { Inject , Injectable , Optional , } from '@angular/core' ;
73import { Router } from '@angular/router' ;
8- import {
9- select ,
10- Store ,
11- } from '@ngrx/store' ;
4+ import { select , Store , } from '@ngrx/store' ;
125import { TranslateService } from '@ngx-translate/core' ;
136import { CookieAttributes } from 'js-cookie' ;
14- import {
15- Observable ,
16- of as observableOf ,
17- } from 'rxjs' ;
18- import {
19- filter ,
20- map ,
21- startWith ,
22- switchMap ,
23- take ,
24- } from 'rxjs/operators' ;
7+ import { Observable , of as observableOf , } from 'rxjs' ;
8+ import { filter , map , startWith , switchMap , take , } from 'rxjs/operators' ;
259
2610import { environment } from '../../../environments/environment' ;
27- import {
28- REQUEST ,
29- RESPONSE ,
30- } from '../../../express.tokens' ;
11+ import { REQUEST , RESPONSE , } from '../../../express.tokens' ;
3112import { AppState } from '../../app.reducer' ;
3213import {
3314 hasNoValue ,
@@ -41,10 +22,7 @@ import {
4122import { NotificationsService } from '../../shared/notifications/notifications.service' ;
4223import { createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils' ;
4324import { followLink } from '../../shared/utils/follow-link-config.model' ;
44- import {
45- buildPaginatedList ,
46- PaginatedList ,
47- } from '../data/paginated-list.model' ;
25+ import { buildPaginatedList , PaginatedList , } from '../data/paginated-list.model' ;
4826import { RemoteData } from '../data/remote-data' ;
4927import { HttpOptions } from '../dspace-rest/dspace-rest.service' ;
5028import { EPersonDataService } from '../eperson/eperson-data.service' ;
@@ -54,13 +32,16 @@ import { CookieService } from '../services/cookie.service';
5432import { HardRedirectService } from '../services/hard-redirect.service' ;
5533import { RouteService } from '../services/route.service' ;
5634import {
57- NativeWindowRef ,
58- NativeWindowService ,
59- } from '../services/window.service' ;
60- import {
61- getAllSucceededRemoteDataPayload ,
62- getFirstCompletedRemoteData ,
63- } from '../shared/operators' ;
35+ getAuthenticatedUserId ,
36+ getAuthenticationToken ,
37+ getExternalAuthCookieStatus ,
38+ getRedirectUrl ,
39+ isAuthenticated ,
40+ isAuthenticatedLoaded ,
41+ isIdle ,
42+ isTokenRefreshing
43+ } from './selectors' ;
44+ import { getAllSucceededRemoteDataPayload , getFirstCompletedRemoteData , } from '../shared/operators' ;
6445import { PageInfo } from '../shared/page-info.model' ;
6546import {
6647 CheckAuthenticationTokenAction ,
@@ -74,20 +55,11 @@ import {
7455import { AuthRequestService } from './auth-request.service' ;
7556import { AuthMethod } from './models/auth.method' ;
7657import { AuthStatus } from './models/auth-status.model' ;
77- import {
78- AuthTokenInfo ,
79- TOKENITEM ,
80- } from './models/auth-token-info.model' ;
81- import {
82- getAuthenticatedUserId ,
83- getAuthenticationToken ,
84- getExternalAuthCookieStatus ,
85- getRedirectUrl ,
86- isAuthenticated ,
87- isAuthenticatedLoaded ,
88- isIdle ,
89- isTokenRefreshing ,
90- } from './selectors' ;
58+ import { AuthTokenInfo , TOKENITEM , } from './models/auth-token-info.model' ;
59+ import { NoContent } from '../shared/NoContent.model' ;
60+ import { URLCombiner } from '../url-combiner/url-combiner' ;
61+ import { MachineToken } from './models/machine-token.model' ;
62+ import { NativeWindowRef , NativeWindowService } from '../services/window.service' ;
9163
9264export const LOGIN_ROUTE = '/login' ;
9365export const LOGOUT_ROUTE = '/logout' ;
@@ -579,6 +551,31 @@ export class AuthService {
579551 } ) ;
580552 }
581553
554+ /**
555+ * Returns the external server redirect URL.
556+ * @param origin - The origin route.
557+ * @param redirectRoute - The redirect route.
558+ * @param location - The location.
559+ * @returns The external server redirect URL.
560+ */
561+ getExternalServerRedirectUrl ( origin : string , redirectRoute : string , location : string ) : string {
562+ const correctRedirectUrl = new URLCombiner ( origin , redirectRoute ) . toString ( ) ;
563+
564+ let externalServerUrl = location ;
565+ const myRegexp = / \? r e d i r e c t U r l = ( .* ) / g;
566+ const match = myRegexp . exec ( location ) ;
567+ const redirectUrlFromServer = ( match && match [ 1 ] ) ? match [ 1 ] : null ;
568+
569+ // Check whether the current page is different from the redirect url received from rest
570+ if ( isNotNull ( redirectUrlFromServer ) && redirectUrlFromServer !== correctRedirectUrl ) {
571+ // change the redirect url with the current page url
572+ const newRedirectUrl = `?redirectUrl=${ correctRedirectUrl } ` ;
573+ externalServerUrl = location . replace ( / \? r e d i r e c t U r l = ( .* ) / g, newRedirectUrl ) ;
574+ }
575+
576+ return externalServerUrl ;
577+ }
578+
582579 /**
583580 * Clear redirect url
584581 */
@@ -664,4 +661,18 @@ export class AuthService {
664661 }
665662 }
666663
664+ /**
665+ * Create a new machine token for the current user
666+ */
667+ public createMachineToken ( ) : Observable < RemoteData < MachineToken > > {
668+ return this . authRequestService . postToMachineTokenEndpoint ( ) ;
669+ }
670+
671+ /**
672+ * Delete the machine token for the current user
673+ */
674+ public deleteMachineToken ( ) : Observable < RemoteData < NoContent > > {
675+ return this . authRequestService . deleteToMachineTokenEndpoint ( ) ;
676+ }
677+
667678}
0 commit comments