Skip to content

Commit 0f2cf74

Browse files
authored
Fixed authorization header remove logic (#1940)
1 parent 7abc96f commit 0f2cf74

File tree

4 files changed

+48
-23
lines changed

4 files changed

+48
-23
lines changed

src/components/operations/operation-details/ko/runtime/authorization.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class Authorization {
3636
public readonly products: ko.Observable<Product[]>;
3737
public readonly selectedSubscriptionKey: ko.Observable<string>;
3838
public readonly collapsedAuth: ko.Observable<boolean>;
39+
private deleteAuthorizationHeader: boolean = false;
3940

4041
constructor(
4142
private readonly sessionManager: SessionManager,
@@ -137,15 +138,22 @@ export class Authorization {
137138
}
138139

139140
private setAuthorizationHeader(accessToken: string): void {
140-
this.removeAuthorizationHeader();
141+
const authorizationHeader = this.getAuthorizationHeader();
141142

143+
if (authorizationHeader) {
144+
authorizationHeader.value(accessToken);
145+
this.deleteAuthorizationHeader = false;
146+
return;
147+
}
148+
149+
this.deleteAuthorizationHeader = true;
142150
const keyHeader = new ConsoleHeader();
143151
keyHeader.name(KnownHttpHeaders.Authorization);
144152
keyHeader.description = "Subscription key.";
145-
keyHeader.secret = true;
153+
keyHeader.secret(true);
146154
keyHeader.inputTypeValue("password");
147155
keyHeader.type = "string";
148-
keyHeader.required = true;
156+
keyHeader.required = false;
149157
keyHeader.value(accessToken);
150158

151159
if (!this.isGraphQL()) {
@@ -174,6 +182,10 @@ export class Authorization {
174182
return this.findHeader(subscriptionKeyHeaderName);
175183
}
176184

185+
private getAuthorizationHeader(): ConsoleHeader {
186+
return this.findHeader(KnownHttpHeaders.Authorization);
187+
}
188+
177189
private setSubscriptionKeyHeader(subscriptionKey: string): void {
178190
this.removeSubscriptionKeyHeader();
179191

@@ -186,7 +198,7 @@ export class Authorization {
186198
const keyHeader = new ConsoleHeader();
187199
keyHeader.name(subscriptionKeyHeaderName);
188200
keyHeader.description = "Subscription key.";
189-
keyHeader.secret = true;
201+
keyHeader.secret(true);
190202
keyHeader.inputTypeValue("password");
191203
keyHeader.type = "string";
192204
keyHeader.required = true;
@@ -201,29 +213,29 @@ export class Authorization {
201213
}
202214
}
203215

204-
private async clearStoredCredentials(grantTypeChanged?: boolean): Promise<void> {
216+
private async clearStoredCredentials(): Promise<void> {
205217
await this.sessionManager.removeItem(oauthSessionKey);
206-
207-
if (grantTypeChanged) {
208-
this.removeAuthorizationHeader(true);
209-
}
210218
}
211219

212-
private removeAuthorizationHeader(clearValue: boolean = false): void {
213-
const authorizationHeader = this.findHeader(KnownHttpHeaders.Authorization);
220+
private removeAuthorizationHeader(): void {
221+
const authorizationHeader = this.getAuthorizationHeader();
214222

215-
if (clearValue && authorizationHeader && authorizationHeader.required) {
216-
authorizationHeader.value(null);
217-
} else {
218-
this.removeHeader(authorizationHeader);
223+
if (authorizationHeader) {
224+
if (!this.deleteAuthorizationHeader) {
225+
authorizationHeader.value(null);
226+
} else {
227+
this.removeHeader(authorizationHeader);
228+
}
219229
}
230+
220231
this.authenticated(false);
221232
}
222233

223234
private async onGrantTypeChange(grantType: string): Promise<void> {
224-
await this.clearStoredCredentials(true);
235+
await this.clearStoredCredentials();
225236

226237
if (!grantType || grantType === GrantTypes.password) {
238+
this.removeAuthorizationHeader();
227239
return;
228240
}
229241

src/components/operations/operation-details/ko/runtime/operation-console.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,12 @@ <h3>Headers
219219
<!-- /ko -->
220220
<!-- ko if: !header.options || header.options.length === 0 -->
221221
<div class="input-group has-validation">
222-
<!-- ko if: !header.secret -->
222+
<!-- ko if: !header.secret() -->
223223
<input type="text" autocomplete="off" class="form-control form-control-sm"
224224
placeholder="value" spellcheck="false" aria-label="Header value"
225225
data-bind="textInput: header.value, attr:{'aria-required': header.required}">
226226
<!-- /ko -->
227-
<!-- ko if: header.secret -->
227+
<!-- ko if: header.secret() -->
228228
<input autocomplete="off" class="form-control form-control-sm" placeholder="value"
229229
spellcheck="false" aria-label="Header value"
230230
data-bind="attr: {type: header.inputTypeValue, 'aria-required': header.required}, textInput: header.value">

src/components/operations/operation-details/ko/runtime/operation-console.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ export class OperationConsole {
238238
this.setVersionHeader();
239239
}
240240

241-
this.consoleOperation().request.meaningfulHeaders().forEach(header => header.value.subscribe(_ => this.updateRequestSummary()));
241+
this.consoleOperation().request.headers().forEach(header => header.value.subscribe(_ => this.updateRequestSummary()));
242+
this.consoleOperation().request.headers().forEach(header => header.name.subscribe(_ => this.updateRequestSummary()));
242243
this.consoleOperation().request.body.subscribe(_ => this.updateRequestSummary());
243244
this.consoleOperation().request.queryParameters().forEach(parameter => parameter.value.subscribe(_ => this.updateRequestSummary()));
244245

@@ -320,6 +321,7 @@ export class OperationConsole {
320321
const newHeader = new ConsoleHeader();
321322
this.consoleOperation().request.headers.push(newHeader);
322323
newHeader.value.subscribe(_ => this.updateRequestSummary());
324+
newHeader.name.subscribe(_ => this.updateRequestSummary());
323325

324326
this.updateRequestSummary();
325327
}

src/models/console/consoleHeader.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as ko from "knockout";
2+
import { KnownHttpHeaders } from "../knownHttpHeaders";
23
import { Parameter } from "../parameter";
34

45
export class ConsoleHeader {
@@ -9,7 +10,7 @@ export class ConsoleHeader {
910
public readonly options: string[];
1011
public inputTypeValue: ko.Observable<string>;
1112
public required: boolean;
12-
public secret: boolean;
13+
public secret: ko.Observable<boolean>;
1314
public revealed: ko.Observable<boolean>;
1415
public description: string;
1516
public type: string;
@@ -27,6 +28,7 @@ export class ConsoleHeader {
2728
constructor(contract?: Parameter) {
2829
this.name = ko.observable(null);
2930
this.value = ko.observable(null);
31+
this.secret = ko.observable();
3032
this.revealed = ko.observable(false);
3133
this.inputTypeValue = ko.observable("text");
3234
this.options = [];
@@ -37,8 +39,18 @@ export class ConsoleHeader {
3739
this.description = "Additional header.";
3840
this.hiddenValue = ko.computed<string>(() => this.value()?.replace(/./g, "•"));
3941

42+
this.name.subscribe(name => {
43+
if (name == KnownHttpHeaders.Authorization) {
44+
this.secret(true);
45+
} else {
46+
this.secret(false);
47+
}
48+
});
49+
50+
this.secret.subscribe(() => this.inputTypeValue((this.secret() && !this.revealed() ? "password" : "text")));
51+
4052
this.revealed.subscribe(() => {
41-
this.inputTypeValue(this.secret && !this.revealed() ? "password" : "text");
53+
this.inputTypeValue(this.secret() && !this.revealed() ? "password" : "text");
4254
});
4355

4456
this.name.extend(<any>{ required: { message: `Name is required.` } });
@@ -52,8 +64,7 @@ export class ConsoleHeader {
5264
this.options = contract.values;
5365
this.description = contract.description ? contract.description : "";
5466
this.type = contract.type;
55-
this.secret = false;
56-
this.inputTypeValue(this.secret && !this.revealed() ? "password" : "text");
67+
this.secret(this.name() == KnownHttpHeaders.Authorization ? true : false);
5768

5869
if (this.required) {
5970
this.value.extend(<any>{ required: { message: `Value is required.` } });

0 commit comments

Comments
 (0)