Skip to content

Commit

Permalink
Merge pull request #227 from authts/fix-225
Browse files Browse the repository at this point in the history
fix 225
  • Loading branch information
pamapa authored Nov 30, 2021
2 parents 8766545 + f15ba9e commit 72a8baf
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 151 deletions.
19 changes: 2 additions & 17 deletions docs/oidc-client-ts.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export class MetadataService {
// (undocumented)
protected _getMetadataProperty(name: keyof OidcMetadata, optional?: boolean): Promise<string | boolean | string[] | undefined>;
// (undocumented)
getRevocationEndpoint(): Promise<string | undefined>;
getRevocationEndpoint(optional?: boolean): Promise<string | undefined>;
// (undocumented)
getSigningKeys(): Promise<SigningKey[] | null>;
// (undocumented)
Expand Down Expand Up @@ -699,15 +699,6 @@ export interface StateStore {
set(key: string, value: string): Promise<void>;
}

// @public (undocumented)
export class TokenRevocationClient {
constructor(settings: OidcClientSettingsStore, metadataService: MetadataService);
// (undocumented)
revoke(token: string, required: boolean, type?: string): Promise<void>;
// (undocumented)
protected _revoke(url: string, client_id: string, client_secret: string | undefined, token: string, type: string): Promise<void>;
}

// @public (undocumented)
export class User {
constructor(args: {
Expand Down Expand Up @@ -775,11 +766,7 @@ export class UserManager {
// (undocumented)
revokeAccessToken(): Promise<void>;
// (undocumented)
protected _revokeAccessTokenInternal(access_token: string, required: boolean): Promise<boolean>;
// (undocumented)
protected _revokeInternal(user: User | null, required?: boolean): Promise<boolean>;
// (undocumented)
protected _revokeRefreshTokenInternal(refresh_token: string | undefined, required: boolean): Promise<boolean>;
protected _revokeInternal(user: User | null, optional: boolean): Promise<boolean>;
// (undocumented)
protected readonly _sessionMonitor: SessionMonitor | null;
readonly settings: UserManagerSettingsStore;
Expand Down Expand Up @@ -826,8 +813,6 @@ export class UserManager {
// (undocumented)
protected readonly _tokenClient: TokenClient;
// (undocumented)
protected readonly _tokenRevocationClient: TokenRevocationClient;
// (undocumented)
protected _useRefreshToken(user: User): Promise<User>;
// (undocumented)
protected get _userStoreKey(): string;
Expand Down
1 change: 1 addition & 0 deletions samples/Parcel/src/code-flow-identityserver/sample.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<button id="getUser">get user</button>
<button id="removeUser">remove user</button>
<button id="querySessionStatus">query user status at token server</button>
<button id="revokeAccessToken">revoke access token</button>
</div>
<div>
<button id="startSigninMainWindow">start signin main window</button>
Expand Down
25 changes: 17 additions & 8 deletions samples/Parcel/src/code-flow-identityserver/sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ document.getElementById("clearState").addEventListener("click", clearState, fals
document.getElementById("getUser").addEventListener("click", getUser, false);
document.getElementById("removeUser").addEventListener("click", removeUser, false);
document.getElementById("querySessionStatus").addEventListener("click", querySessionStatus, false);
document.getElementById("revokeAccessToken").addEventListener("click", revokeAccessToken, false);

document.getElementById("startSigninMainWindow").addEventListener("click", startSigninMainWindow, false);
document.getElementById("endSigninMainWindow").addEventListener("click", endSigninMainWindow, false);
Expand Down Expand Up @@ -114,6 +115,22 @@ function removeUser() {
});
}

function querySessionStatus() {
mgr.querySessionStatus().then(function(status) {
log("user's session status", status);
}).catch(function(err) {
log(err);
});
}

function revokeAccessToken() {
mgr.revokeAccessToken().then(function() {
log("access token revoked");
}).catch(function(err) {
log(err);
});
}

function startSigninMainWindow() {
mgr.signinRedirect(/*{useReplaceToNavigate:true}*/).then(function() {
log("signinRedirect done");
Expand Down Expand Up @@ -158,14 +175,6 @@ function iframeSignin() {
});
}

function querySessionStatus() {
mgr.querySessionStatus().then(function(status) {
log("user's session status", status);
}).catch(function(err) {
log(err);
});
}

function startSignoutMainWindow() {
mgr.signoutRedirect().then(function(resp) {
log("signed out", resp);
Expand Down
18 changes: 10 additions & 8 deletions src/JsonService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ describe("JsonService", () => {
Accept: "application/json",
"Content-Type": "application/json"
}),
json: () => Promise.resolve(json)
text: () => Promise.resolve(JSON.stringify(json))
} as Response);

// act
Expand Down Expand Up @@ -263,7 +263,7 @@ describe("JsonService", () => {
Accept: "application/json",
"Content-Type": "application/json"
}),
json: () => Promise.reject(error)
text: () => Promise.reject(error)
} as Response);

// act
Expand All @@ -282,7 +282,7 @@ describe("JsonService", () => {
Accept: "application/json",
"Content-Type": "text/html"
}),
json: () => Promise.resolve(json)
text: () => Promise.resolve(JSON.stringify(json))
} as Response);

// act
Expand All @@ -301,7 +301,7 @@ describe("JsonService", () => {
Accept: "application/json",
"Content-Type": "application/json"
}),
json: () => Promise.resolve(json)
text: () => Promise.resolve(JSON.stringify(json))
} as Response);

// act
Expand All @@ -320,7 +320,7 @@ describe("JsonService", () => {
Accept: "application/json",
"Content-Type": "application/json"
}),
json: () => Promise.resolve(json)
text: () => Promise.resolve(JSON.stringify(json))
} as Response);

