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

Interceptor #2042

Merged
merged 6 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 3 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { LayoutModule } from './layout/layout.module'
import { BidiModule } from '@angular/cdk/bidi'
import { PseudoModule } from 'src/locale/i18n.pseudo.component'
import { TitleService } from './core/title-service/title.service'
import { HttpContentTypeHeaderInterceptor } from './core/http-content-type-header-interceptor/http-content-type-header-interceptor'
import { HTTP_INTERCEPTORS } from '@angular/common/http'

@NgModule({
declarations: [AppComponent],
Expand All @@ -25,7 +27,7 @@ import { TitleService } from './core/title-service/title.service'
// Environmental dependent modules
environment.SHOW_TEST_WARNING_BANNER ? EnvironmentBannerModule : [],
],
providers: [TitleService],
providers: [TitleService, {provide:HTTP_INTERCEPTORS, useClass:HttpContentTypeHeaderInterceptor, multi:true}],
bootstrap: [AppComponent],
})
export class AppModule {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { ErrorHandlerService } from '../error-handler/error-handler.service'
export class AccountActionsDeactivateService {
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'text/plain charset=utf-8',
})
options: { headers: any; responseType: 'text' }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpClient, HttpHeaders } from '@angular/common/http'
import { HttpClient } from '@angular/common/http'
import { Inject, Injectable } from '@angular/core'
import { catchError, retry, tap } from 'rxjs/operators'
import { WINDOW } from 'src/app/cdk/window'
Expand All @@ -9,10 +9,7 @@ import { ErrorHandlerService } from '../error-handler/error-handler.service'
providedIn: 'root',
})
export class AccountActionsDownloadService {
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})

constructor(
private _errorHandler: ErrorHandlerService,
private _http: HttpClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import { ErrorHandlerService } from '../error-handler/error-handler.service'
})
export class AccountActionsDuplicatedService {
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
})
constructor(
private _errorHandler: ErrorHandlerService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import { ErrorHandlerService } from '../error-handler/error-handler.service'
})
export class AccountDefaultEmailFrequenciesService {
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
})
constructor(
private _errorHandler: ErrorHandlerService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import { ErrorHandlerService } from '../error-handler/error-handler.service'
})
export class AccountDefaultVisibilityService {
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
})

constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import { ErrorHandlerService } from '../error-handler/error-handler.service'
})
export class AccountSecurityAlternateSignInService {
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
})
constructor(
private _errorHandler: ErrorHandlerService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { ErrorHandlerService } from '../error-handler/error-handler.service'
export class AccountSecurityPasswordService {
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})

constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export class AccountTrustedIndividualsService {

headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})
constructor(
private _errorHandler: ErrorHandlerService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { ErrorHandlerService } from '../error-handler/error-handler.service'
export class AccountTrustedOrganizationsService {
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})
constructor(
private _errorHandler: ErrorHandlerService,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { HttpHandler, HttpInterceptor, HttpRequest } from "@angular/common/http";
import { Injectable } from "@angular/core";

@Injectable()
export class HttpContentTypeHeaderInterceptor implements HttpInterceptor {
construct () {

}

intercept(req:HttpRequest<any>, next: HttpHandler) {
var method = req.method
var urlWithParams = req.urlWithParams
// If the request contains a content type, be sure to set the encoding to utf-8
if(['POST', 'PUT'].includes(method)) {
Copy link
Member

Choose a reason for hiding this comment

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

We might need to add PATCH in the future.
NO a change we need right now

var clonedRequest;
if(urlWithParams == '/signin/auth.json') {
Copy link
Member

@leomendoza123 leomendoza123 Aug 18, 2023

Choose a reason for hiding this comment

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

this header excepcion seems to be require for userStatus.json?logUserOut=true, sendReactivation.json, social/signin/auth.json, shibboleth/signin/auth.json, signin/auth.json (all the urls called on SignInService)

Maybe we can declare a class level atribute like

const formUrlEcondedUrls = ["userStatus.json?logUserOut=true", "sendReactivation.json", "social/signin/auth.json", "shibboleth/signin/auth.json", "signin/auth.json"];

And check if a route is in the array

if(formUrlEcondedUrls.includes(urlWithParams)) {

clonedRequest = req.clone({
headers: req.headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8')
});
} else {
clonedRequest = req.clone({
headers: req.headers.set('Content-Type', 'application/json;charset=utf-8')
});
}

console.log("-------------------------------------------------------------------------------------")
Copy link
Member

Choose a reason for hiding this comment

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

Should this logs be remove?

console.log(method)
console.log(urlWithParams)
console.log(clonedRequest.headers)
console.log("-------------------------------------------------------------------------------------")
return next.handle(clonedRequest);
}
console.log('Nothing changed for ' + method + ' - ' + urlWithParams);
// Nothing changed, continue using the same request object
return next.handle(req);
}
}
1 change: 0 additions & 1 deletion src/app/core/inbox/inbox.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export class InboxService {
) {
this.headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})
}

Expand Down
1 change: 0 additions & 1 deletion src/app/core/oauth/oauth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export class OauthService {
) {
this.headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export class PasswordRecoveryService {
) {}
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})

resetPassword(data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export class RecordAffiliationService {
lastEmittedValue: AffiliationUIGroup[]
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})

$affiliations: ReplaySubject<AffiliationUIGroup[]>
Expand Down
1 change: 0 additions & 1 deletion src/app/core/record-biography/record-biography.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export class RecordBiographyService {
$biography: ReplaySubject<BiographyEndPoint>
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})

constructor(
Expand Down
1 change: 0 additions & 1 deletion src/app/core/record-countries/record-countries.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export class RecordCountriesService {
$addresses: ReplaySubject<CountriesEndpoint>
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})

constructor(
Expand Down
1 change: 0 additions & 1 deletion src/app/core/record-emails/record-emails.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { RecordPublicSideBarService } from '../record-public-side-bar/record-pub
export class RecordEmailsService {
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})
private $emailsSubject

Expand Down
3 changes: 1 addition & 2 deletions src/app/core/record-fundings/record-fundings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import { RecordImportWizard } from '../../types/record-peer-review-import.endpoi
export class RecordFundingsService {
lastEmittedValue: FundingGroup[]
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
})

$fundings: ReplaySubject<FundingGroup[]> = new ReplaySubject<FundingGroup[]>()
Expand Down
1 change: 0 additions & 1 deletion src/app/core/record-keyword/record-keyword.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export class RecordKeywordService {
$keywords: ReplaySubject<KeywordEndPoint>
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})

constructor(
Expand Down
1 change: 0 additions & 1 deletion src/app/core/record-names/record-names.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export class RecordNamesService {
$names: ReplaySubject<NamesEndPoint>
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})

constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export class RecordOtherNamesService {
$otherNames: ReplaySubject<OtherNamesEndPoint>
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})

constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export class RecordPeerReviewService {
$peer: ReplaySubject<PeerReview[]> = new ReplaySubject<PeerReview[]>()
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})
lastEmittedValue: PeerReview[]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export class RecordPersonIdentifierService {
$privatePersonIdentifier: ReplaySubject<PersonIdentifierEndpoint>
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})

constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export class RecordPublicSideBarService {

headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})

getPublicRecordSideBar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export class RecordResearchResourceService {

headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})

constructor(
Expand Down
1 change: 0 additions & 1 deletion src/app/core/record-websites/record-websites.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export class RecordWebsitesService {
$websites: ReplaySubject<WebsitesEndPoint>
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})

constructor(
Expand Down
4 changes: 1 addition & 3 deletions src/app/core/record-works/record-works.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,7 @@ export class RecordWorksService {
JSON.stringify(obj),
{
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
}
)
Expand All @@ -448,7 +447,6 @@ export class RecordWorksService {
{
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
},
}
)
Expand Down
1 change: 0 additions & 1 deletion src/app/core/record/record.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export class RecordService {

headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})

subscriptionSourceCountDebugger = 0
Expand Down
19 changes: 6 additions & 13 deletions src/app/core/sign-in/sign-in.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'
import { HttpClient, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { catchError, map, retry, switchMap, first } from 'rxjs/operators'

Expand All @@ -16,19 +16,15 @@ import { Title } from '@angular/platform-browser'
@Injectable({
providedIn: 'root',
})
export class SignInService {
private headers: HttpHeaders
export class SignInService {

constructor(
private _http: HttpClient,
private _titleService: Title,
private _errorHandler: ErrorHandlerService,
private _userService: UserService
) {
this.headers = new HttpHeaders().set(
'Content-Type',
'application/x-www-form-urlencoded;charset=utf-8'
)

}
/**
* @param SignInLocal sign in information
Expand Down Expand Up @@ -61,8 +57,7 @@ export class SignInService {
}
body = body.set('oauthRequest', signInLocal.isOauth ? 'true' : 'false')
return this._http
.post<SignIn>(environment.API_WEB + loginUrl, body, {
headers: this.headers,
.post<SignIn>(environment.API_WEB + loginUrl, body, {
withCredentials: true,
})
.pipe(
Expand All @@ -84,8 +79,7 @@ export class SignInService {
let body = new HttpParams({ encoder: new CustomEncoder() })
body = body.set('email', email)
return this._http
.post<Reactivation>(environment.API_WEB + `sendReactivation.json`, body, {
headers: this.headers,
.post<Reactivation>(environment.API_WEB + `sendReactivation.json`, body, {
withCredentials: true,
})
.pipe(
Expand All @@ -102,8 +96,7 @@ export class SignInService {
singOut() {
this._titleService.setTitle('ORCID')
return this._http
.get<SignIn>(environment.API_WEB + 'userStatus.json?logUserOut=true', {
headers: this.headers,
.get<SignIn>(environment.API_WEB + 'userStatus.json?logUserOut=true', {
withCredentials: true,
})
.pipe(
Expand Down
1 change: 0 additions & 1 deletion src/app/core/trusted-summary/trusted-summary.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { ERROR_REPORT } from 'src/app/errors'
export class TrustedSummaryService {
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})

constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { ErrorHandlerService } from '../error-handler/error-handler.service'
export class TwoFactorAuthenticationService {
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})

constructor(
Expand Down
1 change: 0 additions & 1 deletion src/app/core/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import { UserInfoService } from '../user-info/user-info.service'
export class UserService {
headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
})

constructor(
Expand Down