// act
Expand All @@ -339,7 +339,7 @@ describe("JsonService", () => {
Accept: "application/json",
"Content-Type": "application/json"
}),
json: () => Promise.reject(new SyntaxError("Unexpected token a in JSON"))
text: () => Promise.resolve("not_json_data")
} as Response);

// act
Expand All @@ -358,7 +358,7 @@ describe("JsonService", () => {
Accept: "application/json",
"Content-Type": "text/html"
}),
json: () => Promise.resolve(json)
text: () => Promise.resolve(JSON.stringify(json))
} as Response);

// act
Expand All @@ -369,11 +369,13 @@ describe("JsonService", () => {

it("should reject promise when http response is not 200", async () => {
// arrange
const json = {};
mocked(fetch).mockResolvedValue({
status: 500,
statusText: "server error",
ok: false,
headers: new Headers(),
text: () => Promise.resolve(JSON.stringify(json))
} as Response);

// act
Expand All @@ -393,7 +395,7 @@ describe("JsonService", () => {
Accept: "application/json",
"Content-Type": "foo/bar"
}),
json: () => Promise.resolve(json)
text: () => Promise.resolve(JSON.stringify(json))
} as Response);

// act
Expand Down
9 changes: 7 additions & 2 deletions src/JsonService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,27 @@ export class JsonService {
if (contentType && !this._contentTypes.find(item => contentType.startsWith(item))) {
throw new Error(`Invalid response Content-Type: ${(contentType ?? "undefined")}, from URL: ${url}`);
}
let json: Record<string, unknown>;

const responseText = await response.text();

let json: Record<string, unknown> = {};
try {
json = await response.json();
json = JSON.parse(responseText);
}
catch (err) {
this._logger.error("postForm: Error parsing JSON response", err);
if (response.ok) throw err;
throw new Error(`${response.statusText} (${response.status})`);
}

if (!response.ok) {
this._logger.error("postForm: Error from server:", json);
if (json.error) {
throw new ErrorResponse(json);
}
throw new Error(`${response.statusText} (${response.status}): ${JSON.stringify(json)}`);
}

return json;
}
}
4 changes: 2 additions & 2 deletions src/MetadataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ export class MetadataService {
return this._getMetadataProperty("end_session_endpoint", true) as Promise<string | undefined>;
}

public getRevocationEndpoint(): Promise<string | undefined> {
return this._getMetadataProperty("revocation_endpoint", true) as Promise<string | undefined>;
public getRevocationEndpoint(optional = true): Promise<string | undefined> {
return this._getMetadataProperty("revocation_endpoint", optional) as Promise<string | undefined>;
}

public getKeysEndpoint(optional?: true): Promise<string | undefined>
Expand Down
Loading

0 comments on commit 72a8baf

Please sign in to comment.